2

My query as below

{
  "suggest": {
    "text": "iphame",
    "0-title": {
      "phrase": {
        "field": "title",
        "query": {
          "term": {
            "country_id": 123
          }
        }
      }
    },
    "1-subtitle": {
      "phrase": {
        "field": "subtitle"
      }
    }
  }
}

I want the search suggestion filter based on country, but the query above caused parsing_exception.

How to make the query look for title only based on certain country?

| id | title                   | subtitle    | country_id |
|----|-------------------------|-------------|------------|
| 1  | Cheapest iPhone in town | Lorem ipsum | 123        |
| 2  | iOS 13 beta released    | Lorem ipsum | 25         |
| 3  | iPadOS public beta      | Lorem ipsum | 123        |

E.g. I don't want include the id = 2.

Js Lim
  • 3,625
  • 6
  • 42
  • 80
  • Hey Js Lim, were you able to find a solution for this problem? I'm also looking at something similar, and I even tried browsing the source code, but do not see a way to do this yet. – Varun Natraaj Jul 17 '19 at 12:08
  • @VarunNatraaj not figure out yet. I posted a [question here](https://discuss.elastic.co/t/suggester-filter-by-certain-condition/189647), only you reply. I guess probably this feature not yet available. – Js Lim Jul 19 '19 at 06:31
  • @VarunNatraaj and Js Lim, did you ever work out how to achieve this? – Brad Jun 08 '22 at 11:39

1 Answers1

0

if you mapped country_id as a number then:

{ 
"query": { 
    "match": { 
            "country_id": 123 
        } 
    }
} 

if you mapped country_id as a keyword then:

{ 
"query": { 
    "match": { 
            "country_id.keyword": "123" 
        } 
    }
} 
user7594840
  • 103
  • 6
  • 1
    For normal query result, this is workable. But I'm looking for `suggest`, the [Did you mean](https://qbox.io/blog/how-to-build-did-you-mean-feature-with-elasticsearch-phrase-suggester) feature. – Js Lim Jul 11 '19 at 02:15
  • Are you referring to something like FuzzySearch? – user7594840 Jul 11 '19 at 15:06
  • Is not fuzzy search. Fuzzy search only applicable to `query` if not mistaken. I just want to apply filter in **suggester** – Js Lim Jul 12 '19 at 01:43