A simple AND query with Elasticsearch

后端 未结 3 2045
执笔经年
执笔经年 2021-02-07 06:17

I am trying to do a simple query for two specified fields, and the manual and google is proving to be of little help. Example below should make it pretty clear what I want to do

3条回答
  •  星月不相逢
    2021-02-07 06:48

    Edit: Updated, sorry. You need a separate Term object for each field, inside of a Bool query:

    {
      "query": {
        "bool": {
            "must" : [
              {
               "term": {
                 "name.family_name": "daniel"
               }
             },
             {
               "term": {
                 "name.given_name": "tyrone"
               }
             }
           ]
         }
       }
    }
    

    Term queries are not analyzed by ElasticSearch, which makes them case sensitive. A Term query says to ES "look for this exact token inside your index, including case and punctuation".

    If you want case insensitivity, you could add a keyword + lowercase filter to your analyzer. Alternatively, you could use a query that analyzes your text at query time (like a Match query)

    Edit2: You could also use And or Bool filters too.

提交回复
热议问题