【自学elasticsearch7】结合es语法和java的HighLevelClient:match、term和bool
1.match搜索:一般用来模糊查询
match语句的简单写法:在firstmapping索引下查询查询title中包含“华强”的文档
GET /firstmapping/_search { "query":{ "match": { "content": "华强"} } }
match在匹配时会对所查找的关键词进行分词,“华强”会被分成“华”和“强”,索引中所有包含这两个字其中之一的文档都会被查出。
对应的java代码:
public void matchTest(){ SearchRequest searchRequest = new SearchRequest("firstmapping");//构建请求头 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();//构建请求体 MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("content", "华强"); //构建查询参数 searchSourceBuilder.query(queryBuilder);//将查询条件赋给请求体 searchRequest.source(searchSourceBuilder);//将请求体赋给请求头 SearchResponse searchResponse = null; try{ searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); }catch (Exception e){ e.printStackTrace(); } SearchHit[] hits = searchResponse.getHits().getHits(); for (SearchHit hit : hits) { MapsourceAsMap = hit.getSourceAsMap(); System.out.println(sourceAsMap); } }
查询结果:
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3, "relation" : "eq" }, "max_score" : 1.7261486, "hits" : [ { "_index" : "firstmapping", "_type" : "_doc", "_id" : "125454", "_score" : 1.7261486, "_source" : { "title" : "华强来买阿瓦达啃大瓜", "content" : "此时一个摩托开着华强来买瓜......", "score" : "10.0" } }, { "_index" : "firstmapping", "_type" : "_doc", "_id" : "1224553", "_score" : 1.410736, "_source" : { "title" : "强啊bug", "content" : "bug很强", "score" : "5.5" } }, { "_index" : "firstmapping", "_type" : "_doc", "_id" : "12321313", "_score" : 1.021733, "_source" : { "title" : "为什么", "content" : "华为是一家通讯公司", "score" : "5.5" } } ] } }
2.term搜索:term一般用来进行精确查询,搭配bool查询使用
例子:在firstmapping索引下查询查询title中包含“华强”的文档,
GET /firstmapping/_search { "query": { "bool": { "must": [ { "term":{ "content": "强" } }, { "term":{ "content": "华" } } ] } } }
查询既包含“华”,又包含“强”的文档。
java代码:
public void termTest(){ SearchRequest searchRequest = new SearchRequest("firstmapping");//构建请求头 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();//构建请求体 BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery() .must(QueryBuilders.termQuery("content","华")) .must(QueryBuilders.termQuery("content","强")); //构建查询参数 searchSourceBuilder.query(queryBuilder);//将查询条件赋给请求体 searchRequest.source(searchSourceBuilder);//将请求体赋给请求头 SearchResponse searchResponse = null; try{ searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); }catch (Exception e){ e.printStackTrace(); } SearchHit[] hits = searchResponse.getHits().getHits(); for (SearchHit hit : hits) { MapsourceAsMap = hit.getSourceAsMap(); System.out.println(sourceAsMap); } }
查询结果:
{ "took" : 0, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.7261486, "hits" : [ { "_index" : "firstmapping", "_type" : "_doc", "_id" : "125454", "_score" : 1.7261486, "_source" : { "title" : "华强来买阿瓦达啃大瓜", "content" : "此时一个摩托开着华强来买瓜......", "score" : "10.0" } } ] } }
bool查询可以使用must和must_not组成多层过滤条件,查找出最合适的结果。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)