如何在具有属性层次结构的 DSL 查询中添加权重
How to add weightage in DSL query with hierarchy of attributes
- 我需要搜索更多权重
professor.name
- 那么下一个获得权重的属性是
professor.email
- 检查其他有搜索字符串的字段
下面是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 "
}
PUT /data/test/3
{
"name": "Accounting 101",
"room": "E3",
"professor": {
"name": "Sachin Baszo",
"email": "Thomas@onuni.com"
},
"students_enrolled": 27,
"course_description": "Nothing"
}
下面是查询
GET /_search
{
"query": {
"query_string": {
"query": "(*Thomas*)"
}
}
}
我的输出将显示第二个文档,因为它在描述中包含 4 次“Thomas”
- 我需要给
professor.name
更多的权重,它应该先显示检查如果没有然后检查“professor.email”然后检查其他属性
预期结果是 1,3,2
1 是因为姓名,3 是因为电子邮件,2 是因为描述
Python
es.search(index="data", body={"query": {"query_string": {"query": "(*Thomas*)"}}})
添加包含索引数据、搜索查询和搜索结果的工作示例
boost can be applied to the individual fields as well while creating the index as explained in the
索引数据:
{
"id": "Accounting 101",
"room": "E3",
"professor": {
"name": "Thomas Baszo",
"email": "baszot@onuni.com"
},
"students_enrolled": 27,
"course_description": " financial statements"
}
{
"name": "Accounting 101",
"room": "E3",
"professor": {
"name": "Sachin Baszo",
"email": "baszot@onuni.com"
},
"students_enrolled": 27,
"course_description": "Thomas Thomas Thomas Thomas "
}
{
"name": "Accounting 101",
"room": "E3",
"professor": {
"name": "Baszo",
"email": "Thomas@onuni.com"
},
"students_enrolled": 27,
"course_description": "Nothing"
}
搜索查询:
{
"query": {
"function_score": {
"query": {
"multi_match": {
"query": "Thomas",
"fields": [
"professor.name^16",
"professor.email^8",
"course_description^4"
]
}
},
"boost_mode": "multiply"
}
}
}
搜索结果:
"hits": [
{
"_index": "stof_63933144",
"_type": "_doc",
"_id": "1",
"_score": 14.506382,
"_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": "3",
"_score": 7.846633,
"_source": {
"name": "Accounting 101",
"room": "E3",
"professor": {
"name": "Baszo",
"email": "Thomas@onuni.com"
},
"students_enrolled": 27,
"course_description": "Nothing"
}
},
{
"_index": "stof_63933144",
"_type": "_doc",
"_id": "2",
"_score": 5.9089565,
"_source": {
"name": "Accounting 101",
"room": "E3",
"professor": {
"name": "Sachin Baszo",
"email": "baszot@onuni.com"
},
"students_enrolled": 27,
"course_description": "Thomas Thomas Thomas Thomas "
}
}
]
更新 1:
要同时搜索 bor Thomas OR Sachin
,您可以使用以下查询:
{
"query": {
"function_score": {
"query": {
"multi_match": {
"query": "Thomas Sachin",
"fields": [
"professor.name^16",
"professor.email^8",
"course_description^4"
],
"operator": "OR",
"type": "cross_fields"
}
},
"boost_mode": "multiply"
}
}
}
- 我需要搜索更多权重
professor.name
- 那么下一个获得权重的属性是
professor.email
- 检查其他有搜索字符串的字段
下面是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 "
}
PUT /data/test/3
{
"name": "Accounting 101",
"room": "E3",
"professor": {
"name": "Sachin Baszo",
"email": "Thomas@onuni.com"
},
"students_enrolled": 27,
"course_description": "Nothing"
}
下面是查询
GET /_search
{
"query": {
"query_string": {
"query": "(*Thomas*)"
}
}
}
我的输出将显示第二个文档,因为它在描述中包含 4 次“Thomas”
- 我需要给
professor.name
更多的权重,它应该先显示检查如果没有然后检查“professor.email”然后检查其他属性
预期结果是 1,3,2
1 是因为姓名,3 是因为电子邮件,2 是因为描述
Python
es.search(index="data", body={"query": {"query_string": {"query": "(*Thomas*)"}}})
添加包含索引数据、搜索查询和搜索结果的工作示例
boost can be applied to the individual fields as well while creating the index as explained in the
索引数据:
{
"id": "Accounting 101",
"room": "E3",
"professor": {
"name": "Thomas Baszo",
"email": "baszot@onuni.com"
},
"students_enrolled": 27,
"course_description": " financial statements"
}
{
"name": "Accounting 101",
"room": "E3",
"professor": {
"name": "Sachin Baszo",
"email": "baszot@onuni.com"
},
"students_enrolled": 27,
"course_description": "Thomas Thomas Thomas Thomas "
}
{
"name": "Accounting 101",
"room": "E3",
"professor": {
"name": "Baszo",
"email": "Thomas@onuni.com"
},
"students_enrolled": 27,
"course_description": "Nothing"
}
搜索查询:
{
"query": {
"function_score": {
"query": {
"multi_match": {
"query": "Thomas",
"fields": [
"professor.name^16",
"professor.email^8",
"course_description^4"
]
}
},
"boost_mode": "multiply"
}
}
}
搜索结果:
"hits": [
{
"_index": "stof_63933144",
"_type": "_doc",
"_id": "1",
"_score": 14.506382,
"_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": "3",
"_score": 7.846633,
"_source": {
"name": "Accounting 101",
"room": "E3",
"professor": {
"name": "Baszo",
"email": "Thomas@onuni.com"
},
"students_enrolled": 27,
"course_description": "Nothing"
}
},
{
"_index": "stof_63933144",
"_type": "_doc",
"_id": "2",
"_score": 5.9089565,
"_source": {
"name": "Accounting 101",
"room": "E3",
"professor": {
"name": "Sachin Baszo",
"email": "baszot@onuni.com"
},
"students_enrolled": 27,
"course_description": "Thomas Thomas Thomas Thomas "
}
}
]
更新 1:
要同时搜索 bor Thomas OR Sachin
,您可以使用以下查询:
{
"query": {
"function_score": {
"query": {
"multi_match": {
"query": "Thomas Sachin",
"fields": [
"professor.name^16",
"professor.email^8",
"course_description^4"
],
"operator": "OR",
"type": "cross_fields"
}
},
"boost_mode": "multiply"
}
}
}