How can I remove duplicated keyword with the same category in ElasticSearch?

I want to build completion suggester in ElasticSearch.

I have data look like this.

{
    "suggest": {
        "input": ["iphone", "iphone x"]
    },
    "category_id": 1 
},
{
    "suggest": {
        "input": ["iphone", "iphone 8"]
    },
    "category_id": 2
},
{
    "suggest": {
        "input": ["iphone", "iphone 7"]
    },
    "category_id": 1 
}

And when I search with "iphone" keyword I want it checks duplicated keywords with different category_id.

This is result that I want to see.

{
    "suggest": iphone
    "category": 1
},
{
    "suggest": iphone
    "category": 2
}

If I use skip_duplicates feature, it will returns me only one record because "iphone" are duplicated.

Thanks for your helps.

You can encode "category" in the name, like ["iphone:c1", "iphone 8:c1"]

Or make 2 suggest queries for each category_id and merge them on the client.

1 Like

Encoding "category id" into keyword that's a good idea! Thanks you very much^^

But if I make 2 suggest queries for each category_id and merge them on the client that means I have to query 2 times, right? If I have 100 categories, I think that is not good to do this approach! or I misunderstand about this?

You can make multiple queries in 1 request but yes.

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