Exact multi term match

I'm stuck doing searches in Elasticsearch. I'm trying to find a single result that matches several terms. It seems simple but I can't find the right way to do it.

To search with a single term I'm launching the following search:

{
    "query":
        "term": {
            "name. keyword."
                "value":"Will"
            }
        }
    }
}

And it works great.

To search with more than one term I have tried many things like the following which makes the most sense in my head:

{
    "query":
        "term":[{{
            "name. keyword."
                "value":"Will"
            }
        }, {
            "lastname. keyword."
                "value":"Smith"
            }
        }]
    }
}

But it returns an error. In the best case, I have managed to get all the Will's and all the Smith's back, among them "Will Smith", who is the one I was looking for but I would like to get rid of all those who do not meet all the requirements.

My mappings are the following:

{
    "dataset": {
        "mappings": {
            "data": {
                "properties": {
                    "name": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    },
                    "lastname": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    }
                }
            }
        }
    }
}

Is it possible to get what I'm trying to do?

Upvotes: 0

Views: 159

Answers (1)

Eli
Eli

Reputation: 4956

Try Bool Query:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "name.keyword": "Will"
          }
        },
        {
          "term": {
            "lastname.keyword": "Smith"
          }
        }
      ]
    }
  }
}

Upvotes: 1

Related Questions