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

2024-06-02 06:18:44 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是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",
       "room": "E3",
       "professor": {
           "name": "Sachin Baszo",
           "email": "baszot@onuni.com"
           },
       "students_enrolled": 27, 
       "course_description": "Thomas  Thomas Thomas Thomas "
   }

下面是查询

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

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

  • 我需要给professor.name赋予更多的权重,它应该首先显示check,如果没有,则检查“professor.email”,然后检查其他属性

Python

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


Tags: nametestcomdataputemailthomasquery
1条回答
网友
1楼 · 发布于 2024-06-02 06:18:44

不建议使用query_string,如ES official documentation:中所述

Because it returns an error for any invalid syntax, we don’t recommend using the query_string query for search boxes.

If you don’t need to support a query syntax, consider using the match query. If you need the features of a query syntax, use the simple_query_string query, which is less strict.

您可以在其中使用Boost

Individual fields can be boosted automatically — count more towards the relevance score — at query time

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

索引映射:

{
    "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",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.1090355,   <  note this
                "_source": {
                    "name": "Accounting 101",
                    "room": "E3",
                    "professor": {
                        "name": "Sachin Baszo",
                        "email": "baszot@onuni.com"
                    },
                    "students_enrolled": 27,
                    "course_description": "Thomas  Thomas Thomas Thomas "
                }
            }
        ]

更新1:

用于搜索ThomasSachin的搜索查询

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

更新2:

使用"operator":"OR"的多匹配查询

{
  "query": {
    "multi_match" : {
      "query": "Thomas Sachin", 
      "fields": [ "course_description", "professor.name" ] ,
      "operator":"OR",
      "type":"cross_fields"
    }
  }
}

相关问题 更多 >