6

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?

Ins
  • 808
  • 6
  • 13

1 Answers1

3

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.

technomage
  • 9,861
  • 2
  • 26
  • 40
Heschoon
  • 2,915
  • 9
  • 26
  • 55
  • Thanks for the answer. Is there possible to apply different weightings for name and first name like you can with regular queries? – Harry Wang Jan 30 '15 at 15:29
  • 1
    Hi @Harry. Explaining this in detail would be outside the scope of this question and would make this thead quite messy. You can have a look [here](http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/_boosting_query_clauses.html) or ask a new question and ask me to answer it. Regards, Heschoon – Heschoon Jan 31 '15 at 11:02
  • Thanks a lot, @Heschoon! The article is helpful but I'm still unsure how to make it work for completion suggester specifically. Any chance you can answer my [question](http://stackoverflow.com/q/28268418/2926423)? – Harry Wang Feb 01 '15 at 21:59
  • Done @Harry. I hoped it helped you :) – Heschoon Feb 02 '15 at 15:36