Elasticsearch之RestClient查询文档

GET /goods/_search
{
  "query": {
    "match_all": {}
  }
}
SearchRequest searchRequest=new SearchRequest("goods");
        searchRequest.source().query(QueryBuilders.matchAllQuery());
        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = search.getHits();
        long value = hits.getTotalHits().value;
        for (SearchHit hit : hits.getHits()) {
            System.out.println(hit.getSourceAsString());
        }
GET /goods/_search
{
  "query": {
    "match": {
      "title": "华为"
    }
  }
}
 SearchRequest searchRequest=new SearchRequest("goods");
        searchRequest.source().query(QueryBuilders.matchQuery("title","华为"));
        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = search.getHits();
        long value = hits.getTotalHits().value;
        for (SearchHit hit : hits.getHits()) {
            System.out.println(hit.getSourceAsString());
        }
GET /goods/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "brandName": {
              "value": "Apple"
            }
          }
        }
      ],
      "filter": [
        {
          "range": {
            "price": {
              "gte": 7000,
              "lte": 8000
            }
          }
        }
      ]
    }
  }
}
 SearchRequest searchRequest=new SearchRequest("goods");
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.termQuery("brandName","Apple"))
                .filter(QueryBuilders.rangeQuery("price").gte(7000).lte(8000));
        searchRequest.source().query(boolQueryBuilder);
        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = search.getHits();
        long value = hits.getTotalHits().value;
        for (SearchHit hit : hits.getHits()) {
            System.out.println(hit.getSourceAsString());
        }
GET /goods/_search
{
  "query": {
    "match_all": {}
  },
  "from": 0,
  "size": 20
  , "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ]
}
  SearchRequest searchRequest=new SearchRequest("goods");
        searchRequest.source().query(QueryBuilders.matchAllQuery()).from(0).size(20).sort("price", SortOrder.DESC);
        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = search.getHits();
        long value = hits.getTotalHits().value;
        for (SearchHit hit : hits.getHits()) {
            System.out.println(hit.getSourceAsString());
        }

 

GET /goods/_search
{
  "query": {
    "match": {
      "title": "手机"
    }
  },
  "highlight": {
    "fields": {
      "title": {
        "pre_tags": [
          ""
        ],
        "post_tags": [
          ""
        ]
      }
    }
  }
}
SearchRequest searchRequest=new SearchRequest("goods");
        searchRequest.source().query(QueryBuilders.matchQuery("title","手机"))
                .highlighter(new HighlightBuilder().field("title").preTags("").postTags(""));
        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = search.getHits();
        long value = hits.getTotalHits().value;
        for (SearchHit hit : hits.getHits()) {
            Goods goods = JSON.parseObject(hit.getSourceAsString(), Goods.class);
            Map highlightFields = hit.getHighlightFields();
            if(highlightFields!=null){
                HighlightField highlightField = highlightFields.get("title");
                if(highlightField!=null){
                    String string = highlightField.getFragments()[0].string();
                    goods.setTitle(string);
                }
            }
            System.out.println(goods);

你可能感兴趣的:(ElasticSearch,elasticsearch,java,大数据)