Elasticsearch

Search engine _filter and Query in depth decryption: relevance, performance

Filter and Query examples

 Add some data for testing
 PUT /waws_company/employee/1
 {
   "address": {
     "country": "china"."province": "jiangsu"."city": "nanjing"
   },
   "name": "tom"."age": 30."join_date": "2016-01-01"
 }
 ​
 PUT /waws_company/employee/2
 {
   "address": {
     "country": "china"."province": "shanxi"."city": "xian"
   },
   "name": "marry"."age": 35."join_date": "2015-01-01"
 }
 ​
 PUT /waws_company/employee/3
 {
   "address": {
     "country": "china"."province": "jiangsu"."city": "nanjing"
   },
   "name": "waws"."age": 37."join_date": "2016-01-01"
 }
Copy the code

Search request: The age must be greater than or equal to 31, and join_date must be 2016-01-01

 GET /waws_company/employee/_search
 {
   "query": {
     "bool": {
       "must": [{"match": {
             "join_date": "2016-01-01"}}]."filter": {    #filter is also in bool
         "range": {
           "age": {
             "gte": 31}}}}}} {"took": 37."timed_out": false,
   "_shards": {
     "total": 5."successful": 5."failed": 0
   },
   "hits": {
     "total": 1."max_score": 1."hits": [{"_index": "waws_company"."_type": "employee"."_id": "3"."_score": 1."_source": {
           "address": {
             "country": "china"."province": "jiangsu"."city": "nanjing"
           },
           "name": "waws"."age": 37."join_date": "2016-01-01"}}]}}Copy the code

Filter and Query are compared for decryption

  • Filter only filters the required data according to the search criteria. It does not calculate any correlation score and has no effect on the correlation
  • Query, calculates the relevance of each document relative to the search criteria and sorts it by relevance

In general, if you are performing a search and need to return the data that best matches the search criteria, use query; If you just want to filter out some data based on some criteria, and you don’t care about sorting, then you use filter unless it’s your search criteria, and you want the documents that match those criteria to be returned first, then those search criteria should be in query; If you don’t want some search criteria to affect your document sorting, just put it in filter

Filter and Query performance

  • Filter, does not need to calculate the correlation score, does not need to sort by the correlation score, and also has the built-in automatic cache most commonly used filter data
  • Query, instead, calculates the correlation score, sorts it by score, and does not cache the results

Elasticsearch (50)

Query search syntax (query search syntax)Focus on)

  • match all
 GET /_search
 {
     "query": {
         "match_all": {}}}Copy the code
  • match
 GET /_search
 {
     "query": { "match": { "title": "my elasticsearch article"}}}Copy the code
  • multi match
 GET /waws_index/waws_type/_search
 {
   "query": {
     "multi_match": {
       "query": "test"."fields": ["test_field"."test_field1"]}}}Copy the code
  • range query
 GET /waws_index/waws_type/_search 
 {
   "query": {
     "range": {
       "age": {
         "gte": 30}}}}Copy the code
  • term query
 GET /waws_index/waws_type/_search 
 {
   "query": {
     "term": {
       "test_field": "test hello"  # treat field as an entire string}}}Copy the code
  • terms query
 GET /_search
 {
     "query": { "terms": { "tag": [ "search"."full_text"."nosql"]}}}Copy the code
  • Exist Query (query in 2.x, no longer available)

Elasticsearch (51)

First knowledge of the search engine _ on the machine hands-on practice of multiple search conditions combination query

Combination query

  • Routine 1
 GET /waws_web/waws_article/_search
 {
   "query": {
     "bool": {
       "must": [{"match": {
             "title": "elasticsearch"}}]."should": [{"match": {
             "content": "elasticsearch"}}]."must_not": [{"match": {
             "author_id": 111}}]}}}Copy the code
  • Routine 2
 GET /waws_web/waws_article/_search
 {
     "bool": {
         "must":     { "match": { "title": "how to make millions" }},
         "must_not": { "match": { "tag":   "spam" }},
         "should": [{"match": { "tag": "starred"}}]."filter": {
           "range": { "date": { "gte": "2014-01-01"}}}}}Copy the code
  • bool
    • must
    • must_not
    • should
    • filter
  • Each subquery computes a document’s relevance score against it, and thenBool Synthesizes all scores, combined into a fraction, of course, filter does not calculate the fraction

  • Routine 3

 GET /waws_web/waws_article/_search
 {
     "bool": {
         "must":     { "match": { "title": "how to make millions" }},
         "must_not": { "match": { "tag":   "spam" }},
         "should": [{"match": { "tag": "starred"}}]."filter": {
           "bool": { 
               "must": [{"range": { "date": { "gte": "2014-01-01" }}},
                   { "range": { "price": { "lte": 29.99}}}]."must_not": [{"term": { "category": "ebooks"}}]}}}}Copy the code
  • Routine 4
 GET /company/employee/_search 
 {
   "query": {
     "constant_score": {
       "filter": {
         "range": {
           "age": {
             "gte": 30
           }
         }
       }
     }
   }
 }
Copy the code