1, Prepare
1. Import dependency
<!--springboot <=2.2.5 Need to specify es Version default import es Version 6.x--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
2. Test data
PUT /yuangong { "settings": {}, "mappings": { "properties": { "name": { "type": "text", "analyzer": "ik_max_word" }, "alias": { "type": "text" }, "age": { "type": "integer" }, "sex": { "type": "keyword" }, "phone": { "type": "text" }, "title": { "type": "text", "analyzer": "ik_max_word" }, "slogan": { "type": "text" } } } } POST /yuangong/_doc/ { "name": "Zhang Sansan", "alias": "Xiao Zhang", "age": 28, "sex": "male", "phone": 183xxxx0000, "title": "primary Java development", "slogan": "If you become riches and honour one day, don't forget me." } POST /yuangong/_doc/ { "name": "Li Si", "alias": "Xiao Si", "age": 25, "sex": "male", "phone": 183xxxx0001, "title": "senior Java development", "slogan": "The road is difficult, the road is difficult, and there are many different roads. I'm safe today" } POST /yuangong/_doc/ { "name": "Wang Wu", "alias": "Five brothers", "age": 30, "sex": "male", "phone": 183xxxxx0002, "title": "senior Java development", "slogan": "It's better to start at once" } POST /yuangong/_doc/ { "name": "Wang Liu", "alias": "Name and", "age": 28, "sex": "male", "phone": 183xxxx0003, "title": "Advanced front end development", "slogan": "Surpass yourself" } POST /yuangong/_doc/ { "name": "Son of a bitch", "alias": "son of a bitch", "age": 31, "sex": "male", "phone": 183xxxx0004, "title": "Senior Product Manager", "slogan": "be worldly-wise and play safe" } POST /yuangong/_doc/ { "name": "Wang Mazi", "alias": "Xiaoyaozi", "age": 30, "sex": "female", "phone": 183xxxx0005, "title": "Senior business expert", "slogan": "I don't know if Peng is thousands of miles away" } POST /yuangong/_doc/ { "name": "Tuesday", "alias": "Erzi", "age": 22, "sex": "male", "phone": 18300000006, "title": "Primary test development", "slogan": "The cold ice rain slapped on my face" }
2, Full text search
2.1. Matching search (word splitting)
Related to data type
(1) If the queried field is keywork, it can only be matched in full and will not be split
(2) If the query field type is text, the query value will be segmented according to the word splitter of the query field
For example, if the word splitter is not set, Zhang Sansan will be split into Zhang, San and San, and the data containing [Zhang] or [San] or [San] will be matched
For example, if you set the IK word breaker,
"name": {"type": "text","analyzer": "ik_max_word"}
Zhang Sansan will be split into Zhang Sansan and San Sansan, and the data containing [Zhang Sansan] or [San Sansan] will be matched
For example, if the IK word splitter is set, three three will be split into three three, and the data containing [three three] will be matched
(3) The default operator OR is used above. When the operator is set to AND
For example, if the word splitter is not set, Zhang Sansan will be split into Zhang, San and San, and the data containing [Zhang] and [San] and [San] will be matched
For example, if the IK word splitter is set, Zhang Sansan will be split into Zhang Sansan and San Sansan, and the data containing [Zhang Sansan] and [San San] will be matched
For example, if the IK word splitter is set, three three will be split into three three, and the data containing [three three] will be matched
public void matchQuery() throws IOException { // Search request object SearchRequest searchRequest = new SearchRequest("yuangong"); // Search source build object SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "three three").operator(Operator.OR); doSearch(searchRequest,searchSourceBuilder,matchQueryBuilder); }
2.2. Phrase search
public void matchPhraseQuery() throws IOException { // Search request object SearchRequest searchRequest = new SearchRequest("yuangong"); // Search source build object SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); MatchPhraseQueryBuilder matchPhraseQueryBuilder = QueryBuilders.matchPhraseQuery("title", "JAVA development").slop(1); doSearch(searchRequest,searchSourceBuilder,matchPhraseQueryBuilder); }
2.3. queryString search
You can query without specifying a field
public void queryString() throws IOException { // Search request object SearchRequest searchRequest = new SearchRequest("yuangong"); // Search source build object SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); // QueryStringQueryBuilder queryStringQueryBuilder = QueryBuilders.queryStringQuery("no"); QueryStringQueryBuilder queryStringQueryBuilder = QueryBuilders.queryStringQuery("no").field("slogan"); doSearch(searchRequest,searchSourceBuilder,queryStringQueryBuilder); }
2.4. Multi field matching search
public void multiMatchQuery() throws IOException { // Search request object SearchRequest searchRequest = new SearchRequest("yuangong"); // Search source build object SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); MultiMatchQueryBuilder multiMatchQuery = QueryBuilders.multiMatchQuery("bright","alias","slogan"); doSearch(searchRequest,searchSourceBuilder,multiMatchQuery); }
3, Term level search
3.1. Entry level search
Similar to like in SQL
① , is related to the data type of the field. If the data type is text but no word breaker is specified
For example, if Zhang Sansan does not specify a word splitter, the word will be split into Zhang, San and San
If the value in your termQuery is Zhang San, San San, or Zhang San, you can't find it; Only Zhang or three can be found
② , is related to the data type of the field, if the data type is text but IK word breaker is specified
For example, Zhang Sansan specifies the IK word splitter, and this word will be split into Zhang Sansan and San Sansan
If the value in termQuery is Zhang, San, or Zhang, San, you can't find it; Only Zhang San or San San can be searched
③ , is related to the data type of the field. If the data type is keyword
Search can only use Zhang Sansan as a search
public void termQuery() throws IOException { // Search request object SearchRequest searchRequest = new SearchRequest("yuangong"); // Search source build object SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); TermQueryBuilder termQuery = QueryBuilders.termQuery("name","Zhang Sansan"); doSearch(searchRequest,searchSourceBuilder,termQuery); }
3.2. Terms query
Type and in in SQL
public void termsQuery() throws IOException { // Search request object SearchRequest searchRequest = new SearchRequest("yuangong"); // Search source build object SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); TermsQueryBuilder termsQuery = QueryBuilders.termsQuery("slogan","no","Peng"); doSearch(searchRequest,searchSourceBuilder,termsQuery); }
3.3. Range query
/** * Greater than or equal to a value * @param from Compared value * @param includeLower Include this value * @return */ public RangeQueryBuilder from(Object from, boolean includeLower){} /** * Less than or equal to a value * @param to Compared value * @param includeLower Include this value * @return */ public RangeQueryBuilder to(Object to, boolean includeLower){}
3.4. Not null search (exists query)
Equivalent to column is not null in SQL
public void existsQuery() throws IOException { // Search request object SearchRequest searchRequest = new SearchRequest("yuangong"); // Search source build object SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); ExistsQueryBuilder existsQuery = QueryBuilders.existsQuery("sex"); doSearch(searchRequest,searchSourceBuilder,existsQuery); }
3.5. Prefix query
Note that prefix query will be linked with the word breaker
① . if it is a keyword, you can prefix it at will
② . if a word splitter is set, the first word divided by the word splitter will be used as the prefix.
If the word splitter is not set, the default word splitter will be used. The word Zhang San will be divided into Zhang, San and San. At this time, the prefix can only check Zhang. At this time, the search for Zhang San or Zhang San is invalid
If the word splitter is set to IK, the word Zhang San will be cut into Zhang San and San San. At this time, the prefix can only check Zhang San. At this time, the search for Zhang San is invalid
public void prefixQuery() throws IOException { // Search request object SearchRequest searchRequest = new SearchRequest("yuangong"); // Search source build object SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); PrefixQueryBuilder prefixQuery = QueryBuilders.prefixQuery("title","senior"); doSearch(searchRequest,searchSourceBuilder,prefixQuery); }
3.6. Wildcard query
public void wildcardQuery() throws IOException { // Search request object SearchRequest searchRequest = new SearchRequest("yuangong"); // Search source build object SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); WildcardQueryBuilder wildcardQuery = QueryBuilders.wildcardQuery("name","Zhang*three").boost(2); doSearch(searchRequest,searchSourceBuilder,wildcardQuery); }
3.7. Regular search
public void regexpQuery() throws IOException { // Search request object SearchRequest searchRequest = new SearchRequest("yuangong"); // Search source build object SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); RegexpQueryBuilder regexpQueryBuilder = QueryBuilders.regexpQuery("title","senior.*"); doSearch(searchRequest,searchSourceBuilder,regexpQueryBuilder); }
3.8. Fuzzy query
public void fuzzyQuery() throws IOException { // Search request object SearchRequest searchRequest = new SearchRequest("yuangong"); // Search source build object SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyQuery("title","senior"); doSearch(searchRequest,searchSourceBuilder,fuzzyQueryBuilder); }
3.9. ids search (id set query) (ids query)
public void idsQuery() throws IOException { // Search request object SearchRequest searchRequest = new SearchRequest("yuangong"); // Search source build object SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); IdsQueryBuilder idsQueryBuilder = QueryBuilders.idsQuery().addIds("t1UY3nsB1bu3ZtXKDENc"); doSearch(searchRequest,searchSourceBuilder,idsQueryBuilder); }
4, Compound query
4.1. Compound query
public void boolQuery() throws IOException { // Search request object SearchRequest searchRequest = new SearchRequest("yuangong"); // Search source build object SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); MatchQueryBuilder matchTitleQueryBuilder = QueryBuilders.matchQuery("title", "senior"); RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").from(25,false).to(30); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().must(matchTitleQueryBuilder).mustNot(rangeQueryBuilder); doSearch(searchRequest,searchSourceBuilder,boolQueryBuilder); }
4.2. Sorting
public void sortQuery() throws IOException { // Search request object SearchRequest searchRequest = new SearchRequest("yuangong"); // Search source build object SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); MatchQueryBuilder matchTitleQueryBuilder = QueryBuilders.matchQuery("title", "senior"); RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").from(25,false).to(30); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().must(matchTitleQueryBuilder).mustNot(rangeQueryBuilder); doSearch(searchRequest,searchSourceBuilder,boolQueryBuilder); } public void doSearch(SearchRequest searchRequest, SearchSourceBuilder searchSourceBuilder, QueryBuilder query) throws IOException { // sort,text fields cannot be used for sorting FieldSortBuilder age = SortBuilders.fieldSort("age").order(SortOrder.ASC); searchSourceBuilder.query(query).sort(age); // Set the search source to the search request object searchRequest.source(searchSourceBuilder); // Perform a search and initiate an http request to ES SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); // search result SearchHits hits = searchResponse.getHits(); System.out.println(Arrays.toString(hits.getHits())); }
5, Word splitter
Check how the word to be queried is segmented
5.1. Do not set word splitter, use default word splitter
# Do not set a word breaker, use the default word breaker POST _analyze { "text": "senior Java development" }
Word segmentation result:
{ "tokens" : [ { "token" : "Endowment", "start_offset" : 0, "end_offset" : 1, "type" : "<IDEOGRAPHIC>", "position" : 0 }, { "token" : "deep", "start_offset" : 1, "end_offset" : 2, "type" : "<IDEOGRAPHIC>", "position" : 1 }, { "token" : "java", "start_offset" : 2, "end_offset" : 6, "type" : "<ALPHANUM>", "position" : 2 }, { "token" : "open", "start_offset" : 6, "end_offset" : 7, "type" : "<IDEOGRAPHIC>", "position" : 3 }, { "token" : "hair", "start_offset" : 7, "end_offset" : 8, "type" : "<IDEOGRAPHIC>", "position" : 4 } ] }
5.2. Specify the word breaker and use IK maximum word segmentation
# Specifies the word breaker, using IK maximum word segmentation POST _analyze { "analyzer": "ik_max_word", "text": "Nanjing Yangtze River Bridge" }
Word segmentation result:
{ "tokens" : [ { "token" : "Nanjing City", "start_offset" : 0, "end_offset" : 3, "type" : "CN_WORD", "position" : 0 }, { "token" : "Nanjing", "start_offset" : 0, "end_offset" : 2, "type" : "CN_WORD", "position" : 1 }, { "token" : "mayor", "start_offset" : 2, "end_offset" : 4, "type" : "CN_WORD", "position" : 2 }, { "token" : "Yangtze River Bridge", "start_offset" : 3, "end_offset" : 7, "type" : "CN_WORD", "position" : 3 }, { "token" : "Yangtze River", "start_offset" : 3, "end_offset" : 5, "type" : "CN_WORD", "position" : 4 }, { "token" : "Bridge", "start_offset" : 5, "end_offset" : 7, "type" : "CN_WORD", "position" : 5 } ] }
5.3. Specify the word breaker and use IK minimum word segmentation
# Specifies the word breaker, using IK minimum word segmentation POST _analyze { "text": "Nanjing Yangtze River Bridge", "analyzer": "ik_smart" }
Word segmentation result:
{ "tokens" : [ { "token" : "Nanjing City", "start_offset" : 0, "end_offset" : 3, "type" : "CN_WORD", "position" : 0 }, { "token" : "Yangtze River Bridge", "start_offset" : 3, "end_offset" : 7, "type" : "CN_WORD", "position" : 1 } ] }