0
votes

I'm trying to create a search with suggestions in my website with Elasticsearch. I have a "title" field that has set the mappings this way.

  "mappings" : {
    "products" : {
      "properties" : {
        "title": {
          "type" : "completion", 
          "analyzer": "simple",
          "search_analyzer": "standard"
        }
      }
    }
  }

And this is the search query I'm going to use.

"query": {
"bool": {
  "must": [
    {
      "multi_match": {
        "fields": [ "title^2", "description"],
        "query": "cuboard",
        "fuzziness": "AUTO"
      }
    }]
  }
}

When I run this it returns an error. I use analyzer for get suggestions & it works well. Can't I use fuzziness in analyzed fields in Elasticsearch for searches?

{
  "error": {
    "root_cause": [
      {
        "type": "query_shard_exception",
        "reason": "failed to create query: {\n  \"bool\" : {\n    \"must\" : [\n      {\n        \"multi_match\" : {\n          \"query\" : \"cuboard\",\n          \"fields\" : [\n            \"discrip^1.0\",\n            \"title^2.0\"\n          ],\n          \"type\" : \"best_fields\",\n          \"operator\" : \"OR\",\n          \"slop\" : 0,\n          \"fuzziness\" : \"AUTO\",\n          \"prefix_length\" : 0,\n          \"max_expansions\" : 50,\n          \"zero_terms_query\" : \"NONE\",\n          \"auto_generate_synonyms_phrase_query\" : true,\n          \"fuzzy_transpositions\" : true,\n          \"boost\" : 1.0\n        }\n      }\n    ],\n    \"adjust_pure_negative\" : true,\n    \"boost\" : 1.0\n  }\n}",
        "index_uuid": "VIpm20b0Rra0PI1fyKCsKQ",
        "index": "offers"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "products",
        "node": "gTaPvDb2QXyR3VdK0gX3qg",
        "reason": {
          "type": "query_shard_exception",
          "reason": "failed to create query: {\n  \"bool\" : {\n    \"must\" : [\n      {\n        \"multi_match\" : {\n          \"query\" : \"cuboard\",\n          \"fields\" : [\n            \"discrip^1.0\",\n            \"title^2.0\"\n          ],\n          \"type\" : \"best_fields\",\n          \"operator\" : \"OR\",\n          \"slop\" : 0,\n          \"fuzziness\" : \"AUTO\",\n          \"prefix_length\" : 0,\n          \"max_expansions\" : 50,\n          \"zero_terms_query\" : \"NONE\",\n          \"auto_generate_synonyms_phrase_query\" : true,\n          \"fuzzy_transpositions\" : true,\n          \"boost\" : 1.0\n        }\n      }\n    ],\n    \"adjust_pure_negative\" : true,\n    \"boost\" : 1.0\n  }\n}",
          "index_uuid": "VIpm20b0Rra0PI1fyKCsKQ",
          "index": "offers",
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "Can only use fuzzy queries on keyword and text fields - not on [title] which is of type [completion]"
          }
        }
      }
    ]
  },
  "status": 400
}
1
You need to provide the full error you get with more details.Val
I added the full errorTharindu

1 Answers

1
votes

There you go you can find the reason of the error:

Can only use fuzzy queries on keyword and text fields - not on [title] which is of type [completion]

There's a way to do fuzzy queries on completion fields, but you need to do it differently, like this:

POST products/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "fields": [
              "description"
            ],
            "query": "cuboard",
            "fuzziness": "AUTO"
          }
        }
      ]
    }
  },
  "suggest": {
    "offer-suggest": {
      "prefix": "cuboard",
      "completion": {
        "field": "title",
        "fuzzy": {
          "fuzziness": "AUTO"
        }
      }
    }
  }
}

UPDATE: If you really want to combine the title and description fields into the same query, you need to change your mapping to this:

{
  "mappings": {
    "products": {
      "properties": {
        "title": {
          "type": "completion",
          "analyzer": "simple",
          "search_analyzer": "standard",
          "fields": {
            "title_analyzed": {
              "type": "text",
              "analyzer": "simple",
              "search_analyzer": "standard"
            }
          }
        }
      }
    }
  }

And then you can modify your query to this:

"query": {
"bool": {
  "must": [
    {
      "multi_match": {
        "fields": [ "title.title_analyzed^2", "description"],
        "query": "cuboard",
        "fuzziness": "AUTO"
      }
    }]
  }
}