0
votes

I am creating an index in elasticsearch and i want the ability to search across multiple fields i.e. have those fields be treated as one big search field. I've done some researching a came across 2 different ways to do this:

  1. The first is with cross_fields multi-match query. This allows for searching across multiple fields as one big field with the ability to boost certain fields. But does not allow for fuzziness to be added.

  2. Using copy_to I can copy fields to an 'all' field so that all the searchable terms are in one big field. This allows for fuzzy search but then does not allow me to boost by specific fields

Is there another cross_fields or search option i'm unaware of that will allow for me to fuzzy search as well as boost by a specific field?

1

1 Answers

0
votes

I think you could add fussiness to multi match. But it will be applied to all fields. Find an example below with boost and fuzziness

GET /my_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "bjorn borg schoenen",
            "fields": [
              "title^5.0",
              "brand^2.0"
            ],
            "type": "best_fields",
            "operator": "and",
            "fuzziness": "auto"
          }
        }
      ]
    }
  }
}

If you want to be more granular, you can use a boolean query with should and a minimum should match:

 {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "brand": {
                  "query": "my query",
                  "fuzziness": "auto",
                  "boost": 2
                }
              }
            },
            {
              "match": {
                "title": {
                  "query": "my query",
                  "fuzziness": "auto",
                  "boost": 5
                }
              }
            }
          ],
          "minimum_should_match": 1
        }
      }
    }

And if the query become to complicated, I can suggest you to use a search template to keep integration easy on the app side: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html