如何将简单查询字符串与布尔/必须组合



我有这个针对 ES 版本 7 的 ElasticSearch 查询:

{
"from": 0,
"simple_query_string": {
"query": "*"
},
"query": {
"bool": {
"must": [
{
"term": {
"organization_id": "fred"
}
},
{
"term": {
"assigned_user_id": "24584080"
}
}
]
}
},
"size": 50,
"sort": {
"updated": "desc"
},
"terminate_after": 50,
}

但是ES给了我这个错误:

原因:[simple_query_string] 中START_OBJECT的未知键

我的目标是能够对多个字段使用查询字符串,并且还可以使用带有布尔/必须的术语/匹配。 我应该放弃查询字符串而只使用bool.must[{match:"my query"}]吗?

您可以使用 bool 以这种方式组合多个查询。must 子句将作为逻辑 AND 工作,并确保所有条件都匹配。

您需要在查询部分中包含simple_query_string

添加带有示例文档和搜索查询的工作示例。

索引样本数据

{
"organization_id": 1,
"assigned_user_id": 2,
"title": "welcome"
}{
"organization_id": 2,
"assigned_user_id": 21,
"title": "hello"
}{
"organization_id": 3,
"assigned_user_id": 22,
"title": "hello welocome"
}

搜索查询 :

{
"query": {
"bool": {
"must": [
{
"simple_query_string": {
"fields" : ["title"],
"query" : "welcome"
}
},
{
"match": {
"organization_id": "1"
}
},
{
"match": {
"assigned_user_id": "2"
}
}
]
}
}
}

搜索结果:

"hits": [
{
"_index": "my_index",
"_type": "_doc",
"_id": "1",
"_score": 3.0925694,
"_source": {
"organization_id": 1,
"assigned_user_id": 2,
"title": "welcome"
}
}
]

最新更新