对特定字段的数据进行排序在弹性中首先给出不相关的数据

发布于 2025-01-11 06:59:42 字数 7376 浏览 0 评论 0原文

I want to search data in elastic search  version 5.2.1 such that the exact match term should come first and then all the relevant matches i.e fuzzy data.
The data needs to be sorted on the basis of relevancy (relevancy includes the creation date of item and then item inventory)

The query that i am trying is :

    GET dummy/_search
    {
      "track_scores": true,
      "from" : 0,
      "size" : 24,
      "query" : {
        "bool" : {
          "must" : [
            {
              "bool" : {
                "should" : [
                  {
                    "match" : {
                      "itemNameWeb" : {
                        "query" : "swetashirt",
                        "operator" : "OR",
                        "analyzer" : "standard",
                        "prefix_length" : 0,
                        "max_expansions" : 50,
                        "minimum_should_match" : "66%",
                        "fuzzy_transpositions" : false,
                        "lenient" : false,
                        "zero_terms_query" : "NONE",
                        "boost" : 1.0
                      }
                    }
                  },
                  {
                    "match" : {
                      "itemNameWeb" : {
                        "query" : "swetashirt",
                        "operator" : "OR",
                        "analyzer" : "standard",
                        "fuzziness" : "1",
                        "prefix_length" : 0,
                        "max_expansions" : 50,
                        "minimum_should_match" : "66%",
                        "fuzzy_transpositions" : true,
                        "lenient" : false,
                        "zero_terms_query" : "NONE",
                        "boost" : 1.0
                      }
                    }
                  }
                ],
                "disable_coord" : false,
                "adjust_pure_negative" : true,
                "boost" : 1.0
              }
            }
          ],
          "disable_coord" : false,
          "adjust_pure_negative" : true,
          "boost" : 1.0
        }
      },
      "sort" : [
        {
          "createDate" : {
            "order" : "desc"
          }
        },
        {
          "itemTotalInventory" : {
            "order" : "desc"
          }
        }
      ]
    }

上面的查询没有给出预期的结果,因为排序导致的精确匹配会因为排序而下降

到目前为止尝试的解决方案与建议的解决方案添加了恒定分数:

GET dummy/_search
{
  "track_scores": true,
  "from": 0,
  "size": 1000,
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "constant_score": {
                  "filter": {
                    "bool": {
                      "should":{
                      "match": {
                        "itemNameWeb": {
                          "query": "fidato tshirt",
                          "operator": "OR",
                          "analyzer": "standard",
                          "prefix_length": 0,
                          "max_expansions": 50,
                          "minimum_should_match": "66%",
                          "fuzzy_transpositions": false,
                          "lenient": false,
                          "zero_terms_query": "NONE",
                          "boost": 1
                        }
                      }
                    }
                  }
                  },
                  "boost": 2
                }
              },
              {
                "constant_score": {
                  "filter": {
                    "bool": {
                       "should":{
                      "match": {
                        "itemNameWeb": {
                          "query": "fidato tshirt",
                          "operator": "OR",
                          "analyzer": "standard",
                          "fuzziness": "1",
                          "prefix_length": 0,
                          "max_expansions": 50,
                          "minimum_should_match": "66%",
                          "fuzzy_transpositions": true,
                          "lenient": false,
                          "zero_terms_query": "NONE",
                          "boost": 1
                        }
                      }
                    }
                    }
                  },
                  "boost": 1
                }
              }
            ],
            "disable_coord": false,
            "adjust_pure_negative": true,
            "boost": 1
          }
        }
      ],
      "disable_coord": false,
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "sort": [
    "_score",
    {
      "createDate.keyword": {
        "order": "desc"
      }
    },
    {
      "itemTotalInventory": {
        "order": "desc"
      }
    }
  ]
}

The sample output result from the above GET query 

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 65,
    "max_score": 3,
    "hits": [
      {
        "_index": "dummy",
        "_type": "search",
 
        "_score": 3,
        "_source": {
          "itemNameWeb": "Pack of 2 Men's Printed Full Sleeves Tshirt-M,
          "itemTotalInventory": 11,
          "createDate": "2022-01-10 15:22:47.0"
        },
        "sort": [
          3,
          "2022-01-10 15:22:47.0",
          11
        ]
      },
      {
        "_index": "dummy",
        "_type": "search",
        "_score": 3,
        "_source": {
        
          "itemNameWeb": "Pack of 3 Men's Half Sleeves Tshirt-M",
         "itemTotalInventory": 236,
         "createDate": "2022-01-10 15:22:34.0"
        },
        "sort": [
          3,
          "2022-01-10 15:22:34.0",
          236
        ]
      },
      {
        "_index": "dummy",
        "_type": "search",
        "_score": 3,
        "_source": {
      
          "itemNameWeb": "Greyson 5 Tshirts Size - XL with Track pants Size – XL",
          "itemTotalInventory": 60,

          "createDate": "2021-10-28 06:56:53.0"
        },
        "sort": [
          3,
          "2021-10-28 06:56:53.0",
          60
        ]
      },
      {
        "_index": "dummy",
        "_type": "search",
        "_score": 3,
        "_source": {
       
          "itemNameWeb": "Pack of 2 Shoes & Free 1 Slipper Size - 9 + 1 Wallet+ 2 Tshirts + 1 TrackPant Size - XL",
          "itemTotalInventory": 23,
          "createDate": "2021-10-20 06:22:08.0"
      },
      {
        "_index": "dummy",
        "_type": "search",
        "_score": 3,
        "_source": {
          "itemNameWeb": "Fidato Cordinated Polo Neck Solid Yellow T-Shirt",
          "itemTotalInventory": 4,
          "createDate": "2021-10-14 07:33:06.0"
        },
        "sort": [
          3,
          "2021-10-14 07:33:06.0",
          4
        ]
      },
      {
        "_index": "dummy",
        "_type": "search",
        "_score": 3,
        "_source": {
          "itemNameWeb": "Fidato Solid Round Neck Full Sleeve T-Shirt",
          "itemTotalInventory": 5,
          "createDate": "2021-10-14 07:17:32.0"
        },
        "sort": [
          3,
          "2021-10-14 07:17:32.0",
          5
        ]
      }

Fidato T 恤结果应该是第一位的,但它给出了 2 件装男士印花全袖 T 恤-M 作为最高结果。

I want to search data in elastic search  version 5.2.1 such that the exact match term should come first and then all the relevant matches i.e fuzzy data.
The data needs to be sorted on the basis of relevancy (relevancy includes the creation date of item and then item inventory)

The query that i am trying is :

    GET dummy/_search
    {
      "track_scores": true,
      "from" : 0,
      "size" : 24,
      "query" : {
        "bool" : {
          "must" : [
            {
              "bool" : {
                "should" : [
                  {
                    "match" : {
                      "itemNameWeb" : {
                        "query" : "swetashirt",
                        "operator" : "OR",
                        "analyzer" : "standard",
                        "prefix_length" : 0,
                        "max_expansions" : 50,
                        "minimum_should_match" : "66%",
                        "fuzzy_transpositions" : false,
                        "lenient" : false,
                        "zero_terms_query" : "NONE",
                        "boost" : 1.0
                      }
                    }
                  },
                  {
                    "match" : {
                      "itemNameWeb" : {
                        "query" : "swetashirt",
                        "operator" : "OR",
                        "analyzer" : "standard",
                        "fuzziness" : "1",
                        "prefix_length" : 0,
                        "max_expansions" : 50,
                        "minimum_should_match" : "66%",
                        "fuzzy_transpositions" : true,
                        "lenient" : false,
                        "zero_terms_query" : "NONE",
                        "boost" : 1.0
                      }
                    }
                  }
                ],
                "disable_coord" : false,
                "adjust_pure_negative" : true,
                "boost" : 1.0
              }
            }
          ],
          "disable_coord" : false,
          "adjust_pure_negative" : true,
          "boost" : 1.0
        }
      },
      "sort" : [
        {
          "createDate" : {
            "order" : "desc"
          }
        },
        {
          "itemTotalInventory" : {
            "order" : "desc"
          }
        }
      ]
    }

The query above is not giving expected result as the exact matches due to sorting goes down because of sorting

The solution tried so far from the proposed solution adding constant score:

GET dummy/_search
{
  "track_scores": true,
  "from": 0,
  "size": 1000,
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "constant_score": {
                  "filter": {
                    "bool": {
                      "should":{
                      "match": {
                        "itemNameWeb": {
                          "query": "fidato tshirt",
                          "operator": "OR",
                          "analyzer": "standard",
                          "prefix_length": 0,
                          "max_expansions": 50,
                          "minimum_should_match": "66%",
                          "fuzzy_transpositions": false,
                          "lenient": false,
                          "zero_terms_query": "NONE",
                          "boost": 1
                        }
                      }
                    }
                  }
                  },
                  "boost": 2
                }
              },
              {
                "constant_score": {
                  "filter": {
                    "bool": {
                       "should":{
                      "match": {
                        "itemNameWeb": {
                          "query": "fidato tshirt",
                          "operator": "OR",
                          "analyzer": "standard",
                          "fuzziness": "1",
                          "prefix_length": 0,
                          "max_expansions": 50,
                          "minimum_should_match": "66%",
                          "fuzzy_transpositions": true,
                          "lenient": false,
                          "zero_terms_query": "NONE",
                          "boost": 1
                        }
                      }
                    }
                    }
                  },
                  "boost": 1
                }
              }
            ],
            "disable_coord": false,
            "adjust_pure_negative": true,
            "boost": 1
          }
        }
      ],
      "disable_coord": false,
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "sort": [
    "_score",
    {
      "createDate.keyword": {
        "order": "desc"
      }
    },
    {
      "itemTotalInventory": {
        "order": "desc"
      }
    }
  ]
}

The sample output result from the above GET query 

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 65,
    "max_score": 3,
    "hits": [
      {
        "_index": "dummy",
        "_type": "search",
 
        "_score": 3,
        "_source": {
          "itemNameWeb": "Pack of 2 Men's Printed Full Sleeves Tshirt-M,
          "itemTotalInventory": 11,
          "createDate": "2022-01-10 15:22:47.0"
        },
        "sort": [
          3,
          "2022-01-10 15:22:47.0",
          11
        ]
      },
      {
        "_index": "dummy",
        "_type": "search",
        "_score": 3,
        "_source": {
        
          "itemNameWeb": "Pack of 3 Men's Half Sleeves Tshirt-M",
         "itemTotalInventory": 236,
         "createDate": "2022-01-10 15:22:34.0"
        },
        "sort": [
          3,
          "2022-01-10 15:22:34.0",
          236
        ]
      },
      {
        "_index": "dummy",
        "_type": "search",
        "_score": 3,
        "_source": {
      
          "itemNameWeb": "Greyson 5 Tshirts Size - XL with Track pants Size – XL",
          "itemTotalInventory": 60,

          "createDate": "2021-10-28 06:56:53.0"
        },
        "sort": [
          3,
          "2021-10-28 06:56:53.0",
          60
        ]
      },
      {
        "_index": "dummy",
        "_type": "search",
        "_score": 3,
        "_source": {
       
          "itemNameWeb": "Pack of 2 Shoes & Free 1 Slipper Size - 9 + 1 Wallet+ 2 Tshirts + 1 TrackPant Size - XL",
          "itemTotalInventory": 23,
          "createDate": "2021-10-20 06:22:08.0"
      },
      {
        "_index": "dummy",
        "_type": "search",
        "_score": 3,
        "_source": {
          "itemNameWeb": "Fidato Cordinated Polo Neck Solid Yellow T-Shirt",
          "itemTotalInventory": 4,
          "createDate": "2021-10-14 07:33:06.0"
        },
        "sort": [
          3,
          "2021-10-14 07:33:06.0",
          4
        ]
      },
      {
        "_index": "dummy",
        "_type": "search",
        "_score": 3,
        "_source": {
          "itemNameWeb": "Fidato Solid Round Neck Full Sleeve T-Shirt",
          "itemTotalInventory": 5,
          "createDate": "2021-10-14 07:17:32.0"
        },
        "sort": [
          3,
          "2021-10-14 07:17:32.0",
          5
        ]
      }

Fidato T-Shirt result should have come first but its giving Pack of 2 Men's Printed Full Sleeves Tshirt-M as top most result .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以酷 2025-01-18 06:59:42

如果您通过 constant_score 查询包装每个匹配查询,并给出精确匹配的 boost 值,该值高于模糊的 boost 值,那么如果您之前按 _score 排序您的排序标准,您就可以实现这一点。以下查询应该适合您:

{
  "track_scores": true,
  "from": 0,
  "size": 24,
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "constant_score": {
                  "filter": {
                    "bool": {
                      "match": {
                        "itemNameWeb": {
                          "query": "swetashirt",
                          "operator": "OR",
                          "analyzer": "standard",
                          "prefix_length": 0,
                          "max_expansions": 50,
                          "minimum_should_match": "66%",
                          "fuzzy_transpositions": false,
                          "lenient": false,
                          "zero_terms_query": "NONE",
                          "boost": 1
                        }
                      }
                    }
                  },
                  "boost": 2
                }
              },
              {
                "constant_score": {
                  "filter": {
                    "bool": {
                      "match": {
                        "itemNameWeb": {
                          "query": "swetashirt",
                          "operator": "OR",
                          "analyzer": "standard",
                          "fuzziness": "1",
                          "prefix_length": 0,
                          "max_expansions": 50,
                          "minimum_should_match": "66%",
                          "fuzzy_transpositions": true,
                          "lenient": false,
                          "zero_terms_query": "NONE",
                          "boost": 1
                        }
                      }
                    }
                  },
                  "boost": 1
                }
              }
            ],
            "disable_coord": false,
            "adjust_pure_negative": true,
            "boost": 1
          }
        }
      ],
      "disable_coord": false,
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "sort": [
    "_score",
    {
      "createDate": {
        "order": "desc"
      }
    },
    {
      "itemTotalInventory": {
        "order": "desc"
      }
    }
  ]
}

If you wrap each match query by a constant_score query and give exact match's boost value higher than the fuzzy's boost value then if you sort by _score before your sorting criteria, you can achieve this. Following query should work for you:

{
  "track_scores": true,
  "from": 0,
  "size": 24,
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "constant_score": {
                  "filter": {
                    "bool": {
                      "match": {
                        "itemNameWeb": {
                          "query": "swetashirt",
                          "operator": "OR",
                          "analyzer": "standard",
                          "prefix_length": 0,
                          "max_expansions": 50,
                          "minimum_should_match": "66%",
                          "fuzzy_transpositions": false,
                          "lenient": false,
                          "zero_terms_query": "NONE",
                          "boost": 1
                        }
                      }
                    }
                  },
                  "boost": 2
                }
              },
              {
                "constant_score": {
                  "filter": {
                    "bool": {
                      "match": {
                        "itemNameWeb": {
                          "query": "swetashirt",
                          "operator": "OR",
                          "analyzer": "standard",
                          "fuzziness": "1",
                          "prefix_length": 0,
                          "max_expansions": 50,
                          "minimum_should_match": "66%",
                          "fuzzy_transpositions": true,
                          "lenient": false,
                          "zero_terms_query": "NONE",
                          "boost": 1
                        }
                      }
                    }
                  },
                  "boost": 1
                }
              }
            ],
            "disable_coord": false,
            "adjust_pure_negative": true,
            "boost": 1
          }
        }
      ],
      "disable_coord": false,
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "sort": [
    "_score",
    {
      "createDate": {
        "order": "desc"
      }
    },
    {
      "itemTotalInventory": {
        "order": "desc"
      }
    }
  ]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文