Elasticsearch:多前缀和多字段搜索的问题



希望有人能对我有所启发。假设我有以下数据:

{ "index": { "_index": "courses_test", "_id": 1 } }
{ "Course Name": "Bachelor of Arts in Music", "Job Role": "Theatre & Media Director, Video Engineer" }
{ "index": { "_index": "courses_test", "_id": 2 } }
{ "Course Name": "Bachelor of Arts in Engineering", "Job Role": "Graduate policy officer, editorial assistant, communications and campaigns assistant, assistant advocacy officer, employment consultant." }

我的目标是匹配";单身汉;"与";工程;在其课程名称和工作角色字段中。对于下面的查询,不太确定为什么返回了2个课程,但文档ID 2不满足条件。

如果我在";课程名称";只有在";工作角色"返回0,这也是正确的。

我使用的是查询字符串和*,这样即使用户只是键入前缀,例如"bach-eng",它也应该匹配。

完整查询:

{
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "Bachelor* AND Engineer*",
"fields": [
"Course Name",
"Job Role"
]
}
}
]
}
}
}

响应:

{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 2.0,
"hits": [
{
"_index": "courses_test",
"_type": "_doc",
"_id": "1",
"_score": 2.0,
"_source": {
"Course Name": "Bachelor of Arts in Music",
"Job Role": "Theatre & Media Director, Video Engineer"
}
},
{
"_index": "courses_test",
"_type": "_doc",
"_id": "2",
"_score": 2.0,
"_source": {
"Course Name": "Bachelor of Arts in Engineering",
"Job Role": "Graduate policy officer, editorial assistant, communications and campaigns assistant, assistant advocacy officer, employment consultant"
}
}
]
}
}

谢谢你的帮助!

查询字符串查询将为您提供的每个字段将查询扩展为OR查询。请看这里。最后,所有文档都将匹配,在任何字段中至少有一个匹配项。

您可能需要使用https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html和/或https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html


对于未来的调试:有一个API端点能够解释文档匹配的原因:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-explain.html

在您的情况下,这应该会给您提供相关的见解(请注意url中的索引名称和文档id(:

GET /courses_test/_explain/1  
{
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "Bachelor* AND Engineer*",
"fields": [
"Course Name",
"Job Role"
]
}
}
]
}
}
}

最新更新