Home > Software design >  Ranking the multiple categories in the search result
Ranking the multiple categories in the search result

Time:10-08

I have a problem with rating different categories when displaying search results, I hope you can help me. Let's say I searched "shoes" and this terms is exist in different cat: Shoes in men's shoes Shoes in women's shoes Shoes in children's shoes Shoes in sport's shoes

I am going to display maximum 3 categories in the search result. in Elasticsearch how can I rate them? or What factors are used for scoring?

CodePudding user response:

  "size": 3,
  "query": {
    "match": {
      "categories": "shoe"
    }
  }
}

CodePudding user response:

If you can provide more information about your data or at least data fields in your elastic index community can help you more effectively. Since there is no more info available about your data/index I am assuming two fields in your index one is product_name which may contain product name with other details and another one is category which will of course contain from which category product belongs. For example -

{
"product_name" : "shoes - Midnight Blue"
"category" : ["Women's Shoes", "Women's Clothing"]    ----> This document belongs to women's category. 
}

Now as per my understanding you are looking to categories above product_name in respective category and want to provide some weight. So below query will fit for you. Please test against your index and suggest here.

GET ecommerce_data/_search
{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "product_name": "shoes"
        }
      },
      "functions": [
        {
          "filter": {
            "match":{
              "category":"children's"
            }
          },
          "weight": 3
        },
        {
          "filter": {                       -----> Above document will match this filter
            "match":{
              "category":"women's"
            }
          },
          "weight": 2
        },
        {
          "filter": {
            "match":{
              "category":"men's"
            }
          },
          "weight": 1
        }
      ],
      "score_mode": "max",
      "boost_mode": "replace"
    }
  },
  "_source": ["product_name","category"]    -----> Verify with limited fields.
}
  • Related