webmaster Chp
webmaster Chp

Reputation: 97

how to deal with search_as_you_type field and misspelling

I would like to do a search in a e-commerce website to find product with a good query that manage the misspelling.

I was learning elasticsearch and the search_as_you_type field. But in the doc, you have search_as_you_type and fuzziness.

If I have this product : Post-it Notes 76x76 mm

I would like something to find this product if I write :

and then, find all similar products (because in this example I have only one product but in reality around 30 000)

For the moment I have someting like that for the mapping and I don't know how to add a fuzzyness :

"mappings": {
    "properties": {
        "title": {
            "type": "search_as_you_type",
        }
    }
},

Thank your for your help

Upvotes: 1

Views: 173

Answers (2)

Bob Yoplait
Bob Yoplait

Reputation: 2499

If the indexed field is of type search_as_you_type, you can use something like here below, however, you will not have fuzziness on the last term or if there is only one term.

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html#type-bool-prefix

{  
  "query": {
    "bool": {
      "must": {
        "multi_match": {
          "type": "bool_prefix",
          "query": "one two th",
      "fields": [
        "autocomp.etab",
        "autocomp.etab._2gram",
        "autocomp.etab._3gram"
      ],
      "operator": "AND",
      "fuzziness": "AUTO"
    }
  }  
}

If you want fuzziness on the last term of the query (single term query being a specific case of that), you can use a completion suggester but this has other limitations. For example, if the queried term is the second term of the search field, it doesn't find it as such directly. Anyway, here is the completion suggester query:

{
  "suggest": {
    "my_suggestion": {
      "prefix": "Arras football ma",
      "completion": {
        "field": "s_alpha.autocomp_suggest",
        "contexts": {
          "location": [
            {
              "context": {
                "lat": 50.28472,
                "lon":  2.766359
              },
              "precision": "50km"
            }
          ]
        }
      }
    }
  }
}

Upvotes: 0

Related Questions