2.1,term&termsThe query

2.1.1 Term Query

Reference: Elasticsearch Reference [7.10] » Term-level Queries » Term Query Term = ‘Compliable’ and ‘Compliable’ It will not be divided into hand and machine; Then according to the keywords to the document word segmentation database to match the content. Where type =? Where type =?

2.1.1.1, commands,

# from size limit POST/SMS-logs-index /_search {"from": 0, "size": 10, "query": {"term": {" type ": {"value": "Beijing"}}}}

2.1.1.2. Java code

@Test public void termQuery() throws IOException {//1. SearchRequest object searchRequest object mySearchRequest = new SearchRequest(index); SearchSourceBuilder = new SearchSourceBuilder(); SearchSourceBuilder = new SearchSourceBuilder(); Builder. from(0).size(10).query(queryBuilders. TermQuery (" type ", "Beijing ")); MySearchRequest. source(Builder); SearchResponse Search = Client.search (mySearchRequest, requestOptions. Default); GetHits () for (searchHit) getHits() for (searchHit) getHits() for (searchHit) search.getHits().getHits()) { Map<String, Object> result = hit.getSourceAsMap(); System.out.println(result); }}

2.1.2. Terms query

The query mechanism of terms and terms is the same, neither of them will divide the specified query keywords into words, but directly match in the word segmentation database to find the corresponding document content. Terms: is used when a field contains multiple values. In other words:

  • Term is similar to MySQL where type =?
  • Terms is similar to where type in (? ,? ,?)

Note: Term and terms are just say not to participle of keyword, is not to say that can only be used in the field the query keyword types, such as the assumption is a field in a document type text, use the ik points, the value is the struggle of age, through online ik participles, we know that breaks down into struggle, courageously, bucket, era, as shown in figure 4, But if you use the term or terms of the field query, the input keyword is the era of struggle, because the input keyword will not divide the word, but can not find the record, if you input the keyword is struggle is OK.

2.1.2.1, commands,

POST/SMS - logs - index / _search {" from ": 0," size ": 20," query ": {" terms" : {" province ": [" Beijing", "Shanghai", "hangzhou"]}}}

2.1.2.2 Java code

@Test public void termSquery () throws IOException {//1. SearchRequest object searchRequest object mySearchRequest = new SearchRequest(index); SearchSourceBuilder = new SearchSourceBuilder(); SearchSourceBuilder = new SearchSourceBuilder(); Builder. from(0).size(10).query(queryBuilders. TermSquery (" Province ", "Beijing "," China ", "Guangzhou ")); MySearchRequest.source (Builder);}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} SearchResponse Search = Client.search (mySearchRequest, requestOptions. Default); GetHits () for (searchHit) getHits() for (searchHit) getHits() for (searchHit) search.getHits().getHits()) { Map<String, Object> result = hit.getSourceAsMap(); System.out.println(result); }}