如何为 DSL 查询搜索字符串中的一个属性赋予更多权重
How to give more weightage for one attributes in DSL query search string
下面是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
更多的权重,它应该先显示检查如果没有然后检查“professor.email”然后检查其他属性
Python
es.search(index="data", body={"query": {"query_string": {"query": "(*Thomas*)"}}})
不建议使用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:
用于搜索 Thomas
或 Sachin
的搜索查询
{
"query": {
"multi_match" : {
"query": "(Thomas) OR (Sachin)",
"fields": [ "course_description", "professor.name" ]
}
}
}
更新二:
使用 "operator":"OR"
的多重匹配查询
{
"query": {
"multi_match" : {
"query": "Thomas Sachin",
"fields": [ "course_description", "professor.name" ] ,
"operator":"OR",
"type":"cross_fields"
}
}
}
下面是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
更多的权重,它应该先显示检查如果没有然后检查“professor.email”然后检查其他属性
Python
es.search(index="data", body={"query": {"query_string": {"query": "(*Thomas*)"}}})
不建议使用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:
用于搜索 Thomas
或 Sachin
{
"query": {
"multi_match" : {
"query": "(Thomas) OR (Sachin)",
"fields": [ "course_description", "professor.name" ]
}
}
}
更新二:
使用 "operator":"OR"
{
"query": {
"multi_match" : {
"query": "Thomas Sachin",
"fields": [ "course_description", "professor.name" ] ,
"operator":"OR",
"type":"cross_fields"
}
}
}