DSL filter query

1. What is DSL inquiry 

  Provide a rich and flexible query language, called by the ES DSL query (Query DSL), which allows you to build a more complex , powerful queries.

  The DSL (domain-specific language the Domain Specific Language) in JSON form bodies request occurs.  

  DSL is a generic query query ES offers, the biggest feature of this approach is the development language of independence , that is, any client as long as the support HTTP requests, you can JSON format to perform complex search query data.

  

  For simple query using a query string is better, but for complex queries , because conditions and more complicated nested logical, easy organization and expression of the query string, and error-prone, it is recommended complex queries by DSL request body content in JSON format instead.

 

  DSL has two components: DSL query , DSL filter .

  DSL DSL filter statements and queries are very similar, but their purpose is different:

  DSL filter (pinpoint) query document is more like a way for my condition "yes" or "no", while DSL queries (fuzzy query) is like "How like."

  

  DSL DSL filters and queries on the performance difference :

      The results can be cached by filtration and applied to subsequent requests.

      The documents that match the query, the correlation is calculated, it is more time-consuming, and does not cache.

     Filter statement can effectively meet the query is completed document filtering.

  In principle, the use of DSL queries do full-text searches or other need for relevance score of the scene, the other all-filtered with DSL.

 

2, DSL inquiry 

  Use DSL query, you must query parameters to be passed to the ES.

  GET _search

  {"query": YOUR_QUERY_HERE}

 

  A common DSL relatively complete query:

GET itsource/employee/_search

  {

  "query": {

     "match": {"sex":"女"}

  },

   "_source": ["id","name"],

  "from": 20,

  "size": 10,

  "sort": [{"join_date": "desc"},{"age": "asc"}]

  }

 

  select id,name from t_user where name like “%heh%” order by id desc limit 0,10.

  DSL on behalf of the above query: Discover employees gender woman staff, and in accordance Joined descending, ascending order of age, eventually returned Article 21-30 data (return only name, age and email fields)

3, DSL filter 

  Fuzzy query query with DSL and precise query filter statement with DSL.

2.0以上的用法
{   
"query": {   "bool": { "must": [         {"match": {"description": "search" }}       ], "filter": { "term": {"age": "12"} }   }   },   "_source": ["id","name"],   "from": 20,   "size": 10,   "sort": [{"join_date": "desc"},{"age": "asc"}] }

 

4, using DSL filter query

  ① full match (match_all)

  Ordinary search (match all documents):

{
  "query" : {
    "match_all" : {}
  }
}

  If you need to use filters (filter in all documents, not the red part of the default write):

{
  "query" : {
    "bool" : {
      "must" : [{
        "match_all":{}
      }],
      "filter":{....}
    }
  }
} 

  ② standard query (match and multi_match)

  match the query is a standard query, whether you need full-text search or precise query basically have to use it.

  If you use a full-text query match field, it will use the parser to parse the query character before the actual query:

{
  "query": {
    "match": {
      "fullName": "Steven King"
    }
  }
} 

  Steven King will search the above word and find documents that contain or Steven King, and then give ranking score.

  If the next match with a precise value specified, in the face of numbers, date, boolean, or string not_analyzed, it will search for you the value given to you, such as:

{ "match": { "age": 20 }}

{ "match": { "date": "2016-05-01" }}

{ "match": { "public": true }}

{ "match": { "tag": "full_text" }}

  multi_match query allows you to simultaneously search multiple fields basis do match the query on:

{
  "query":{
    "multi_match": {
      "query": "Steven King",
      "fields": [ "fullName","title" ]
    }
  }
} 

  The above simultaneously search and matching fullName title field.

  Tip: match generally only used to match query text field, generally not used for filtering.

  ③ word search and filter (Term and Terms )

{
  "query": {
    "bool": {
      "must": { 
        "match_all": {} 
      }, 
      "filter": { 
        "term": { 
          "age": "20" 
        } 
      } 
    } 
  }
} 

  Terms search and filter

{
    "query": {
        "terms": {
            "tags": ["jvm", "hadoop", "lucene"],
            "minimum_match": 2
        }
    }
}
        

  minimum_match: at least the number of hits, default 1

  ④ combination condition search and filter (Bool )

  Bool compositions may be combined with the search query to a plurality of query objects, including query criteria must, should and must_not.

  For example: Query interested in eating rice, but also like a game or a sport, and people born in 1990-06-30 and beyond. Range: range

{
  "query": {
    "bool": {
      "must": [{"term": {"hobby": "吃米饭"}}],
      "should": [{"term": {"hobby": "游戏"}}, 
         {"term": {"hobby": "运动"}} 
      ],
      "must_not": [
        {"range" :{"birth_date":{"lt": "1990-06-30"}}} 
      ],
        "filter": [...],
     "minimum_should_match": 1
    }
  }
}

  Tip: If there is no clause must under bool inquiry, then at least there should be a clause should. But if there must clause, clause should not be queried.  

  ⑤ range queries and filters (the Range )

  range filter allows us to find a group of data in accordance with the specified range:

{
  "query":{
    "range": {
      "age": {
        "gte": 20,
        "lt": 30
      }
    }
  }
}

  The example query older than or equal to 20 and less than 30.

  gt:>    gte:>=   lt:<  lte:<=

  

  ⑥ presence and absence of the filter (exists and Missing )

{
  "query": {
    "bool": {
      "must": [{
        "match_all": {}
      }],
      "filter": {
        "exists": { "field": "gps" }
      }
    }
  }
}

  Tip: exists and missing only be used to filter results.

 

  ⑦ before matching search and filter (prefix )

  And similar term query, the search is not an exact match before the match, but similar in SQL like 'key%'

{
  "query": {
    "prefix": {
      "fullName": "黄"
    }
  }
}

  That is the example for all queries surnamed Huang.

 

  ⑧ wildcard search (wildcard )

  * For 0 ~ N, uses at? On behalf of one.

{
  "query": {
    "wildcard": {
      "fullName": "文*华"
    }
  }
}

 

Guess you like

Origin www.cnblogs.com/wanghj-15/p/11310595.html