【问题标题】:ElasticSearch - query documents by term and field priorityElasticSearch - 按术语和字段优先级查询文档
【发布时间】:2021-03-02 15:56:25
【问题描述】:

我目前正在使用 elasticsearch,并且我正在尝试从 Java 后端实现一个查询,该查询将不仅按术语而且按字段优先级从我的索引中查询文档。在我的索引中,我的文档有一个术语和一个指定类型的字段。

e.g 
term: "Flu Shot"
type: "procedure"

term: "Fluphenazine"
type: "drug"

我创建了一个查询,它将按术语搜索,弹性索引将返回与该术语匹配的最相关的结果。我要创建的功能是创建一个查询以返回匹配相同术语但按“类型”字段的优先级排序的结果。例如,当我输入“流感”时,我想先获取类型为“程序”的文档,然后再获取类型为“药物”的文档。目前,由于许多药物以“flu”开头,因此该索引仅返回类型为“drugs”的文档。

【问题讨论】:

    标签: elasticsearch elastic-stack


    【解决方案1】:

    您可以使用function_score

    function_score 允许您修改查询检索到的文档的分数。要使用function_score,用户必须定义​​一个查询和一个或多个函数,为查询返回的每个文档计算一个新分数。

    以您的数据为例(使用 Elasticsearch server 7.9):

    1. 创建索引,添加文档

       PUT /example_index
       {
         "mappings": {
           "properties": {
             "term": {"type": "text" },
             "type": {"type": "keyword"}
           }
         }
       }
      
       PUT /_bulk
       {"create": {"_index": "example_index", "_id": 1}}
       {"term": "Flu Shot", "type": "procedure"}
       {"create": {"_index": "example_index", "_id": 2}}
       {"term": "Fluphenazine", "type": "drug"}
       {"create": {"_index": "example_index", "_id": 3}}
       {"term": "Flu Shot2", "type": "procedure"}
       {"create": {"_index": "example_index", "_id": 4}}
       {"term": "Fluphenazine2", "type": "drug"}
      
    2. 使用自定义评分逻辑查询文档

       GET /example_index/_search
       {
         "query": {
           "function_score": {
             "query": {
               "wildcard": {
                 "term": {
                   "value": "*flu*"
                 }
               }
             },
             "functions": [
               {
                 "filter": {
                   "term": {
                     "type": "procedure"
                   }
                 },
                 "weight": 2
               },
               {
                 "filter": {
                   "term": {
                     "type": "drug"
                   }
                 },
                 "weight": 1
               }
             ]
           }
         }
       }
      
    3. 结果:

       {
         "took" : 2,
         "timed_out" : false,
         "_shards" : {
           "total" : 1,
           "successful" : 1,
           "skipped" : 0,
           "failed" : 0
         },
         "hits" : {
           "total" : {
             "value" : 4,
             "relation" : "eq"
           },
           "max_score" : 2.0,
           "hits" : [
             {
               "_index" : "example_index",
               "_type" : "_doc",
               "_id" : "1",
               "_score" : 2.0,
               "_source" : {
                 "term" : "Flu Shot",
                 "type" : "procedure"
               }
             },
             {
               "_index" : "example_index",
               "_type" : "_doc",
               "_id" : "3",
               "_score" : 2.0,
               "_source" : {
                 "term" : "Flu Shot2",
                 "type" : "procedure"
               }
             },
             {
               "_index" : "example_index",
               "_type" : "_doc",
               "_id" : "2",
               "_score" : 1.0,
               "_source" : {
                 "term" : "Fluphenazine",
                 "type" : "drug"
               }
             },
             {
               "_index" : "example_index",
               "_type" : "_doc",
               "_id" : "4",
               "_score" : 1.0,
               "_source" : {
                 "term" : "Fluphenazine2",
                 "type" : "drug"
               }
             }
           ]
         }
       }
      

    您可以看到type 设置为procedure 的文档比type 设置为drug 的文档得分更高。这是因为我们为function_score 中的不同types 分配了不同的权重。

    【讨论】:

    • 如果我使用功能分数查询,这似乎是一个很好的解决方案,但是术语匹配查询呢?特别是那些依赖于自定义分析器的术语?我试图将它包装在 bool/must 查询中。它适用于 term + a type 但如果我想按优先顺序排列两个或多个类型怎么办?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    • 2022-01-03
    • 2022-12-13
    • 2021-07-13
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多