如何为DSL查询搜索字符串中的一个属性赋予更多权重

问题描述

以下是Elasticsearch中的样本数据

   PUT /data/test/1
 {
       "id": "Accounting 101","room": "E3","professor": {
           "name": "Thomas Baszo","email": "baszot@onuni.com"
           },"students_enrolled": 27,"course_description": " financial statements"
   }
   
   PUT /data/test/2
   {
       "name": "Accounting 101","professor": {
           "name": "Sachin Baszo","course_description": "Thomas  Thomas Thomas Thomas "
   }

下面是查询

GET /_search
{
  "query": {
    "query_string": {
      "query": "(*Thomas*)"
    }
  }
}

我的输出将第二个文档显示为第一个文档,因为它在描述中包含“ Thomas” 4次

  • 我需要为professor.name赋予更大的权重,它应该首先显示是否检查,然后检查“ professor.email”,然后检查其他属性

Python

es.search(index="data",body={"query": {"query_string": {"query": "(*Thomas*)"}}})

解决方法

不建议使用ES official documentation:

中提到的query_string

由于它针对任何无效语法返回错误,因此不建议 使用query_string查询搜索框。

如果您不需要支持查询语法,请考虑使用匹配项 查询。如果您需要查询语法的功能,请使用 simple_query_string查询,不太严格。

您可以在其中使用Boost

单个字段可以自动增强-向 相关分数—查询时

添加带有索引映射,搜索查询和搜索结果的工作示例

索引映射:

{
    "mappings": {
        "properties": {
            "professor": {
                "properties": {
                    "name": {
                        "type": "text","boost": 2
                    }
                }
            }
        }
    }
}

搜索查询:

 {
  "query": {
    "multi_match" : {
      "query": "Thomas","fields": [ "course_description","professor.name" ] 
    }
  }
}

搜索结果:

"hits": [
            {
                "_index": "stof_63933144","_type": "_doc","_id": "1","_score": 1.3862942,<-- note this
                "_source": {
                    "id": "Accounting 101","room": "E3","professor": {
                        "name": "Thomas Baszo","email": "baszot@onuni.com"
                    },"students_enrolled": 27,"course_description": " financial statements"
                }
            },{
                "_index": "stof_63933144","_id": "2","_score": 1.1090355,<-- note this
                "_source": {
                    "name": "Accounting 101","professor": {
                        "name": "Sachin Baszo","course_description": "Thomas  Thomas Thomas Thomas "
                }
            }
        ]

更新1:

用于搜索ThomasSachin的搜索查询

 {
      "query": {
        "multi_match" : {
          "query": "(Thomas) OR (Sachin)","professor.name" ] 
        }
      }
    }

更新2:

使用"operator":"OR"

的多重匹配查询
{
  "query": {
    "multi_match" : {
      "query": "Thomas Sachin","professor.name" ],"operator":"OR","type":"cross_fields"
    }
  }
}