# Help needed with search as you type

**URL:** <https://discuss.elastic.co/t/help-needed-with-search-as-you-type/204486>\
**Category:** Elasticsearch\
**Created:** [October 21, 2019, 1:49pm UTC](https://discuss.elastic.co/t/help-needed-with-search-as-you-type/204486 "2019-10-21T13:49:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![iuzzino](https://avatars.discourse-cdn.com/v4/letter/i/77aa72/32.png) [@iuzzino](https://discuss.elastic.co/u/iuzzino)\
**Post date:** [October 21, 2019, 1:49pm UTC](https://discuss.elastic.co/t/help-needed-with-search-as-you-type/204486/1 "2019-10-21T13:49:13Z")

</div>

My goal is to create a search engine (elasticsearch 7.x) that suggests you the correct searches to input, a sort of what amazon does, based on previous searches (machine learning). My fields to index are the following:

- Keywords = search phrases
- occurrences = number of times this search was performed
- total = number of results (it only to exclude searches that produced no results)

INDEX  
{  
"mappings": {  
"properties": {  
"keywords": {  
"type": "search\_as\_you\_type"  
},  
"occurences":{  
"type": "integer"  
}  
}  
}  
}

What I would like to achieve is a search as you type "starts with" but when I apply the query:

{  
"query": {  
"match\_phrase\_prefix": {  
"keywords": "fill"  
}  
}  
}

I get not only keywords starting wiht fill (such as "filler") But also other that contain the query, such as "real filler" or any other phrase that contains "fill". What I would like to ahcieve is returning only the phrases starting with the query term.

The next step would be to boost results that have a higher value of occurences

Any hint?  
Thank you

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [October 22, 2019, 1:33pm UTC](https://discuss.elastic.co/t/help-needed-with-search-as-you-type/204486/2 "2019-10-22T13:33:41Z")

</div>

I think the [completion suggester](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters.html#completion-suggester) may be a better fit for what you're trying to do. It will only offer suggestions that start with the prefix you're requesting. It also allows you to specify a `weight` for every suggestion, which will be used for ranking the suggestions. In your case the weight could be equal to the number of `occurrences`.

Given an index mapping like this:

```auto
PUT my_index
{
  "mappings": {
    "properties": {
      "keywords": {
        "type": "completion"
      }
    }
  }
}

```

You can index a few documents like below. The `weight` here would be populated with the value of `occurrences`:

```auto
PUT my_index/_doc/1
{
  "keywords": {
    "input": "filler1",
    "weight": 1
  }
}

PUT my_index/_doc/2
{
  "keywords": {
    "input": "filler2",
    "weight": 2
  }
}

PUT my_index/_doc/3
{
  "keywords": {
    "input": "real filler",
    "weight": 3
  }
}

```

If you now send the following suggest request to Elasticsearch, it will only return documents 1 and 2, and it will rank them based on the provided weight:

```auto
POST my_index/_search
{
  "suggest": {
    "my_suggestion": {
      "prefix": "fill",
      "completion": {
        "field": "keywords"
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![iuzzino](https://avatars.discourse-cdn.com/v4/letter/i/77aa72/32.png) [@iuzzino](https://discuss.elastic.co/u/iuzzino)\
**Post date:** [October 23, 2019, 7:09am UTC](https://discuss.elastic.co/t/help-needed-with-search-as-you-type/204486/3 "2019-10-23T07:09:42Z")

</div>

Got it!  
I tested it and it works very fine. Thank you Abdon, I appreciate your help

---

<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:** [November 20, 2019, 7:09am UTC](https://discuss.elastic.co/t/help-needed-with-search-as-you-type/204486/4 "2019-11-20T07:09:48Z")

</div>

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