elasticsearch 查询精度和调用与排序

hgtggwj0  于 2023-10-17  发布在  ElasticSearch
关注(0)|答案(1)|浏览(142)

我有一个产品目录,当我试图按其他标准而不是按相关性对结果进行排序时,我遇到了一些问题。
今天,我可以按顺序对结果进行排序:最新和最受欢迎的。
我的查询具有在结果的第一页具有最准确的结果的特点,而其他页面是召回的结果。
当我尝试按“最高评级”标准排序结果时,第一页上的结果与搜索词无关,但返回文档,因为文档中存在搜索词的一个词。
索引数据:

POST idx_test/_bulk
{"index": {}}
{"name": "course english advance", "rating": 2 }
{"index": {}}
{"name": "course english mediujm", "rating": 4 }
{"index": {}}
{"name": "english by teacher", "rating": 2 }
{"index": {}}
{"name": "course makeup", "rating": 5 }
{"index": {}}
{"name": "course craftwork ", "rating": 2 }

Search for:中文

GET idx_test/_search
{
  "query": {
    "match": {
      "name": "course english"
    }
  },
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    }
  ]
}

结果如下:

"hits": [
      {
        "_index": "idx_test",
        "_id": "YQqH8YkBOnuUjQMmwaIc",
        "_score": 0.7777306,
        "_source": {
          "name": "course english advance",
          "rating": 2
        }
      },
      {
        "_index": "idx_test",
        "_id": "YgqH8YkBOnuUjQMmwaIc",
        "_score": 0.7777306,
        "_source": {
          "name": "course english mediujm",
          "rating": 4
        }
      },
      {
        "_index": "idx_test",
        "_id": "YwqH8YkBOnuUjQMmwaIc",
        "_score": 0.5070823,
        "_source": {
          "name": "english by teacher",
          "rating": 2
        }
      },
      {
        "_index": "idx_test",
        "_id": "ZAqH8YkBOnuUjQMmwaIc",
        "_score": 0.3176721,
        "_source": {
          "name": "course makeup",
          "rating": 5
        }
      },
      {
        "_index": "idx_test",
        "_id": "ZQqH8YkBOnuUjQMmwaIc",
        "_score": 0.3176721,
        "_source": {
          "name": "course craftwork ",
          "rating": 2
        }
      }
    ]

如果我按最佳评级排序,结果是:

GET idx_test/_search
{
  "query": {
    "match": {
      "name": "course english"
    }
  },
  "sort": [
    {
      "rating": {
        "order": "desc"
      }
    },
    {
      "_score": {
        "order": "desc"
      }
    }
  ]
}

结果如下:

"hits": [
      {
        "_index": "idx_test",
        "_id": "ZAqH8YkBOnuUjQMmwaIc",
        "_score": 0.3176721,
        "_source": {
          "name": "course makeup",
          "rating": 5
        },
        "sort": [
          5,
          0.3176721
        ]
      },
      {
        "_index": "idx_test",
        "_id": "YgqH8YkBOnuUjQMmwaIc",
        "_score": 0.7777306,
        "_source": {
          "name": "course english mediujm",
          "rating": 4
        },
        "sort": [
          4,
          0.7777306
        ]
      },
      {
        "_index": "idx_test",
        "_id": "YQqH8YkBOnuUjQMmwaIc",
        "_score": 0.7777306,
        "_source": {
          "name": "course english advance",
          "rating": 2
        },
        "sort": [
          2,
          0.7777306
        ]
      },
      {
        "_index": "idx_test",
        "_id": "YwqH8YkBOnuUjQMmwaIc",
        "_score": 0.5070823,
        "_source": {
          "name": "english by teacher",
          "rating": 2
        },
        "sort": [
          2,
          0.5070823
        ]
      },
      {
        "_index": "idx_test",
        "_id": "ZQqH8YkBOnuUjQMmwaIc",
        "_score": 0.3176721,
        "_source": {
          "name": "course craftwork ",
          "rating": 2
        },
        "sort": [
          2,
          0.3176721
        ]
      }
    ]

有没有人有任何指导意见,以获得排序只在有意义的文件与检索词?
我已经尝试过有一个查询,只是精确,但我需要检索文件,可能是相关的,与此排序失败在许多搜索。

s8vozzvw

s8vozzvw1#

一个field_value_factorfunction score query可以帮助你实现你想要的。

GET idx_test/_search
{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "name": "course english"
        }
      },
      "field_value_factor": {
        "field": "rating",
        "factor": 0.1,
        "modifier": "none"
      },
      "boost_mode": "sum"
    }
  },
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    }
  ]
}

相关问题