# Fixing query for type ahead with more than one query term

**URL:** https://discuss.elastic.co/t/fixing-query-for-type-ahead-with-more-than-one-query-term/349564
**Category:** Elasticsearch
**Created:** [December 18, 2023, 2:30pm UTC](https://discuss.elastic.co/t/fixing-query-for-type-ahead-with-more-than-one-query-term/349564 "2023-12-18T14:30:19Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![RodAndTom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rodandtom/32/126553_2.png) [@RodAndTom](https://discuss.elastic.co/u/RodAndTom)
#### Post date: [December 18, 2023, 2:30pm UTC](https://discuss.elastic.co/t/fixing-query-for-type-ahead-with-more-than-one-query-term/349564/1 "2023-12-18T14:30:19Z")

</div>

Hi,

I'm currently using the following query to suggest terms for type ahead completion for a title field, and it suits me very well:

```auto
GET transcricoes/_search
{
  "query": {
    "wildcard": {
      "name": {
        "value": "pr*"
      }
    }
  },
  "highlight": {
    "fields": {
      "name": {"type": "plain"}},
    "pre_tags": "<hl>",
    "post_tags": "</hl>",
    "fragment_size" : 255,
    "order": "score",
    "fragmenter": "simple",
    "boundary_chars": ".,!?",
    "boundary_scanner": "word"
  },
  "_source": false
}

```

This query will return results like "Prime" and "Prime Minister", which is correct for my use case.  
Problem is when try to add a second term for completion. For example "value": "prime m\*". In this case, no results are returned. Any ideas how to fix my query?

Thanks in advance.

---

<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: [December 19, 2023, 7:46pm UTC](https://discuss.elastic.co/t/fixing-query-for-type-ahead-with-more-than-one-query-term/349564/2 "2023-12-19T19:46:26Z")

</div>

Hi @RodAndTom

You haven't provided many details, but the reason it doesn't work is because the token generated isn't composed of prime and minister.  
In this case, I suggest using a shingle analyzer to obtain a token with prime and minister so that your query will work.

```auto
PUT idx_test
{
  "settings": {
    "analysis": {
      "analyzer": {
        "shingle_analyzer": {
          "tokenizer": "standard",
          "filter": [
            "my_shingle_filter"
          ]
        }
      },
      "filter": {
        "my_shingle_filter": {
          "type": "shingle",
          "min_shingle_size": 2,
          "max_shingle_size": 3,
          "output_unigrams": false
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "fields": {
          "shingle": {
            "analyzer": "shingle_analyzer",
            "type": "text"
          }
        }
      }
    }
  }
}

POST idx_test/_doc
{
  "name": "prime"
}

POST idx_test/_doc
{
  "name": "prime minister"
}

GET idx_test/_search
{
  "query": {
    "wildcard": {
      "name.shingle": {
        "value": "prime m*"
      }
    }
  }
}

```

---

<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: [December 19, 2023, 9:20pm UTC](https://discuss.elastic.co/t/fixing-query-for-type-ahead-with-more-than-one-query-term/349564/3 "2023-12-19T21:20:34Z")

</div>

Perhaps take a look at

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

---

<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: [January 16, 2024, 9:20pm UTC](https://discuss.elastic.co/t/fixing-query-for-type-ahead-with-more-than-one-query-term/349564/4 "2024-01-16T21:20:47Z")

</div>

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