s137
s137

Reputation: 41

ElasticSearch - Match all search tokens in one or any fields in any order

I'm trying to write an elasticsearch (v7.17) query that returns all documents where all tokens in my search term match in one or any number of fields in any order.

I can do this per field, using a multi_match query with the operator and, but I want to do it for any number of fields, for example if there are 3 fields: text1, text2 and number1 and they are filled for two documents as such:

text1: 'Car', text2: 'Audi', number1: 'A6'

text1: 'Car Audi', text2: '' number1: 'Q7',

I want to find all two when I search "Car Audi", but only the first when I search "Car Audi A6" and none when I search "Car Audi A8".

How can I accomplish this without all just writing them into the same field (which I neither want nor can because they partly have different analyzers in the mapping)?

It's important to note that the mapping is a little complex and the fields have different analyzers so that the search token "Audi" in the field text will match also "Audi-123" for example while in the field number1 that's not the case.

Thanks in advance for your help.

Upvotes: 0

Views: 2195

Answers (1)

Sagar Patel
Sagar Patel

Reputation: 5486

You can achive same using multi_match query only. here, you need to set operator value as and and type value as cross_fields. Please see this documentation.

Query:

POST test/_search
{
  "query": {
    "multi_match": {
      "query": "Car Audi A6",
      "fields": [
        "text1",
        "text2",
        "number1"
      ],
      "type": "cross_fields", 
      "operator": "and"
    }
  }
}

Upvotes: 3

Related Questions