# Autocomplete address search

**URL:** <https://discuss.elastic.co/t/autocomplete-address-search/304715>\
**Category:** Elasticsearch\
**Created:** [May 13, 2022, 11:21pm UTC](https://discuss.elastic.co/t/autocomplete-address-search/304715 "2022-05-13T23:21:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![foobar2022](https://avatars.discourse-cdn.com/v4/letter/f/df788c/32.png) [@foobar2022](https://discuss.elastic.co/u/foobar2022)\
**Post date:** [May 13, 2022, 11:21pm UTC](https://discuss.elastic.co/t/autocomplete-address-search/304715/1 "2022-05-13T23:21:20Z")

</div>

Hi,  
I'm trying to build an index & query for address search autocomplete for City, State & Country.  
I have a structured data fields:

- City,
- State
- Country
- Full (a complete field that concatenates City State & Country, separated by space)

I used ngram approach, and that was working fine when I'm using it on City+Country. But when the field is longer, the ngram approach is not working. for eg I'm not getting any result for query term 'san fran', but I get result for 'san francisco'

I'm using this for indexing:

```auto
{
  "index_patterns": ["address_book*"],
  "settings": {
    "analysis": {
      "analyzer": {
        "autocomplete": {
          "tokenizer": "autocomplete",
          "filter": [
            "lowercase"
          ]
        },
        "autocomplete_search": {
          "tokenizer": "lowercase"
        }
      },
      "tokenizer": {
        "autocomplete": {
          "type": "edge_ngram",
          "min_gram": 2,
          "max_gram": 50,
          "token_chars": [
            "letter"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "full": {
        "type": "text",
        "analyzer": "autocomplete",
        "search_analyzer": "autocomplete_search"
      },
      "location": {
        "type": "geo_point"
      }
    }
  }
 }

```

And the query is:

```auto
{
   "query": {
    "match": {
      "full": {
        "query": "san fran", 
        "operator": "and"
      }
    }
  }
}

```

I'm guessing that the index field (full) needs to be tokenized if that is not happening already?  
Any suggestion on how I can get the auto complete working on the partial matches?

Thank you

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [May 13, 2022, 11:54pm UTC](https://discuss.elastic.co/t/autocomplete-address-search/304715/2 "2022-05-13T23:54:51Z")

</div>

Did you look at

Should work pretty good on the full concatenated address.

As people start to type the address. You have to be a little careful on the front and so that you don't flood your back end with queries, You need a couple millisecond pause between submissions, but I'm not the front-end expert 🙂

> **[Search-as-you-type field type | Elasticsearch Guide \[8.2\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-as-you-type.html)**

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [May 14, 2022, 12:58am UTC](https://discuss.elastic.co/t/autocomplete-address-search/304715/3 "2022-05-14T00:58:10Z")

</div>

Ohh and App Search pretty much has the Out of the Box

---

<div class="post-metadata">

**Author:** ![foobar2022](https://avatars.discourse-cdn.com/v4/letter/f/df788c/32.png) [@foobar2022](https://discuss.elastic.co/u/foobar2022)\
**Post date:** [May 14, 2022, 1:22am UTC](https://discuss.elastic.co/t/autocomplete-address-search/304715/4 "2022-05-14T01:22:48Z")

</div>

ah didn't realize there already is `search_as_you_type` field type! Will give that a try thanks 🙂

On the frontend side, I do have a bouncer that waits for 500ms after the last char before sending the search request.

---

<div class="post-metadata">

**Author:** ![foobar2022](https://avatars.discourse-cdn.com/v4/letter/f/df788c/32.png) [@foobar2022](https://discuss.elastic.co/u/foobar2022)\
**Post date:** [May 14, 2022, 4:45am UTC](https://discuss.elastic.co/t/autocomplete-address-search/304715/5 "2022-05-14T04:45:07Z")

</div>

@stephenb Still I'm having exact same issue with `search_as_you_type`  
Here is the new index setting:

```auto
 {
  "index_patterns": ["address_book*"],
  "mappings": {
    "properties": {
      "full": {
        "type": "search_as_you_type"
      },
      "location": {
        "type": "geo_point"
      }
    }
  }
 }

```

Here is a field value: 'San Francisco California USA US'  
I get results for: 'san francisco', 'san usa', 'san us'  
But no results for: 'san fran', 'san cali'

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [May 14, 2022, 5:23am UTC](https://discuss.elastic.co/t/autocomplete-address-search/304715/6 "2022-05-14T05:23:13Z")

</div>

Hmmmm I get all expected results

```auto
PUT discuss-search-as-type
{
  "mappings": {
    "properties": {
      "full": {
        "type": "search_as_you_type"
      },
      "location": {
        "type": "geo_point"
      }
    }
  }
}

POST discuss-search-as-type/_doc
{
  "full" : "San Francisco California USA US"
}

POST discuss-search-as-type/_doc
{
  "full" : "San Diego USA US"
}

```

I get

```auto
GET discuss-search-as-type/_search
{
  "query": {
    "multi_match": {
      "query": "san",
      "type": "bool_prefix",
      "fields": [
        "full",
        "full._2gram",
        "full._3gram"
      ]
    }
  }
}

#Result Both as Expected
{
  "took" : 67,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "discuss-search-as-type",
        "_id" : "OU77wIABpvjF7kd-eB1V",
        "_score" : 1.0,
        "_source" : {
          "full" : "San Francisco California USA US"
        }
      },
      {
        "_index" : "discuss-search-as-type",
        "_id" : "Ok4BwYABpvjF7kd-Dx1b",
        "_score" : 1.0,
        "_source" : {
          "full" : "San Diego USA US"
        }
      }
    ]
  }
}

```

For

I get results for: 'san francisco', 'san usa', 'san us'  
Also for 'san fran', 'san cali'

```auto
GET discuss-search-as-type/_search
{
  "query": {
    "multi_match": {
      "query": "san fran",
      "type": "bool_prefix",
      "fields": [
        "full",
        "full._2gram",
        "full._3gram"
      ]
    }
  }
}

```

Results Both as Expected, Scored as expected san fran higher

```auto
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 2.1743946,
    "hits" : [
      {
        "_index" : "discuss-search-as-type",
        "_id" : "OU77wIABpvjF7kd-eB1V",
        "_score" : 2.1743946,
        "_source" : {
          "full" : "San Francisco California USA US"
        }
      },
      {
        "_index" : "discuss-search-as-type",
        "_id" : "Ok4BwYABpvjF7kd-Dx1b",
        "_score" : 0.19100355,
        "_source" : {
          "full" : "San Diego USA US"
        }
      }
    ]
  }
}

```

And for just "fran"

```auto
GET discuss-search-as-type/_search
{
  "query": {
    "multi_match": {
      "query": "fran",
      "type": "bool_prefix",
      "fields": [
        "full",
        "full._2gram",
        "full._3gram"
      ]
    }
  }
}

```

Results

```auto
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "discuss-search-as-type",
        "_id" : "OU77wIABpvjF7kd-eB1V",
        "_score" : 1.0,
        "_source" : {
          "full" : "San Francisco California USA US"
        }
      }
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![foobar2022](https://avatars.discourse-cdn.com/v4/letter/f/df788c/32.png) [@foobar2022](https://discuss.elastic.co/u/foobar2022)\
**Post date:** [May 14, 2022, 5:42am UTC](https://discuss.elastic.co/t/autocomplete-address-search/304715/7 "2022-05-14T05:42:59Z")

</div>

Turns out my query was not good:

```auto
{
   "query": {
    "match": {
      "full_address": {
        "query": "san fran", 
        "operator": "and"
      }
    }
  }
}

```

I tried it against your example, It didn't produce any result. I was expecting to partially match all search terms to avoid looking into score values to discard unwanted results.  
For eg, for the query 'san fran', the result 'san diego' is not relevant.

I think the sorted result is good enough for now. Thanks a lot for digging in deep 🙂

---

<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:** [June 11, 2022, 5:43am UTC](https://discuss.elastic.co/t/autocomplete-address-search/304715/8 "2022-06-11T05:43:18Z")

</div>

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