# Autocomplete based on past user searches

**URL:** <https://discuss.elastic.co/t/autocomplete-based-on-past-user-searches/220180>\
**Category:** Elasticsearch\
**Created:** [February 20, 2020, 11:57am UTC](https://discuss.elastic.co/t/autocomplete-based-on-past-user-searches/220180 "2020-02-20T11:57:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![abhijith\_chandran](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhijith_chandran/32/60470_2.png) [@abhijith\_chandran](https://discuss.elastic.co/u/abhijith_chandran)\
**Post date:** [February 20, 2020, 11:57am UTC](https://discuss.elastic.co/t/autocomplete-based-on-past-user-searches/220180/1 "2020-02-20T11:57:48Z")

</div>

I am trying to add autocomplete based on what the user searches.  
Currently, I have the following mapping:

```auto
{
  "courts_2": {
    "mappings": {
      "properties": {
        "author": {
          "type": "text",
          "analyzer": "my_analyzer"
        },
        "bench": {
          "type": "text",
          "analyzer": "my_analyzer"
        },
        "citation": {
          "type": "text"
        },
        "content": {
          "type": "text",
          "fields": {
            "standard": {
              "type": "text"
            }
          },
          "analyzer": "my_analyzer"
        },
        "court": {
          "type": "text"
        },
        "date": {
          "type": "text"
        },
        "id_": {
          "type": "text"
        },
        "title": {
          "type": "text",
          "fields": {
            "standard": {
              "type": "text"
            }
          },
          "analyzer": "my_analyzer"
        },
        "verdict": {
          "type": "text"
        }
      }
    }
  }
}

```

Below is the code I used for the settings:

```auto
{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "my_analyzer": {
            "tokenizer": "standard",
            "filter": [
              "lowercase",
              "my_metaphone"
            ]
          }
        },
        "filter": {
          "my_metaphone": {
            "type": "phonetic",
            "encoder": "metaphone",
            "replace": true
          }
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "author": {
        "type": "text",
        "analyzer": "my_analyzer"
      },
      "bench": {
        "type": "text",
        "analyzer": "my_analyzer"
      },
      "citation": {
        "type": "text"
      },
      "court": {
        "type": "text"
      },
      "date": {
        "type": "text"
      },
      "id_": {
        "type": "text"
      },
      "verdict": {
        "type": "text"
      },
      "title": {
        "type": "text",
        "analyzer": "my_analyzer",
        "fields": {
          "standard": {
            "type": "text"
          }
        }
      },
      "content": {
        "type": "text",
        "analyzer": "my_analyzer",
        "fields": {
          "standard": {
            "type": "text"
          }
        }
      }
    }
  }
}

```

Here's what I would like to implement: I would like to collect and store all of the queries made to the endpoint and use autocomplete on that. For example, to date, all of the users have made the following queries -

```auto
Real Madrid v/s Barcelona 
Real Madrid Team 
Real Madrid Coach 
Barcelona v/s Man City 
Sevilla Home Ground
Man Utd. recent results

```

Now, if anyone searches `Rea` then the following autocomplete queries should be suggested:

```auto
Real Madrid v/s Barcelona 
Real Madrid Team 
Real Madrid Coach 

```

This based on the searches made by all of the users till date and not a single user. Further, I would like to analyze what are the top queries that were made in let's say the past month.

I am using ElasticSearch version 7.1 on AWS Elasticsearch service.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [February 20, 2020, 2:23pm UTC](https://discuss.elastic.co/t/autocomplete-based-on-past-user-searches/220180/2 "2020-02-20T14:23:08Z")

</div>

This is a complex topic and cannot really be answered in a small quick reply. Just adding a couple of throughts. First you have to think about what kind of suggestions you want

1. If they should be based on your data (and not the queries), then take a look at the search-as-you-type datatype, see [https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-as-you-type.html](https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-as-you-type.html)

2. If you want this to be based on the existing searches, you first need to search log all the searches typed by your users and use that as a data source for suggestions (you could again use the afore mentioned search as you type datatype). However you would still need to ensure that your searches also return documents and probably have some logic to score popular searches higher (i.e. via rank\_feature), see [https://www.elastic.co/guide/en/elasticsearch/reference/master/rank-feature.html](https://www.elastic.co/guide/en/elasticsearch/reference/master/rank-feature.html)

As you can see, to implement this properly, this requires some more trial and error to come up with a solution. Hope this helps as a first hint.

--Alex

---

<div class="post-metadata">

**Author:** ![abhijith\_chandran](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhijith_chandran/32/60470_2.png) [@abhijith\_chandran](https://discuss.elastic.co/u/abhijith_chandran)\
**Post date:** [February 21, 2020, 12:01pm UTC](https://discuss.elastic.co/t/autocomplete-based-on-past-user-searches/220180/3 "2020-02-21T12:01:26Z")

</div>

I'll go through the links. Thank you, @spinscale!

---

<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:** [March 20, 2020, 12:01pm UTC](https://discuss.elastic.co/t/autocomplete-based-on-past-user-searches/220180/4 "2020-03-20T12:01:34Z")

</div>

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