# How to add suggestion inside term query in DSL

**URL:** <https://discuss.elastic.co/t/how-to-add-suggestion-inside-term-query-in-dsl/309893>\
**Category:** Elasticsearch\
**Created:** [July 18, 2022, 11:19am UTC](https://discuss.elastic.co/t/how-to-add-suggestion-inside-term-query-in-dsl/309893 "2022-07-18T11:19:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sim\_elastic](https://avatars.discourse-cdn.com/v4/letter/s/c77e96/32.png) [@sim\_elastic](https://discuss.elastic.co/u/sim_elastic)\
**Post date:** [July 18, 2022, 11:19am UTC](https://discuss.elastic.co/t/how-to-add-suggestion-inside-term-query-in-dsl/309893/1 "2022-07-18T11:19:13Z")

</div>

My DOc is below

```json
[
{'id':1, 'name': 'sachin messi', 'description': 'football@football.com', 'type': 'football', 'var':'sports'},
{'id':2, 'name': 'lionel messi', 'description': 'messi@fifa.com','type': 'soccer','var':'sports'},
{'id':3, 'name': 'sachin', 'description': 'was', 'type': 'cricket', 'var':'sports'}
]

```

- I need to suggest a string which after the `term`

My DSL query is below

```json
quer = {
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "var.keyword": [
              "notsports"
            ]
          }
        },
        {
          "query_string": {
            "query": "schin*",
            "fields": [
              "name^128",
              "description^64",
            ]
          }
        }
      ]
    }
  },
  "suggest": {
    "my-suggestion": {
      "text": "schin",
      "term": {
        "field": "name",
        "prefix_length": 0,
        "min_word_length": 3,
        "string_distance": "ngram"
      }
    }
  }
}

```

- My var.keyword is `notsports`

- still i am getting suggestion ` 'suggest': {'my-suggestion': [{'text':'schin','offset':0,'length':5,'options': [{'text':'sachin', 'score': 0.75, 'freq': 1}]}`

- When i tried to put `suggest` inside terms list then i am getting `RequestError: RequestError(400, 'x_content_parse_exception', 'unknown query [suggest]')`

- I need to get the suggestion only if `var.keyword` matches `sports`

---

<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 18, 2022, 5:37pm UTC](https://discuss.elastic.co/t/how-to-add-suggestion-inside-term-query-in-dsl/309893/2 "2022-07-18T17:37:33Z")

</div>

Hi @sim_elastic

You could use Phrase suggester to get the suggestions and [collate](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters.html#_basic_phrase_suggest_api_parameters) to match on the "var" field. That way you would get all the suggestions and through the collate, for each doc to guarantee the "var" is the value you want, in your case "notsports"

Like this:

```auto
{
  "suggest": {
    "text": "schin",
    "simple_phrase": {
      "phrase": {
        "field": "name.trigram",
        "collate": {
          "query": {
            "source": {
              "terms": {
                "var.keyword": [
                  "notsports"
                ]
              }
            }
          },
          "prune":false
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![sim\_elastic](https://avatars.discourse-cdn.com/v4/letter/s/c77e96/32.png) [@sim\_elastic](https://discuss.elastic.co/u/sim_elastic)\
**Post date:** [July 19, 2022, 4:15am UTC](https://discuss.elastic.co/t/how-to-add-suggestion-inside-term-query-in-dsl/309893/3 "2022-07-19T04:15:21Z")

</div>

@RabBit_BR But i need to add query\_string also,

```auto
 {
          "query_string": {
            "query": "schin*",
            "fields": [
              "name^128",
              "description^64",
            ]
          }
        }

```

also can i add ` "string_distance": "ngram"` also in collate method?

---

<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 19, 2022, 4:51pm UTC](https://discuss.elastic.co/t/how-to-add-suggestion-inside-term-query-in-dsl/309893/4 "2022-07-19T16:51:02Z")

</div>

> [@sim\_elastic](#):
>
> also can i add ` "string_distance": "ngram"` also in collate method?

No, the string\_distance is parameter of Term Suggester. The collate is parameter [Phrase Suggester](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters.html#_basic_phrase_suggest_api_parameters)

---

<div class="post-metadata">

**Author:** ![sim\_elastic](https://avatars.discourse-cdn.com/v4/letter/s/c77e96/32.png) [@sim\_elastic](https://discuss.elastic.co/u/sim_elastic)\
**Post date:** [July 19, 2022, 6:14pm UTC](https://discuss.elastic.co/t/how-to-add-suggestion-inside-term-query-in-dsl/309893/5 "2022-07-19T18:14:38Z")

</div>

How to add one more term

```auto
                "type.keyword": [
                  "cricket"
                ]
              }

```

when i try

```auto
 {
  "suggest": {
    "text": "schin",
    "simple_phrase": {
      "phrase": {
        "field": "name",
        "collate": {
          "query": {
            "source": [
                  {
                    "terms": {
                      "var.keyword": [
                        "sports"
                      ]
                    }
                  },
                  {
                    "terms": {
                      "type.keyword": [
                        "cricket"
                      ]
                    }
                  }
                ],
          "prune":"false"
        }
      }
    }
  }
}
}

```

I got the error `RequestError: RequestError(400, 'x_content_parse_exception', "[1:99] [script] source doesn't support values of type: START_ARRAY") `

---

<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 19, 2022, 7:03pm UTC](https://discuss.elastic.co/t/how-to-add-suggestion-inside-term-query-in-dsl/309893/6 "2022-07-19T19:03:38Z")

</div>

Try this:

```auto
{
  "suggest": {
    "text": "schin",
    "simple_phrase": {
      "phrase": {
        "field": "name",
        "collate": {
          "query": {
            "source": {
              "bool": {
                "filter": [
                  {
                    "terms": {
                      "var.keyword": [
                        "sports"
                      ]
                    }
                  },
                  {
                    "terms": {
                      "type.keyword": [
                        "cricket"
                      ]
                    }
                  }
                ]
              }
            }
          },
          "prune": "false"
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![sim\_elastic](https://avatars.discourse-cdn.com/v4/letter/s/c77e96/32.png) [@sim\_elastic](https://discuss.elastic.co/u/sim_elastic)\
**Post date:** [July 20, 2022, 8:51am UTC](https://discuss.elastic.co/t/how-to-add-suggestion-inside-term-query-in-dsl/309893/7 "2022-07-20T08:51:06Z")

</div>

This is not exact solution i required with respect to question, i will say more nearer solution, But thanks. I understood that "suggest" cannot done with inside terms. Correct me if i am wrong

---

<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 17, 2022, 8:51am UTC](https://discuss.elastic.co/t/how-to-add-suggestion-inside-term-query-in-dsl/309893/8 "2022-08-17T08:51:10Z")

</div>

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