【发布时间】:2021-06-21 08:35:51
【问题描述】:
希望有人能在这方面启发我。假设我有以下数据:
{ "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"
}
}
]
}
}
感谢您的帮助!
【问题讨论】:
-
所以我在这里问了这个问题:discuss.elastic.co/t/multi-fields-multi-prefixes-search/276504,只是为了更新这个问题,我找到了满足我目标的解决方案。在下面从@ibexit re AND 到 OR 的解释之后。我想过将每个字段的查询分开,并使用“应该”而不是“必须”。
标签: elasticsearch query-string