6

I need to implement elasticsearch completion suggester.

I have an index mapped like this:

{
  "user": {
    "properties": {
      "username": {
        "index": "not_analyzed",
        "analyzer": "simple",
        "type": "string"
      },
      "email": {
        "index": "not_analyzed",
        "analyzer": "simple",
        "type": "string"
      },
      "name": {
        "index": "not_analyzed",
        "analyzer": "simple",
        "type": "string"
      },
      "name_suggest": {
        "payloads": true,
        "type": "completion"
      }
    }
  }
}

I add documents to the index like this:

{
  "doc": {
    "id": 1,
    "username": "jack",
    "name": "Jack Nicholson",
    "email": "nick@myemail.com",
    "name_suggest": {
      "input": [
        "jack",
        "Jack Nicholson",
        "nick@myemail.com"
      ],
      "payload": {
        "id": 1,
        "name": "Jack Nicholson",
        "username": "jack",
        "email": "nick@myemail.com"
      },
      "output": "Jack Nicholson (jack) - nick@myemail.com"
    }
  },
  "doc_as_upsert": true
}

And I send this request to my_index/_suggest:

{
  "user": {
    "text": "jack",
    "completion": {
      "field": "name_suggest"
    }
  }
}

I get the resulting options that look like this:

[
  {
    "text": "John Smith",
    "score": 1.0,
    "payload": {
      "id": 11,
      "name": "John Smith",
      "username": "jack",
      "email": "john@myemail.com"
    }
  },
  {
    "text": "Jack Nickolson",
    "score": 1.0,
    "payload": {
      "id": 1,
      "name": "Jack Nickolson",
      "username": "jack.n",
      "email": "nickolson@myemail.com"
    }
  },
  {
    "text": "Jackson Jermaine",
    "score": 1.0,
    "payload": {
      "id": 10,
      "name": "Jackson Jermaine",
      "username": "jermaine",
      "email": "jermaine@myemail.com"
    }
  },
  {
    "text": "Tito Jackson",
    "score": 1.0,
    "payload": {
      "id": 9,
      "name": "Tito Jackson",
      "username": "tito",
      "email": "jackson@myemail.com"
    }
  },
  {
    "text": "Michael Jackson",
    "score": 1.0,
    "payload": {
      "id": 6,
      "name": "Michael Jackson",
      "username": "michael_jackson",
      "email": "jackson_michael@myemail.com"
    }
  }
]

This works fine but, I need to have the options sorted that way that those that have username matched come first. I can do it manually, but that would prevent me to use length and offset and would be slower.

Is it possible to add scoring to the individual inputs (not the whole suggests), and that way affect the sorting? With the approach that I use it seems it is not.

Another related question, is it possible to specify in the input an array of fields instead of an array of values, and that way avoid the duplication? If yes, would setting the score on the fields be taken into account when ES generates suggestions?

Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
bosskovic
  • 2,034
  • 1
  • 14
  • 29
  • 1
    As far as I am aware you can't do custom scoring with the completion suggester. This is because it's designed to be really really quick. Also, when ES is searching for completions it's not searching your payloads, it's searching through the "input" field, not the different fields in "payload". Payload is literally the stuff that gets returned by ES when giving you back the suggestions. – Garry Welding Mar 17 '14 at 16:40
  • Thanks for the answer Garry! I know about the difference between the payload and input. I use payload to avoid an additional request for the matching suggestions. – bosskovic Mar 17 '14 at 17:43
  • It seem even running specific types of queries (wildcard / regexp) isn't possible. Maybe I'm mistaken! IS it possible to feed the output of your suggest into a relevance query? – Steve Mar 21 '14 at 09:24
  • Not possible with this Lucene storage engine. But you can use simple indexing to match your needs (suggestion + scoring). – Thomas Decaux Aug 20 '14 at 11:19

1 Answers1

1

You can add score to your input with the weight option.

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters-completion.html#indexing

Max
  • 774
  • 7
  • 20