Need advice for my term completion feature


(Basiclaser) #1

I have a website with a searchbar where users can search for documents.
Currently I am using a term suggester ( https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-term.html ) to provide word suggestions to people as they type their queries.
This approach is 99% perfect, except that I want a greater edit distance / Levenstein distance for the suggestions.
It seems the maximum max_edits is set to 2, which AFAIK means that if a user is typing 'subspace', they have to type 'subspa' before they get a matching suggestion.
I would prefer it is the matching suggestion was available after only 3-4 characters were typed eg.'subs'. It seems this is a limitation within term suggester.

Is there any way of circumventing this and increasing the edit distance, or switching the suggester to prefix functionality?

I am considering other methods such as prefix-filters ( https://www.elastic.co/guide/en/elasticsearch/reference/1.4/query-dsl-prefix-filter.html ) . Is it possible to use prefix-filters and extract and return only the matching terms, not the whole matching documents?

Thanks for your help.


(Mike Simos) #2

Hi,

Have you seen these two examples in the definitive guide:

https://www.elastic.co/guide/en/elasticsearch/guide/current/_query_time_search_as_you_type.html

https://www.elastic.co/guide/en/elasticsearch/guide/current/_index_time_search_as_you_type.html

Take a look at these as they may help. Probably the second example using edge_ngram will work best.


(Basiclaser) #3

Msimos thanks for helping.
My issue with this is that I just want the matching terms to be returned, not the documents containing the matching terms.

So if i query 'sub', i want 'subspace', 'substring', 'subzero' to be returned. Is there a way to do this with queries?


(Basiclaser) #4

My issue is that term suggester has a limit of 2 edit distance. Could I solve this problem by adding edge Ngrams to the field which i want to perform autocompletions on?


(Mike Simos) #5

Hi,

If there is a specific field you are using autocomplete for, then you can use the "fields" directive to just return that field and not the whole document. Like:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-fields.html

GET /my_index/my_type/_search
{
  "fields": ["name"], 
    "query": {
        "match": {
            "name": "yel"
        }
    }
}

Which will just return:

            "fields": {
               "name": [
                  "Yellow furballs"
               ]
            }

So at index time, you can add a list of terms into your documents which you can use for autocomplete. Or you can use any existing fields in your documents like title, city or whatever makes sense.

Here's a useful comparison of the different autosuggest methods:

http://techblog.realestate.com.au/implementing-autosuggest-in-elasticsearch/


(Mike Simos) #6

One last thing, you can also use the highlighter to get the term like this:

GET /my_index/my_type/_search
{
  "fields": ["name"], 
    "query": {
        "match": {
            "name": "yel"
        }
    },
    "highlight": {
      "fields": {
        "name": {}
      }
    }
}
      "hits": [
         {
            "_index": "my_index",
            "_type": "my_type",
            "_id": "2",
            "_score": 0.625,
            "fields": {
               "name": [
                  "Yellow furballs"
               ]
            },
            "highlight": {
               "name": [
                  "<em>Yellow</em> furballs"
               ]
            }
         }

Then you need to parse out the term(s) which have been highlighted.


(system) #7