Some API implementations of ElasticSearch in Java

query all

Query the documents of the specified type in the specified index library. (by using this method)

{
    
    
	"query": {
    
    
		"match_all": {
    
    }
	},
	"_source" : ["name","studymodel"]
}

_source: source filter settings, specify which fields are included in the result.

Result description:

took: the time spent on this operation, in milliseconds.

timed_out: whether the request timed out

_shards: Indicates which shards were searched in this operation

hits: records hit by the search

hits.total : the total number of documents matching the criteria

hits.hits : top N documents with high matching

hits.max_score: document matching score, here is the highest score

_score: Each document has a match score, sorted in descending order.

_source: Shows the original content of the document.

Java Client

@SpringBootTest
@RunWith(SpringRunner.class)
public class TestDSL {
    
    

    @Autowired
    RestHighLevelClient restHighLevelClient;

    @Autowired
    RestClient restClient;

    //全部搜索
    @Test
    public void testSearchAll() throws IOException {
    
    
        //创建搜索对象
        SearchRequest searchRequest = new SearchRequest("xc_course");
        //指定类型
        searchRequest.types("doc");
        //创建搜索源对象
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //搜索方式 搜索全部
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        //设置源字段过滤 第一个参数是包括哪些字段,第二个参数是不包括哪些字段
        searchSourceBuilder.fetchSource(new String[]{
    
    "name","studymodel"},new String[]{
    
    });
        //向搜索对象中设置搜索源
        searchRequest.source(searchSourceBuilder);
        //执行搜索
        SearchResponse response = restHighLevelClient.search(searchRequest);
        //搜索结果
        SearchHits hits = response.getHits();
        //匹配的总记录数
        long totalHits = hits.totalHits;
        //得到匹配度最高的文档
        SearchHit[] hitsHits = hits.getHits();
        for (SearchHit hit : hitsHits){
    
    
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println(sourceAsMap);
        }
    }
}

Paging query

ES supports paging query, passing in two parameters: from and size.

form: Indicates the subscript of the starting document, starting from 0.

size: The number of documents to query.

{
    
     "
	from" : 0, "size" : 1,
	"query": {
    
    
		"match_all": {
    
    }
	},
	"_source" : ["name","studymodel"]
}

Java Client

SearchRequest searchRequest = new SearchRequest("xc_course");
searchRequest.types("xc_course");
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);

Term Query

Term Query is an exact query, which will match the keyword as a whole when searching, and no longer divide the keyword into words.

{
    
    
	"query": {
    
    
		"term" : {
    
    
			"name": "spring"
		}
	},
	"_source" : ["name","studymodel"]
}

The above search will look for documents whose name contains the word "spring".

Java Client

SearchRequest searchRequest = new SearchRequest("xc_course");
searchRequest.types("xc_course");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.termQuery("name","spring"));
//source源字段过虑
searchSourceBuilder.fetchSource(new String[]{
    
    "name","studymodel"}, new String[]{
    
    });
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest);

Exact match by id

ES provides a method for matching based on multiple id values:

{
    
    
	"query": {
    
    
		"ids" : {
    
    
			"type" : "doc",
			"values" : ["3", "4", "100"]
		}
	}
}

Java Client

String[] split = new String[]{
    
    "1","2"};
List<String> idList = Arrays.asList(split);
searchSourceBuilder.query(QueryBuilders.termsQuery("_id", idList));

match Query

1. Basic use

match Query is a full-text search. Its search method is to first segment the search string into words, and then use each entry to search from the index.

The difference between match query and Term query is that match query first divides the search keyword into words before searching, and then uses each word to search in the index.

{
    
    
	"query": {
    
    
		"match" : {
    
    
			"description" : {
    
    
				"query" : "spring开发",
				"operator" : "or"
			}
		}
	}
}

query: The keywords to search for. If there are multiple words in English keywords, they must be separated by half-width commas. For Chinese keywords, they can be separated by commas or not.

operator: or means that the condition is met as long as one word appears in the document, and means that the condition is met only if every word appears in the document.

The execution process of the above search is:

1. Divide "spring development" into two words, spring and development

2. Then use the two words spring and development to search in the matching index.

3. Since the operator is set to or, as long as one word matches successfully, the document will be returned.

Java Client

//根据关键字搜索
@Test
public void testMatchQuery() throws IOException {
    
    
	SearchRequest searchRequest = new SearchRequest("xc_course");
	searchRequest.types("xc_course");
	SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
	//source源字段过虑
	searchSourceBuilder.fetchSource(new String[]{
    
    "name","studymodel"}, new String[]{
    
    });
	//匹配关键字
	searchSourceBuilder.query(QueryBuilders.matchQuery("description", "spring开发").operator(Operator.OR));
	searchRequest.source(searchSourceBuilder);
	SearchResponse searchResponse = client.search(searchRequest);
	SearchHits hits = searchResponse.getHits();
	SearchHit[] searchHits = hits.getHits();
	for (SearchHit hit : searchHits) {
    
    
		String index = hit.getIndex();
		String type = hit.getType();
		String id = hit.getId();
		float score = hit.getScore();
		String sourceAsString = hit.getSourceAsString();
		Map<String, Object> sourceAsMap = hit.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);
	}
}

2、minimum_should_match

The operator = or used above means that as long as one word matches, the score will be scored. If at least two words match three words, how to achieve it?

Use minimum_should_match to specify the proportion of document matching words:

For example, the search statement is as follows:

{
    
    
	"query": {
    
    
		"match" : {
    
    
        "description" : {
    
    
        	"query" : "spring开发框架",
        	"minimum_should_match": "80%"
       		}
        }
    }
}

"spring development framework" will be divided into three words: spring, development, framework

Setting "minimum_should_match": "80%" means that the matching ratio of three words in the document is 80%, that is, 3*0.8=2.4, which is rounded up to 2, indicating that at least two words must be matched successfully in the document.

Java Client

//匹配关键字
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("description", "前台页面开发框架 架构").minimumShouldMatch("80%");//设置匹配占比
searchSourceBuilder.query(matchQueryBuilder);

multi Query

The termQuery and matchQuery learned above can only match one Field at a time. In this section, we will learn multiQuery, which can match multiple fields at a time.

1. The basic use of single-item matching is to match in one field, and multi-item matching is to use keywords to match in multiple Fields.

Take the keyword "spring css" to match the name and description fields

{
    
    
    "query": {
    
    
        "multi_match" : {
    
    
            "query" : "spring css",
            "minimum_should_match": "50%",
            "fields": [ "name", "description" ]
    	}
    }
}

2. Boost boost

When matching multiple fields, the boost (weight) of the field can be increased to improve the score

Boost boost, usually the weight of the keyword matching the name is higher than the weight of matching the description, here you can increase the weight of the name.

{
    
    
	"query": {
    
    
		"multi_match" : {
    
    
            "query" : "spring框架",
            "minimum_should_match": "50%",
            "fields": [ "name^10", "description" ]
		}
	}
}

"name^10" means that the weight is increased by 10 times, and the above query is executed, and it is found that the documents containing the spring keyword in the name are ranked first.

Java Client

MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery("spring框架",
"name", "description").minimumShouldMatch("50%");
multiMatchQueryBuilder.field("name",10);//提升boost

Boolean query

Boolean query corresponds to Lucene's BooleanQuery query, which realizes the combination of multiple queries.

Three parameters:

  • must: The document must match the query conditions included in must, which is equivalent to "AND"
  • should: The document should match one or more of the query conditions included in should, which is equivalent to "OR"
  • must_not: The document cannot match the query condition included in must_not, which is equivalent to "NOT"

Use must, should, must_not to test the query below:

{
    
    
    "_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"
                    }
                }
            ]
        }
    }
}

must: means must, multiple query conditions must be met. (usually use must)

should: Indicates or, as long as one of the multiple query conditions is satisfied.

must_not: means not.

Java Client

//BoolQuery,将搜索关键字分词,拿分词去索引库搜索
@Test
public void testBoolQuery() throws IOException {
    
    
	//创建搜索请求对象
	SearchRequest searchRequest= new SearchRequest("xc_course");
	searchRequest.types("doc");
	//创建搜索源配置对象
	SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
	searchSourceBuilder.fetchSource(new String[]{
    
    "name","pic","studymodel"},new String[]{
    
    });
	//multiQuery
	String keyword = "spring开发框架";
	MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery("spring框架",
	"name", "description").minimumShouldMatch("50%");
	multiMatchQueryBuilder.field("name",10);
	//TermQuery
	TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("studymodel", "201001");
	//布尔查询
	BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
	boolQueryBuilder.must(multiMatchQueryBuilder);
	boolQueryBuilder.must(termQueryBuilder);
	//设置布尔查询对象
	searchSourceBuilder.query(boolQueryBuilder);
	searchRequest.source(searchSourceBuilder);//设置搜索源配置
	SearchResponse searchResponse = client.search(searchRequest);
	SearchHits hits = searchResponse.getHits();
	SearchHit[] searchHits = hits.getHits();
	for(SearchHit hit:searchHits){
    
    
		Map<String, Object> sourceAsMap = hit.getSourceAsMap();
		System.out.println(sourceAsMap);
	}
}

filter

Filtering is to filter the search results. The filter mainly judges whether the document matches, and does not calculate and judge the matching score of the document. Therefore, the performance of the filter is higher than that of the query, and it is convenient for caching. It is recommended to use the filter as much as possible. Query or filter and query together.

Filters are used in Boolean queries, and the following filter is based on search results:

{
    
    
	"_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
						}
					}
				}
			]
		}
	}
}

range: Range filtering, keep records greater than or equal to 60 and less than or equal to 100.

term: Item matching filtering, keep the records with studymodel equal to "201001".

Note: range and term can only filter one Field at a time.

Java Client

//布尔查询使用过虑器
@Test
public void testFilter() throws IOException {
    
    
    SearchRequest searchRequest = new SearchRequest("xc_course");
    searchRequest.types("doc");
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    //source源字段过虑
    searchSourceBuilder.fetchSource(new String[]{
    
    "name","studymodel","price","description"},
    new String[]{
    
    });
    searchRequest.source(searchSourceBuilder);
    //匹配关键字
    MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery("spring框
    架", "name", "description");
    //设置匹配占比
    multiMatchQueryBuilder.minimumShouldMatch("50%");
    //提升另个字段的Boost值
    multiMatchQueryBuilder.field("name",10);
    searchSourceBuilder.query(multiMatchQueryBuilder);
    //布尔查询
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    boolQueryBuilder.must(searchSourceBuilder.query());
    //过虑
    boolQueryBuilder.filter(QueryBuilders.termQuery("studymodel", "201001"));
    boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(60).lte(100));
    SearchResponse searchResponse = client.search(searchRequest);
    SearchHits hits = searchResponse.getHits();
    SearchHit[] searchHits = hits.getHits();
    for (SearchHit hit : searchHits) {
    
    
        String index = hit.getIndex();
        String type = hit.getType();
        String id = hit.getId();
        float score = hit.getScore();
        String sourceAsString = hit.getSourceAsString();
        Map<String, Object> sourceAsMap = hit.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);
    }
}

to sort

One or more sorts can be added to the field, and it is supported to add on types such as keyword, date, float, etc. It is not allowed to add sorts to fields of text type.

Send POST to http://localhost:9200/xc_course/doc/_search

Filter the documents in the price range of 0-100 yuan, and sort the results, first in descending order of studymodel, then in ascending order of price

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

Java Client



Highlight

Highlighting can highlight one or more words in the search results, so as to show the user the location of matching keywords.

This can be achieved by adding highlight to the search statement, as follows:

{
    
    
	"_source": ["name", "studymodel", "description", "price"],
	"query": {
    
    
		"bool": {
    
    
			"must": [{
    
    
				"multi_match": {
    
    
					"query": "开发框架",
					"minimum_should_match": "50%",
					"fields": ["name^10", "description"],
					"type": "best_fields"
				}
			}],
			"filter": [{
    
    
				"range": {
    
    
					"price": {
    
    
						"gte": 0,
						"lte": 100
					}
				}
			}]
		}
	},
	"sort": [{
    
    
		"price": "asc"
	}],
	"highlight": {
    
    
		"pre_tags": ["<tag>"],
		"post_tags": ["</tag>"],
		"fields": {
    
    
			"name": {
    
    },
			"description": {
    
    }
		}
	}
}

Java Client

@Test
public void testHighlight() throws IOException {
    
    
    SearchRequest searchRequest = new SearchRequest("xc_course");
    searchRequest.types("doc");
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    //source源字段过虑
    searchSourceBuilder.fetchSource(new String[]{
    
    "name","studymodel","price","description"},
    new String[]{
    
    });
    searchRequest.source(searchSourceBuilder);
    //匹配关键字
    MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery("开发",
    "name", "description");
    searchSourceBuilder.query(multiMatchQueryBuilder);
    //布尔查询
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    boolQueryBuilder.must(searchSourceBuilder.query());
    //过虑
    boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(0).lte(100));
    //排序
    searchSourceBuilder.sort(new FieldSortBuilder("studymodel").order(SortOrder.DESC));
    searchSourceBuilder.sort(new FieldSortBuilder("price").order(SortOrder.ASC));
    //高亮设置
    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);
    SearchResponse searchResponse = client.search(searchRequest);
    SearchHits hits = searchResponse.getHits();
    SearchHit[] searchHits = hits.getHits();
    for (SearchHit hit : searchHits) {
    
    
        Map<String, Object> sourceAsMap = hit.getSourceAsMap();
        //名称
        String name = (String) sourceAsMap.get("name");
        //取出高亮字段内容
        Map<String, HighlightField> highlightFields = hit.getHighlightFields();
        if(highlightFields!=null){
    
    
            HighlightField nameField = highlightFields.get("name");
            if(nameField!=null){
    
    
                Text[] fragments = nameField.getFragments();
                StringBuffer stringBuffer = new StringBuffer();
                for (Text str : fragments) {
    
    
                    stringBuffer.append(str.string());
                } 
                name = stringBuffer.toString();
            }
        } 
        String index = hit.getIndex();
        String type = hit.getType();
        String id = hit.getId();
        float score = hit.getScore();
        String sourceAsString = hit.getSourceAsString();
        String studymodel = (String) sourceAsMap.get("studymodel");
        String description = (String) sourceAsMap.get("description");
        System.out.println(name);
        System.out.println(studymodel);
        System.out.println(description);
    }
}

Guess you like

Origin blog.csdn.net/weixin_43650254/article/details/100902304