ES-3-Advanced Query

Elasticsearch Provided based on JSON Provide a complete query DSL To define the query Define data # POST /student/_d...
Elasticsearch Provided based on JSON Provide a complete query DSL To define the query Define data
# POST /student/_doc/1001 { "name":"zhangsan", "nickname":"zhangsan", "sex":"male", "age":30 } # POST /student/_doc/1002 { "name":"lisi", "nickname":"lisi", "sex":"male", "age":20 } # POST /student/_doc/1003 { "name":"wangwu", "nickname":"wangwu", "sex":"female", "age":40 } # POST /student/_doc/1004 { "name":"zhangsan1", "nickname":"zhangsan1", "sex":"female", "age":50 } # POST /student/_doc/1005 { "name":"zhangsan2", "nickname":"zhangsan2", "sex":"female", "age":30 }
Query all documents stay Postman Medium, to ES Server send GET Request: http://127.0.0.1:9200/student/_search
{ "query": { "match_all": {} } } # "Query": query here represents a query object, which can have different query attributes # "Match_all": Query type, for example: match_all (for all queries), match, term, range, etc. # : Query criteria vary depending on the type and the way they are written
The server response results are as follows:
{ "took[Query takes time in milliseconds)" : 1116, "timed_out[Timeout or not)" : false, "_shards[Fragmentation Information)" : { "total[Total)" : 1, "successful[Success)" : 1, "skipped[Ignore)" : 0, "failed[Failed)" : 0 }, "hits[Search Hit Results)" : { "total"[Total number of documents with matching search criteria): { "value"[Total Hit Count Value): 3, "relation"[Count Rule): "eq" # eq means the count is accurate, gte means the count is not accurate }, "max_score[Matching Score]" : 1.0, "hits[Hit Result Set)" : [ . . . } ] } }
Match Query match Match type query, which divides the query criteria into words, and then queries, with multiple entries between or Relationships stay Postman Medium, to ES Server send GET Request: http://127.0.0.1:9200/student/_search
{ "query": { "match": { "name":"zhangsan" } } }
Field Matching Query multi_match and match Similarly, the difference is that it can be queried in multiple fields. stay Postman Medium, to ES Server send GET Request: http://127.0.0.1:9200/student/_search
{ "query": { "multi_match": { "query": "zhangsan", "fields": ["name","nickname"] } } }
Keyword Exact Query term Query, exact keyword matching query, no word segmentation for query conditions. stay Postman Medium, to ES Server send GET Request: http://127.0.0.1:9200/student/_search
{ "query": { "term": { "name": { "value": "zhangsan" } } } }
Multi-keyword exact query terms Query and term The query is the same, but it allows you to specify multiple values to match. If the field contains any of the specified values, the document satisfies the criteria, similar to mysql Of in stay Postman Medium, to ES Server send GET Request: http://127.0.0.1:9200/student/_search
{ "query": { "terms": { "name": ["zhangsan","lisi"] } } }
Specify Query Fields By default, Elasticsearch In the results of the search, the document will be saved in _source All fields are returned. If we only want to get some of the fields, we can add _source Filtering stay Postman Medium, to ES Server send GET Request: http://127.0.0.1:9200/student/_search
{ "_source": ["name","nickname"], "query": { "terms": { "nickname": ["zhangsan"] } } }
Filter Fields We can also: includes : to specify the fields you want to display excludes : to specify fields that you do not want to display stay Postman Medium, to ES Server send GET Request: http://127.0.0.1:9200/student/_search
"_source": { "includes": ["name","nickname"] }, "query": { "terms": { "nickname": ["zhangsan"] } } }
stay Postman Medium, to ES Server send GET Request: http://127.0.0.1:9200/student/_search
{ "_source": { "excludes": ["name","nickname"] }, "query": { "terms": { "nickname": ["zhangsan"] } } }
Combinatorial Query `bool` Pass various other queries through `must` (required) `must_not` (Must not), `should` (should) party Type Combine stay Postman Medium, to ES Server send GET Request: http://127.0.0.1:9200/student/_search
{ "query": { "bool": { "must": [ { "match": { "name": "zhangsan" } } ], "must_not": [ { "match": { "age": "40" } } ], "should": [ { "match": { "sex": "male" } } ] } } }
Range Query range The query finds the numbers or times that fall within the specified interval. range Query allows the following characters stay Postman Medium, to ES Server send GET Request: http://127.0.0.1:9200/student/_search
{ "query": { "range": { "age": { "gte": 30, "lte": 35 } } } }
Fuzzy Query Returns a document containing words similar to search terms. Edit distance is the number of character changes required to convert one term into another. These changes can include: Change characters ( box → fox ) Delete characters ( black → lack ) Insert characters ( sic → sick ) Transpose two adjacent characters ( act → cat ) To find similar terms, fuzzy Queries create all possible variants of a set of search terms within a specified edit distance Or extension. The query then returns the exact match for each extension. adopt fuzziness Modify the edit distance. Default values are generally used AUTO To generate an edit distance based on the length of the term. stay Postman Medium, to ES Server send GET Request: http://127.0.0.1:9200/student/_search
{ "query": { "fuzzy": { "title": { "value": "zhangsan" } } } }
stay Postman Medium, to ES Server send GET Request: http://127.0.0.1:9200/student/_search
{ "query": { "fuzzy": { "title": { "value": "zhangsan", "fuzziness": 2 } } } }
Single Field Sorting sort Let's sort by different fields and pass order Specify how to sort. desc Descending order, asc Ascending order. stay Postman Medium, to ES Server send GET Request: http://127.0.0.1:9200/student/_search
{ "query": { "match": { "name":"zhangsan" } }, "sort": [{ "age": { "order":"desc" } }] }
Multi-field Sorting Suppose we want to combine age and _score Queried and matched results are first sorted by age, then Sort by relevance score stay Postman Medium, to ES Server send GET Request: http://127.0.0.1:9200/student/_search
{ "query": { "match_all": {} }, "sort": [ { "age": { "order": "desc" } }, { "_score":{ "order": "desc" } } ] }
Highlight Query When searching for keywords, the keywords in the searched content show different colors, called highlights. Elasticsearch You can label and style the keyword part of the query content ( Highlight ) Settings for. in use match Add one at the same time as the query highlight attribute pre_tags : Pre-label post_tags : Postlabel fields : Fields that need to be highlighted title : Statement here title The field needs to be highlighted, and you can set a specific configuration for the field later, or you can leave it empty stay Postman Medium, to ES Server send GET Request: http://127.0.0.1:9200/student/_search
{ "query": { "match": { "name": "zhangsan" } }, "highlight": { "pre_tags": "<font color='red'>", "post_tags": "</font>", "fields": { "name": {} } } }
Paging Query from : Start index of current page, default from 0 Start. from = (pageNum - 1) * size size : How many bars are displayed per page stay Postman Medium, to ES Server send GET Request: http://127.0.0.1:9200/student/_search
{ "query": { "match_all": {} }, "sort": [ { "age": { "order": "desc" } } ], "from": 0, "size": 2 }
Aggregate queries Aggregation allows users to pair es Documents are statistically analyzed, similar to those in relational databases group by Of course, there are many more Many other aggregates, such as maximizing, averaging, and so on. Maximize a field max stay Postman Medium, to ES Server send GET Request: http://127.0.0.1:9200/student/_search
{ "aggs":{ "max_age":{ "max":{"field":"age"} } }, "size":0 }
Minimize a field min stay Postman Medium, to ES Server send GET Request: http://127.0.0.1:9200/student/_search
{ "aggs":{ "min_age":{ "min":{"field":"age"} } }, "size":0 }
Summing up a field sum stay Postman Medium, to ES Server send GET Request: http://127.0.0.1:9200/student/_search
{ "aggs":{ "sum_age":{ "sum":{"field":"age"} } }, "size":0 }
Average a field avg stay Postman Medium, to ES Server send GET Request: http://127.0.0.1:9200/student/_search
{ "aggs":{ "avg_age":{ "avg":{"field":"age"} } }, "size":0 }
Remove the value of a field before taking the total number stay Postman Medium, to ES Server send GET Request: http://127.0.0.1:9200/student/_search
{ "aggs":{ "distinct_age":{ "cardinality":{"field":"age"} } }, "size":0 }
State polymerization stats Aggregate, return one time to a field count , max , min , avg and sum Five Indicators stay Postman Medium, to ES Server send GET Request: http://127.0.0.1:9200/student/_search
{ "aggs":{ "stats_age":{ "stats":{"field":"age"} } }, "size":0 }
Bucket Aggregate Query Bucket and equivalent to sql In group by Statement' terms Aggregation, Grouping Statistics stay Postman Medium, to ES Server send GET Request: http://127.0.0.1:9200/student/_search
{ "aggs":{ "age_groupby":{ "terms":{"field":"age"} } }, "size":0 }
stay terms Aggregate again under grouping stay Postman Medium, to ES Server send GET Request: http://127.0.0.1:9200/student/_search
{ "aggs":{ "age_groupby":{ "terms":{"field":"age"} } }, "size":0 }

13 September 2021, 12:52 | Views: 3407

Add new comment

For adding a comment, please log in
or create account

0 comments