Autocomplete using Completion Suggesters

Hello,
I want to write Autocomplete using Elasticsearch.
I use Completion Suggesters and I have problems with it.

For example
I have fullName and title fields
"fullName": "John Smith"
"title": "Python Developer"

Both fields type is "completion"

My suggested text query is - "Smith"
And I got 0 results

I tried to reverse index
"Smith John"
but there I got the opposite result ("Smith John") and not the original ("John Smith").

I know that happens because Completion Suggesters is prefix based

So, what is the best way to get "John Smith" when I search "Smith" using Completion Suggesters?

Btw, the "title" field is because I want to search on both fields.

For example -
"fullName": "Mike Devqon"
"title": "Python Developer"

My suggested text query is - "Dev"
I want to get
"Python Developer" and "Mike Devqon"

It is important to note that I am not interested in receiving results of full documents and then looking for a way to extract the relevant values from it using highlight for example.

Thank you.

Hi @Senchok

Look this example:

PUT idx_test
{
  "mappings": {
    "properties": {
      "fullName": {
        "type": "completion"
      },
      "title": {
        "type": "completion"
      }
    }
  }
}

POST idx_test/_doc
{
  "fullName": ["John", "Smith"],
  "title": ["Python", "Developer"]
}


GET idx_test/_search
{
  "suggest": {
    "title-suggest": {
      "prefix": "Smith",        
      "completion": {         
          "field": "title"  
      }
    },
     "fullName-suggest": {
      "prefix": "Smith",        
      "completion": {         
          "field": "fullName"  
      }
    }
  }
}

Thanks for your answer.
Your example return only "John" when I search "John" and return "Smith" when I search "Smith".
I want get the full name "John Smith" for both searches.

Add the entry ["John", "Smith", "John Smith"] in input.

Still got "John" or "Smith" and not "John Smith"

And if you create fields for suggestion and use "_source": ["title", "fullName"] to get this values.

POST idx_test/_doc
{
  "fullName": "John Smith",
  "title": "Python Developer",
  "fullName_suggestion": [
    "John",
    "Smith"
  ],
  "title_suggestion": [
    "Python",
    "Developer"
  ]
}


GET idx_test/_search
{
  "_source": ["title", "fullName"], 
  "suggest": {
    "title-suggest": {
      "prefix": "Smith",        
      "completion": {         
          "field": "title_suggestion"  
      }
    },
     "fullName-suggest": {
      "prefix": "Smith",        
      "completion": {         
          "field": "fullName_suggestion"  
      }
    }
  }
}

I tried it and my problem with this is the order of the answers

Gomez Joanizo
John Smith
Fabis Jorden

instead of

John Smith
Gomez Joanizo
Fabis Jorden

Its look like the answers ordered by alphabetically

Joanizo
John
Jorden

And I don't want to sort in my Application service - less efficient

PUT /my_index
{
  "mappings": {
    "properties": {
      "fullName": {
        "type": "text"
      },
      "fullName-suggest": {
        "type": "completion"
      }
    }
  }
}

POST /my_index/_doc/1
{
  "fullName": "John Smith",
  "fullName-suggest": {
    "input": [
      "John",
      "Smith"
    ]
  }
}

POST /my_index/_doc/2
{
  "fullName": "Fabis Jorden",
  "fullName-suggest": {
    "input": [
      "Fabis",
      "Jorden"
    ]
  }
}

POST /my_index/_doc/3
{
  "fullName": "Gomez Joanizo",
  "fullName-suggest": {
    "input": [
      "Gomez",
      "Joanizo"
    ]
  }
}

POST /my_index/_search
{
  "_source": ["fullName"], 
  "suggest": {
    "fullName-suggest": {
      "prefix": "Jo",
      "completion": {
        "field": "fullName-suggest"
      }
    }
  }
}

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 0,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "suggest": {
    "fullName-suggest": [
      {
        "text": "Jo",
        "offset": 0,
        "length": 2,
        "options": [
          {
            "text": "Joanizo",
            "_index": "my_index",
            "_id": "3",
            "_score": 1,
            "_source": {
              "fullName": "Gomez Joanizo"
            }
          },
          {
            "text": "John",
            "_index": "my_index",
            "_id": "1",
            "_score": 1,
            "_source": {
              "fullName": "John Smith"
            }
          },
          {
            "text": "Jorden",
            "_index": "my_index",
            "_id": "2",
            "_score": 1,
            "_source": {
              "fullName": "Fabis Jorden"
            }
          }
        ]
      }
    ]
  }
}

Anybody?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.