# Autocomplete using Completion Suggesters

**URL:** <https://discuss.elastic.co/t/autocomplete-using-completion-suggesters/338823>\
**Category:** Elasticsearch\
**Created:** [July 19, 2023, 7:25pm UTC](https://discuss.elastic.co/t/autocomplete-using-completion-suggesters/338823 "2023-07-19T19:25:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Senchok](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/senchok/32/122545_2.png) [@Senchok](https://discuss.elastic.co/u/Senchok)\
**Post date:** [July 19, 2023, 7:25pm UTC](https://discuss.elastic.co/t/autocomplete-using-completion-suggesters/338823/1 "2023-07-19T19:25:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [July 20, 2023, 2:04am UTC](https://discuss.elastic.co/t/autocomplete-using-completion-suggesters/338823/2 "2023-07-20T02:04:14Z")

</div>

Hi @Senchok

Look this example:

```auto
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"  
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Senchok](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/senchok/32/122545_2.png) [@Senchok](https://discuss.elastic.co/u/Senchok)\
**Post date:** [July 20, 2023, 4:37am UTC](https://discuss.elastic.co/t/autocomplete-using-completion-suggesters/338823/3 "2023-07-20T04:37:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [July 20, 2023, 11:12am UTC](https://discuss.elastic.co/t/autocomplete-using-completion-suggesters/338823/4 "2023-07-20T11:12:47Z")

</div>

> [@Senchok](#):
>
> I want get the full name "John Smith"

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

---

<div class="post-metadata">

**Author:** ![Senchok](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/senchok/32/122545_2.png) [@Senchok](https://discuss.elastic.co/u/Senchok)\
**Post date:** [July 20, 2023, 11:46am UTC](https://discuss.elastic.co/t/autocomplete-using-completion-suggesters/338823/5 "2023-07-20T11:46:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [July 20, 2023, 4:04pm UTC](https://discuss.elastic.co/t/autocomplete-using-completion-suggesters/338823/6 "2023-07-20T16:04:32Z")

</div>

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

```auto
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"  
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Senchok](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/senchok/32/122545_2.png) [@Senchok](https://discuss.elastic.co/u/Senchok)\
**Post date:** [July 21, 2023, 3:31pm UTC](https://discuss.elastic.co/t/autocomplete-using-completion-suggesters/338823/7 "2023-07-21T15:31:17Z")

</div>

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

```auto
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"
            }
          }
        ]
      }
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![Senchok](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/senchok/32/122545_2.png) [@Senchok](https://discuss.elastic.co/u/Senchok)\
**Post date:** [July 31, 2023, 5:12pm UTC](https://discuss.elastic.co/t/autocomplete-using-completion-suggesters/338823/8 "2023-07-31T17:12:04Z")

</div>

Anybody?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 28, 2023, 5:12pm UTC](https://discuss.elastic.co/t/autocomplete-using-completion-suggesters/338823/9 "2023-08-28T17:12:22Z")

</div>

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