当前位置:   article > 正文

Elasticsearch之RestClient查询文档_elasticsearch restclient

elasticsearch restclient

  1. GET /goods/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. }
  6. }
  1. SearchRequest searchRequest=new SearchRequest("goods");
  2. searchRequest.source().query(QueryBuilders.matchAllQuery());
  3. SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
  4. SearchHits hits = search.getHits();
  5. long value = hits.getTotalHits().value;
  6. for (SearchHit hit : hits.getHits()) {
  7. System.out.println(hit.getSourceAsString());
  8. }
  1. GET /goods/_search
  2. {
  3. "query": {
  4. "match": {
  5. "title": "华为"
  6. }
  7. }
  8. }
  1. SearchRequest searchRequest=new SearchRequest("goods");
  2. searchRequest.source().query(QueryBuilders.matchQuery("title","华为"));
  3. SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
  4. SearchHits hits = search.getHits();
  5. long value = hits.getTotalHits().value;
  6. for (SearchHit hit : hits.getHits()) {
  7. System.out.println(hit.getSourceAsString());
  8. }
  1. GET /goods/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [
  6. {
  7. "term": {
  8. "brandName": {
  9. "value": "Apple"
  10. }
  11. }
  12. }
  13. ],
  14. "filter": [
  15. {
  16. "range": {
  17. "price": {
  18. "gte": 7000,
  19. "lte": 8000
  20. }
  21. }
  22. }
  23. ]
  24. }
  25. }
  26. }
  1. SearchRequest searchRequest=new SearchRequest("goods");
  2. BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
  3. boolQueryBuilder.must(QueryBuilders.termQuery("brandName","Apple"))
  4. .filter(QueryBuilders.rangeQuery("price").gte(7000).lte(8000));
  5. searchRequest.source().query(boolQueryBuilder);
  6. SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
  7. SearchHits hits = search.getHits();
  8. long value = hits.getTotalHits().value;
  9. for (SearchHit hit : hits.getHits()) {
  10. System.out.println(hit.getSourceAsString());
  11. }
  1. GET /goods/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6. "from": 0,
  7. "size": 20
  8. , "sort": [
  9. {
  10. "price": {
  11. "order": "desc"
  12. }
  13. }
  14. ]
  15. }
  1. SearchRequest searchRequest=new SearchRequest("goods");
  2. searchRequest.source().query(QueryBuilders.matchAllQuery()).from(0).size(20).sort("price", SortOrder.DESC);
  3. SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
  4. SearchHits hits = search.getHits();
  5. long value = hits.getTotalHits().value;
  6. for (SearchHit hit : hits.getHits()) {
  7. System.out.println(hit.getSourceAsString());
  8. }

 

  1. GET /goods/_search
  2. {
  3. "query": {
  4. "match": {
  5. "title": "手机"
  6. }
  7. },
  8. "highlight": {
  9. "fields": {
  10. "title": {
  11. "pre_tags": [
  12. "<font color='red'>"
  13. ],
  14. "post_tags": [
  15. "</font>"
  16. ]
  17. }
  18. }
  19. }
  20. }
  1. SearchRequest searchRequest=new SearchRequest("goods");
  2. searchRequest.source().query(QueryBuilders.matchQuery("title","手机"))
  3. .highlighter(new HighlightBuilder().field("title").preTags("<font color=\'red\'>").postTags("</font>"));
  4. SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
  5. SearchHits hits = search.getHits();
  6. long value = hits.getTotalHits().value;
  7. for (SearchHit hit : hits.getHits()) {
  8. Goods goods = JSON.parseObject(hit.getSourceAsString(), Goods.class);
  9. Map<String, HighlightField> highlightFields = hit.getHighlightFields();
  10. if(highlightFields!=null){
  11. HighlightField highlightField = highlightFields.get("title");
  12. if(highlightField!=null){
  13. String string = highlightField.getFragments()[0].string();
  14. goods.setTitle(string);
  15. }
  16. }
  17. System.out.println(goods);

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/659142
推荐阅读
相关标签
  

闽ICP备14008679号