Elasticsearch 将多个字段与 AND 运算符匹配不起作用
Elasticsearch match multiple fields with AND operator not working
我正在尝试使用 AND 运算符从具有多个字段的 elasticsearch 中获取文档
对于以下查询,我期望得到以下结果
AB-7000-8002-W
但是我收到了这条错误消息Unrecognized token 'get': was expecting ('true', 'false' or 'null')\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@40d2a7e8; line: 1, column: 5]
get my_index12/_search {
"query" : {
"bool": {
"should": [
{
"match": {
"code": {
"query": "AB-5000-6002-AK",
"operator": "and"
}
}
},
{
"match": {
"locale": {
"query": "en_US",
"operator": "and"
}
}
}
]
}
}
}
请在下面找到我的索引文件
{
"_index": "my_index12",
"_type": "doc",
"_id": "2",
"_score": 1,
"_source": {
"code": "AB-7000-8002-W",
"locale": "en_US"
}
},
{
"_index": "my_index12",
"_type": "doc",
"_id": "4",
"_score": 1,
"_source": {
"code": "AB-7000-8002-W",
"locale": "en_EU"
}
},
{
"_index": "my_index12",
"_type": "doc",
"_id": "1",
"_score": 1,
"_source": {
"code": "sG66tsdF",
"locale": "en_US"
}
},
{
"_index": "my_index12",
"_type": "doc",
"_id": "3",
"_score": 1,
"_source": {
"code": "AB-7000-6002-WK",
"locale": "en_EU"
}
只需将 get my_index12/_search {
行中的花括号移动到下一行即可。
它应该有效。
为了获得同时满足这两个条件的结果,您必须使用 must
子句而不是 should
。 match
查询中的 "AND" 运算符不适用于您想要实现的用例。使用以下查询。
{
"query": {
"bool": {
"must": [
{
"match": {
"code": {
"query": "TE-7000-8002-W",
"operator": "and"
}
}
},
{
"match": {
"locale": {
"query": "en_US",
"operator": "and"
}
}
}
]
}
}
}
对于那些在多个字段上寻找 AND 之类的查询应该可行。下面将搜索代码为“TE-7000-8002-W”,区域设置为 en_US only
"query": {
"bool": {
"must": [
{
"match": {
"code": "TE-7000-8002-W"
}
},
{
"match": {
"locale": "en_US"
}
}
]
}
}
}
我正在尝试使用 AND 运算符从具有多个字段的 elasticsearch 中获取文档
对于以下查询,我期望得到以下结果
AB-7000-8002-W
但是我收到了这条错误消息Unrecognized token 'get': was expecting ('true', 'false' or 'null')\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@40d2a7e8; line: 1, column: 5]
get my_index12/_search {
"query" : {
"bool": {
"should": [
{
"match": {
"code": {
"query": "AB-5000-6002-AK",
"operator": "and"
}
}
},
{
"match": {
"locale": {
"query": "en_US",
"operator": "and"
}
}
}
]
}
}
}
请在下面找到我的索引文件
{
"_index": "my_index12",
"_type": "doc",
"_id": "2",
"_score": 1,
"_source": {
"code": "AB-7000-8002-W",
"locale": "en_US"
}
},
{
"_index": "my_index12",
"_type": "doc",
"_id": "4",
"_score": 1,
"_source": {
"code": "AB-7000-8002-W",
"locale": "en_EU"
}
},
{
"_index": "my_index12",
"_type": "doc",
"_id": "1",
"_score": 1,
"_source": {
"code": "sG66tsdF",
"locale": "en_US"
}
},
{
"_index": "my_index12",
"_type": "doc",
"_id": "3",
"_score": 1,
"_source": {
"code": "AB-7000-6002-WK",
"locale": "en_EU"
}
只需将 get my_index12/_search {
行中的花括号移动到下一行即可。
它应该有效。
为了获得同时满足这两个条件的结果,您必须使用 must
子句而不是 should
。 match
查询中的 "AND" 运算符不适用于您想要实现的用例。使用以下查询。
{
"query": {
"bool": {
"must": [
{
"match": {
"code": {
"query": "TE-7000-8002-W",
"operator": "and"
}
}
},
{
"match": {
"locale": {
"query": "en_US",
"operator": "and"
}
}
}
]
}
}
}
对于那些在多个字段上寻找 AND 之类的查询应该可行。下面将搜索代码为“TE-7000-8002-W”,区域设置为 en_US only
"query": {
"bool": {
"must": [
{
"match": {
"code": "TE-7000-8002-W"
}
},
{
"match": {
"locale": "en_US"
}
}
]
}
}
}