es : java 查询

这篇具有很好参考价值的文章主要介绍了es : java 查询。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1. POM 配置

 <dependency>
      <groupId>org.elasticsearch.client</groupId>
      <artifactId>elasticsearch-rest-high-level-client</artifactId>
      <version>7.6.2</version>
 </dependency>

2. 建立ES集群连接


RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost(IP, PORT, "http")));

同样,如果我们要连接集群中多个ES节点时,只需要在RestClient 的 builder 方法中多添加几个HttpPost对象即可
String ipPort = "10.xx:9200,10.xx:9200,10.xx:9200";
String[] ipPortArry = ipPort.split(",");
List<HttpHost>  httpHostsList = new ArrayList();
        for(String ips : ipPortArry){
           String[] ipArray = ips.split(":");
            httpHostsList.add(new HttpHost(ipArray[0], Integer.parseInt(ipArray[1]), "http"));
        }

RestHighLevelClient client = new RestHighLevelClient(  RestClient.builder(httpHostsList.toArray(new HttpHost[httpHostsList.size()])));


补充:
Java配置多个ES节点时,请求的时候会随机选一个节点作为协调节点负责分发请求和处理结果,所以Java链接ES节点数量的多少,不会影响到Java请求ES查询结果的速度,只是其中某个节点宕机时,其他节点可以保证正常的查询和操作。

ES 端口9200与9300的区别:
9200作为Http协议,主要用于外部通讯
9300作为Tcp协议,jar之间就是通过tcp协议通讯
ES集群之间是通过9300进行通讯

3. 简单的查询并获取查询结果

RestHighLevelClient client = new RestHighLevelClient(
            RestClient.builder(
                new HttpHost("localhost", 9200, "http")));

SearchRequest searchRequest = new SearchRequest("my_index");
//或者
/*
 SearchRequest searchRequest = new SearchRequest();
 searchRequest.indices("my_index");
*/

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
//配置source源字段过虑,1显示的,2排除的
searchSourceBuilder.fetchSource(new String[]{"name","studymodel","description"},new String[]{});

searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest);
//获取所有搜索结果、总匹配数量
        SearchHits hits = searchResponse.getHits();
        long totalHits = hits.getTotalHits();    //如果总是超过1万, index没有设置,则返回1万
        SearchHit[] searchHits = hits.getHits();
        //遍历结果
        for(SearchHit searchHit:searchHits){
            String index = searchHit.getIndex();
            String type = searchHit.getType();
            String id = searchHit.getId();
            float score = searchHit.getScore();
            String sourceAsString = searchHit.getSourceAsString();
            Map<String, Object> sourceAsMap = searchHit.getSourceAsMap();
            String name = (String) sourceAsMap.get("name");
            String studymodel = (String) sourceAsMap.get("studymodel");
            String description = (String) sourceAsMap.get("description");
            System.out.println(name);
            System.out.println(studymodel);
            System.out.println(description);
        }

//关闭链接
client.close()


es的dsl: 
 {
        "query": {
        	"match_all": {}
        },
        "_source" : ["name","studymodel","description"]
}

4. 使用es的json 拼接查询语句

StringBuffer dsl = new StringBuffer();
		dsl.append("{\"bool\": {");
		dsl.append("      \"must\": [");
		dsl.append("        {");
		dsl.append("          \"term\": {");
		dsl.append("            \"mdid.keyword\": {");
		dsl.append("              \"value\": \"2fa9d41e1af460e0d47ce36ca8a98737\"");
		dsl.append("            }");
		dsl.append("          }");
		dsl.append("        }");
		dsl.append("      ]");
		dsl.append("    }");
		dsl.append("}");


 SearchRequest searchRequest = new SearchRequest();
 searchRequest.indices("my_index");


SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(ueryBuilders.wrapperQuery(dsl.toString()));
//配置source源字段过虑,1显示的,2排除的
searchSourceBuilder.fetchSource(new String[]{"name","studymodel","description"},new String[]{});

searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest);

5. query 

5.0 QueryBuilders转换为JSON字符串,方便调试
QueryBuilder query = QueryBuilders.termQuery("name", "John");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(query);

String json = sourceBuilder.toString();
System.out.println(json);
并将其转换为JSON字符串
toString()方法返回的字符串是格式化的,可以在控制台中方便地查看和调试。
5.1 Term Query
 {
        "query": {
            "term" : {
                "name": "spring"
            }
        },
        "_source" : ["name","studymodel"]
    }


SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.termQuery("name","spring"));
searchSourceBuilder.fetchSource(new String[]{"name","studymodel","description"}, new String[]{});
 
5.2  terms query ES提供根据多个id值匹配的方法
{
  "query": {
    "terms": {
      "systemAssignedTo.keyword": [
        "1",
        "2"
      ]
    }
    
  }
}

select  *  from  mr_zcy where  systemAssignedTo in ('1','2')

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
String[] ids = {"1", "2"};
searchSourceBuilder.query(QueryBuilders.termsQuery("systemAssignedTo.keyword",ids));

 
5.3 match query (匹配单个字段)
{
        "query": {
            "match": {
                "name": {
                    "query": "spring开发",
                    "operator": "or"
                }
            }
        },
    	"_source" : ["name"]
    }
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("name","spring开发").operator(Operator.OR));
searchSourceBuilder.fetchSource(new String[]{"name"}, new String[]{});
5.4 minimum_should_match
{
        "query": {
            "match": {
                "description": {
                    "query": "spring开发框架",
                    "minimum_should_match": "80%"
                }
            }
        }
    }
spring开发框架 会被分为三个词:spring、开发、框架

设置 minimum_should_match:80% 表示,三个词在文档的匹配占比为 80%,即 3 * 0.8=2.4,向上取整得2,表示至少有 两个词 在文档中要匹配成功。

searchSourceBuilder.query(QueryBuilders.matchQuery("description","spring开发框架").minimumShouldMatch("80%"));
5.5 multi query (匹配多个字段)
{
        "query": {
            "multi_match": {
                "query": "spring框架",
                "minimum_should_match": "50%",
                "fields": [
                    "name^10",
                    "description"
                ]
            }
        }
    }
拿关键字 spring css去匹配 name 和 description 字段。
name^10 表示权重提升 10 倍,执行上边的查询,发现 name 中包括 spring 关键字的文档排在前边
MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery("spring框架", "name", "description").minimumShouldMatch("50%");
multiMatchQueryBuilder.field("name",10);
searchSourceBuilder.query(multiMatchQueryBuilder);
 5.6 布尔查询
{
        "_source": [
            "name",
            "studymodel",
            "description"
        ],
        "from": 0,
        "size": 1,
        "query": {
            "bool": {
                "must": [
                    {
                        "multi_match": {
                            "query": "spring框架",
                            "minimum_should_match": "50%",
                            "fields": [
                                "name^10",
                                "description"
                            ]
                        }
                    },
                    {
                        "term": {
                            "studymodel": "201001"
                        }
                    }
                ]
            }
        }
    }




        //创建multiMatch查询
MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery("spring框架", "name", "description").minimumShouldMatch("50%");
        multiMatchQueryBuilder.field("name",10);
        //创建term查询
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("studymodel", 201001);

        //创建布尔查询
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(multiMatchQueryBuilder);
boolQueryBuilder.must(termQueryBuilder);
5.7 过滤器 
{
        "_source": [
            "name",
            "studymodel",
            "description",
            "price"
        ],
        "query": {
            "bool": {
                "must": [
                    {
                        "multi_match": {
                            "query": "spring框架",
                            "minimum_should_match": "50%",
                            "fields": [
                                "name^10",
                                "description"
                            ]
                        }
                    }
                ],
                "filter": [
                    {
                        "term": {
                            "studymodel": "201001"
                        }
                    },
                    {
                        "range": {
                            "price": {
                                "gte": 60,
                                "lte": 100
                            }
                        }
                    }
                ]
            }
        }
    }


SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //创建multiMatch查询
MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery("spring框架", "name", "description").minimumShouldMatch("50%");

multiMatchQueryBuilder.field("name",10);

        //布尔查询
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(multiMatchQueryBuilder);
        //过虑条件
boolQueryBuilder.filter(QueryBuilders.termQuery("studymodel", "201001"));
boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(60).lte(100));
searchSourceBuilder.query(boolQueryBuilder);
 5.8 排序

可以在字段上添加一个或多个排序,支持在 keyword、date、float 等类型上添加,text 类型的字段上不允许添加排序。

{
       "_source": [
           "name",
           "studymodel",
           "description",
           "price"
       ],
       "query": {
           "bool": {
               "filter": [
                   {
                       "range": {
                           "price": {
                               "gte": 0,
                               "lte": 100
                           }
                       }
                   }
               ]
           }
       },
       "sort": [
           {
               "studymodel": "desc"
           },
           {
               "price": "asc"
           }
       ]
   }


SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //boolQuery搜索方式
        //定义一个boolQuery
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        //定义过虑器
 boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(0).lte(100));
        
searchSourceBuilder.query(boolQueryBuilder);

        //添加排序
        searchSourceBuilder.sort("studymodel", SortOrder.DESC);
        searchSourceBuilder.sort("price", SortOrder.ASC);
        searchSourceBuilder.fetchSource(new String[]{"name","studymodel","description"}, new String[]{});
        //向搜索请求对象中设置搜索源
        searchRequest.source(searchSourceBuilder);
 5.9 高亮
public void testHighlight() throws IOException, ParseException {
        //搜索请求对象
        SearchRequest searchRequest = new SearchRequest("xc_course");
        //指定类型
        searchRequest.types("doc");
        //搜索源构建对象
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        //boolQuery搜索方式
        //先定义一个MultiMatchQuery
        MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery("开发框架", "name", "description")
                .minimumShouldMatch("50%")
                .field("name", 10);

        //定义一个boolQuery
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(multiMatchQueryBuilder);
        //定义过虑器
        boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(0).lte(100));

        searchSourceBuilder.query(boolQueryBuilder);
        //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
        searchSourceBuilder.fetchSource(new String[]{"name","studymodel","price","timestamp"},new String[]{});

        //设置高亮
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.preTags("<tag>");
        highlightBuilder.postTags("</tag>");
        highlightBuilder.fields().add(new HighlightBuilder.Field("name"));
//        highlightBuilder.fields().add(new HighlightBuilder.Field("description"));
        searchSourceBuilder.highlighter(highlightBuilder);

        //向搜索请求对象中设置搜索源
        searchRequest.source(searchSourceBuilder);
        //执行搜索,向ES发起http请求
        SearchResponse searchResponse = client.search(searchRequest);
        //搜索结果
        SearchHits hits = searchResponse.getHits();
        //匹配到的总记录数
        long totalHits = hits.getTotalHits();
        //得到匹配度高的文档
        SearchHit[] searchHits = hits.getHits();
        //日期格式化对象
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for(SearchHit hit:searchHits){
            //文档的主键
            String id = hit.getId();
            //源文档内容
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            //源文档的name字段内容
            String name = (String) sourceAsMap.get("name");
            //取出高亮字段
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            if(highlightFields!=null){
                //取出name高亮字段
                HighlightField nameHighlightField = highlightFields.get("name");
                if(nameHighlightField!=null){
                    Text[] fragments = nameHighlightField.getFragments();
                    StringBuffer stringBuffer = new StringBuffer();
                    for(Text text:fragments){
                        stringBuffer.append(text);
                    }
                    name = stringBuffer.toString();
                }
            }

            //由于前边设置了源文档字段过虑,这时description是取不到的
            String description = (String) sourceAsMap.get("description");
            //学习模式
            String studymodel = (String) sourceAsMap.get("studymodel");
            //价格
            Double price = (Double) sourceAsMap.get("price");
            //日期
            Date timestamp = dateFormat.parse((String) sourceAsMap.get("timestamp"));
            System.out.println(name);
            System.out.println(studymodel);
            System.out.println(description);
        }

    }
 5.10 聚合
#聚合搜索 address 中包含 mill 的所有人的年龄分布以及平均薪资
GET bank/_search
{
  "query":{
    "match": {
      "address": "mill"
    }
  },
  "aggs": {
    "ageAgg": {
      "terms": {
        "field": "age",
        "size": 10
      }
    },
    "balanceAvg":{
      "avg":{
        "field": "balance"
      }
    }
  },
  "size": 0
}


SearchRequest searchRequest = new SearchRequest();
//指定索引
searchRequest.indices("bank");
		//指定DSL 检索条件
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("address","mill"));
//按照年龄只分布进行聚合
TermsAggregationBuilder ageAgg = AggregationBuilders.terms("ageAgg").field("age").size(10);
		searchSourceBuilder.aggregation(ageAgg);
//计算平均薪资
AvgAggregationBuilder balanceAvg = AggregationBuilders.avg("balanceAvg").field("blance");
searchSourceBuilder.aggregation(balanceAvg);

//打印检索条件
System.out.println("检索条件:"+searchSourceBuilder);
searchRequest.source(searchSourceBuilder);
//执行检索
SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = search.getHits();
SearchHit[] searchHits = hits.getHits();
for (SearchHit hit: searchHits){

			String sourceAsString = hit.getSourceAsString();
			Accout accout = JSON.parseObject(sourceAsString, Accout.class);
			System.out.println(accout.toString());
}
//获取检索的分析信息
Aggregations aggregations = search.getAggregations();
//		for (Aggregation aggregation : aggregations.asList()) {
//			System.out.println("当前聚合名字:"+aggregation.getName());
//		}
//分类聚合
Terms ageAgg1 = aggregations.get("ageAgg");
for (Terms.Bucket bucket : ageAgg1.getBuckets()) {
			String keyAsString = bucket.getKeyAsString();
			System.out.println("年龄:" + keyAsString + "人数:"+bucket.getDocCount());
}
//平局值
Avg balanceAvg1 = aggregations.get("balanceAvg");
System.out.println("平均薪资"+ balanceAvg1.getValue());

6.分页查询

6.1  from,size
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
//分页查询,设置起始下标,从0开始
searchSourceBuilder.from(0);
//每页显示个数
searchSourceBuilder.size(10);
//source源字段过虑
searchSourceBuilder.fetchSource(new String[]{"name","studymodel"}, new String[]{});
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest);
6.2 searchAfter
searchSourceBuilder.query(QueryBuilders.wrapperQuery(dsl.toString()));
searchSourceBuilder.size(10);
searchSourceBuilder.sort("id", SortOrder.ASC);
searchRequest.source(searchSourceBuilder);

SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] searchHits = searchResponse.getHits().getHits();
        while (searchHits.length > 0) {
   
            SearchHit last = searchHits[searchHits.length - 1];
            sourceBuilder.searchAfter(last.getSortValues());
            searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            searchHits = searchResponse.getHits().getHits();
        }
6.3 Scroll分页 

Elasticsearch-Scroll分页-Java示例-CSDN博客文章来源地址https://www.toymoban.com/news/detail-770782.html

searchSourceBuilder.query(QueryBuilders.wrapperQuery(dsl.toString()));
searchSourceBuilder.size(10);
searchSourceBuilder.sort("id", SortOrder.ASC);

searchRequest.source(searchSourceBuilder);
// 指定超时时间
searchRequest.scroll(new TimeValue(5000));

SearchResponse searchResponse = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);

long length = searchResponse.getHits().getHits().length;
String scrollId = null;
while (length > 0) {
            consumer.accept(searchResponse);
            scrollId = searchResponse.getScrollId();
            SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId)
                    .scroll(keepAlive);
            searchResponse = highLevelClient.scroll(scrollRequest, RequestOptions.DEFAULT);
            length = searchResponse.getHits().getHits().length;
}

// 清空快照记录,避免内存占用
        if (isClearScroll && scrollId != null) {
            ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
            clearScrollRequest.addScrollId(scrollId);

            highLevelClient.clearScrollAsync(clearScrollRequest, RequestOptions.DEFAULT, new ActionListener<ClearScrollResponse>() {
            @Override
            public void onResponse(ClearScrollResponse clearScrollResponse) {

            }

            @Override
            public void onFailure(Exception e) {
                throw new ElasticsearchException(e);
            }
        });

        }


到了这里,关于es : java 查询的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 从白日梦到现实:推出 Elastic 的管道查询语言 ES|QL

    作者:George Kobar, Bahubali Shetti, Mark Settle 今天,我们很高兴地宣布 Elastic® 的新管道查询语言 ES|QL(Elasticsearch 查询语言)的技术预览版,它可以转换、丰富和简化数据调查。 ES|QL 由新的查询引擎提供支持,通过并发处理提供高级搜索功能,无论数据源和结构如何,都可以提高

    2024年02月04日
    浏览(55)
  • ElasticSearch第三讲:ES详解 - Elastic Stack生态和场景方案

    本文是ElasticSearch第三讲,在了解ElaticSearch之后,我们还要了解Elastic背后的生态 即我们 常说的ELK ;与此同时,还会给你展示ElasticSearch的 案例场景 ,让你在学习ES前对它有个全局的印象。 Beats + Logstash + ElasticSearch + Kibana 如下是我从官方博客中找到图,这张图展示了ELK生态以

    2024年02月07日
    浏览(40)
  • Elastic Search的RestFul API入门:如何进行ES的查询-search

    在这篇教学文章中,我们将深入探讨Elasticsearch的search功能。这是一个非常强大且灵活的功能,它允许我们对存储在Elasticsearch中的数据进行各种复杂的查询和分析。本章的目标是让读者理解如何进行Elasticsearch的搜索,以及如何在搜索过程中自主调整搜索参数,从而灵活地控制

    2024年02月03日
    浏览(50)
  • ElasticSearch进阶:一文全览各种ES查询在Java中的实现

    ElasticSearch进阶:一文全览各种ES查询在Java中的实现 es基本语句详解 查询语句详解 ElasticSearch第一篇: ElasticSearch基础:从倒排索引说起,快速认知ES 完整项目已上传至:ElasticSearch Demo 项目,该项目是关于springboot的集成项目,ElasticSearch部分请关注【elasticSearch-demo】模块。觉得

    2024年02月02日
    浏览(54)
  • Elastic Stack 8.11:引入一种新的强大查询语言 ES|QL

    作者:Tyler Perkins, Ninoslav Miskovic, Gilad Gal, Teresa Soler, Shani Sagiv, Jason Burns Elastic® Stack 8.11 引入了数据流生命周期、一种配置数据流保留和降采样(downsampling) 的简单方法(技术预览版),以及有关 Kibana® Inspector 中跨集群搜索响应的详细信息以及重新路由摄取处理器(reroute i

    2024年02月03日
    浏览(54)
  • ElasticSearch如何使用以及java代码如何查询并排序ES中的数据(距离排序)

    import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.geo.GeoDistance; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.common.unit.DistanceUnit; import org.elasticsearch.common.unit.

    2024年04月12日
    浏览(51)
  • 「Elasticsearch 」Es复合查询

    目录 Bool 查询   Dis_max 查询  Function_score 查询 Nested 查询  Geospatial 查询  1. Geo Point 查询 2. Geo Shape 查询 Elasticsearch(简称为ES)是一个基于Lucene的分布式搜索和分析引擎,它提供了丰富的查询语言和API,可以用于构建高性能、可扩展的全文搜索、日志分析和数据可视化等应用

    2024年02月13日
    浏览(40)
  • 【Elasticsearch】ES精确查询和范围查询,ES时间字段排序实例,ES倒排索引介绍

    termQuery matchQuery 模糊查询 multiMatchQuery 多个字段模糊查询 如果时间字段写入时用的类型是Text,可以用“时间字段.keyword”来处理 #查询前传入分页参数 #分页后拿到总记录数 把文档D对应到的映射转换为到文档ID的映射,每个都对应着一系列的文档,这些文

    2024年02月15日
    浏览(120)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包