Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

elasticsearch completion suggest on multifield

I'm trying to get suggestions from a multifield. I can't find examples like this, so maybe it's not the best idea, but I'm interested in your opinion.

mapping:

POST /authors
    {
       "mappings": {
          "author": {
             "properties": {
                "name": {
                   "type": "multi_field",
                   "fields": {
                      "name": {
                         "type": "string",
                         "index": "analyzed"
                      },
                      "ac": {
                         "type": "completion",
                         "index_analyzer": "simple",
                         "search_analyzer": "simple",
                         "payloads": true
                      }
                   }
                }
             }
          }
       }
    }

data:


POST /authors/author/1
    {
       "name": "Fyodor Dostoevsky"
    }

query:

POST /authors/_suggest

    {
       "authorsAutocomplete": {
          "text": "fyodor",
          "completion": {
             "field": "name.ac"
          }
       }
    }

Requirements are:

  • get query works with text "fyodor" and also with "dostoevsky", this example works only with "fyodor"
  • be enable to filter suggestions

any ideas how can I achieve these?

like image 633
Ins Avatar asked Sep 19 '14 11:09

Ins


1 Answers

Firstly, the suggesters don't work well in multi-fields, so you could want to put it outside. Secondly, to make you query work with both the name and firstname, you have to choose the outputs/inputs in when indexing the data.

Example of working code for SENSE:

POST authors

PUT authors/_mapping/author
{
    "properties" : {
        "name" : { "type" : "string" },
        "suggest" : { "type" : "completion"}
    }
}

POST authors/author/1
{
    "name": "Fyodor Dostoevsky",
    "suggest": {
        "input": ["Dostoevsky", "Fyodor"],
        "output": "Fyodor Dostoevsky"
    }
}

POST authors/_suggest
{
    "authorsAutocomplete": {
        "text": "d",
        "completion": {
            "field": "suggest"
        }
    }
}

DELETE authors

Result:

{
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "authorsAutocomplete": [
        {
            "text": "d",
            "offset": 0,
            "length": 1,
            "options": [
                {
                    "text": "Fyodor Dostoevsky",
                    "score": 1
                }
            ]
        }
    ]
}

Filters are not available for suggestions. To implement some kind of filtering, you can look at this blog post about the use of context in suggestions.

like image 103
Heschoon Avatar answered Oct 02 '22 15:10

Heschoon