How to add suggestion inside term query in DSL

My DOc is below

[
{'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

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

Hi @sim_elastic

You could use Phrase suggester to get the suggestions and collate 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:

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

@RabBit_BR But i need to add query_string also,

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

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

How to add one more term

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

when i try

 {
  "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")

Try this:

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

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

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