Elasticsearch。试图找出合适的搜索查询算法。

问题描述 投票:0回答:1

我希望从弹性搜索中得到一个结果,输入的搜索字符串与文档中的多个字段相匹配,并且从索引中列出的结果是在列表中字段匹配记录的最大数量显示在前,字段匹配记录的最小数量显示在后。

例如:如果我搜索关键字 "test",而我在索引中的一条记录中有超过12个字段。

现在,如果test在1条记录中匹配了10个字段,那么在另一条记录中匹配了6个字段,然后在另一条记录中匹配了2个字段。

我想在列表中以最大字段匹配数到最小字段匹配数显示第一条记录。

根据这个例子,第一条记录显示10个字段匹配搜索字符串,第二条显示6个字段匹配,第三条显示2个字段匹配,然后继续......

如果能得到一些好的建议或例子,那就更好了。

php elasticsearch elastic-stack elk elasticsearch-6.8
1个回答
1
投票

这是elasticsearch的默认行为。匹配数越多的文档得分越高。

查询。

{
  "query": {
    "query_string": {
      "default_field": "*", -->search in all fields
      "query": "test"
    }
  }
}

结果:

{
        "_index" : "index18",
        "_type" : "_doc",
        "_id" : "iSCe6nEB8J88APx3YBGn",
        "_score" : 0.9808291,  --> scored higher as two fields match
        "_source" : {
          "field1" : "test",
          "field2" : "test"
        }
      },
      {
        "_index" : "index18",
        "_type" : "_doc",
        "_id" : "iiCe6nEB8J88APx3ghF-",
        "_score" : 0.4700036,
        "_source" : {
          "field1" : "test",
          "field2" : "abc"
        }
      }
© www.soinside.com 2019 - 2024. All rights reserved.