在 Elasticsearch 中提升元组

Boosting the tuple in Elasticsearch

我在 Elasticsearch 文档中有如下服装数据。它包含产品名称、颜色和 material 信息

/products/data
{
   product_name: "xxxxxx",
   color: "C1",
   material: "M1"
},
{
   product_name: "yyyyyy",
   color: "C2",
   material: "M2"
},
{
   product_name: "zzzzzz",
   color: "C1",
   material: "M2"
}...

现在我想用 color: C1material: M2 获取 10 产品。如果该组合中没有足够的产品,我想使用 color: C1material: M1 获取产品。我将使用 colormaterial 的组合。在这个例子中,我对组合C1, M2的优先级最高,次高的是C1, M1,那么它可能是C2, M3等等。基于我想提升结果的优先级(在单个查询中)。这在 Elasticsearch 中可能吗?

我希望返回的文档如下所示

    {
       _score: highest_score,
       product_name: "xxxxxx",
       color: "C1",
       material: "M2"
    },
    {
       _score: sencond_highest
       product_name: "yyyyyy",
       color: "C1",
       material: "M1"
    },
    {
       _score: third_highest
       product_name: "zzzzzz",
       color: "C2",
       material: "M3"
    }

重现问题的示例数据集

 POST mat/product
    {
     "pname": "prod1",
     "color": "c1",
     "material": "m1"
    }
    POST mat/product
    {
     "pname": "prod2",
     "color": "c2",
     "material": "m2"
    }
    POST mat/product
    {
     "pname": "prod3",
     "color": "c3",
     "material": "m3"
    }
    POST mat/product
    {
     "pname": "prod4",
     "color": "c1",
     "material": "m2"
    }
    POST mat/product
    {
     "pname": "prod5",
     "color": "c2",
     "material": "m2"
    }
    POST mat/product
    {
     "pname": "prod6",
     "color": "c3",
     "material": "m3"
    }

  GET mat/product/_search
  {
  "query":{
    "bool":{
      "minimum_should_match":1,
      "should":[
        {"bool":{
          "boost":181,
          "must":[{"match":{"color":"c3"}},{"match":{"material":"m3"}}]
        }},
        {"bool":{
          "boost":180,
          "must":[{"match":{"color":"c2"}},{"match":{"material":"m2"}}]
        }},
        {"bool":{
          "boost":179,
          "must":[{"match":{"color":"c1"}},{"match":{"material":"m2"}}]
        }}
]}},"size":10}

是的,您可以使用 bool/should 查询来为不同的 material/color 对提供不同的提升。

{
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "bool": {
            "boost": 4,
            "must": [
              {
                "match": {
                  "color": {
                    "value": "C1"
                  }
                }
              },
              {
                "match": {
                  "material": {
                    "value": "M1"
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "boost": 2,
            "must": [
              {
                "match": {
                  "color": {
                    "value": "C1"
                  }
                }
              },
              {
                "match": {
                  "material": {
                    "value": "M2"
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "boost": 1,
            "must": [
              {
                "match": {
                  "color": {
                    "value": "C2"
                  }
                }
              },
              {
                "match": {
                  "material": {
                    "value": "M3"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}