gpt4 book ai didi

json - Elasticsearch中的AND查询

转载 作者:行者123 更新时间:2023-12-02 23:32:14 25 4
gpt4 key购买 nike

我正在尝试按2个字段过滤查询,但始终会出错。我正在使用Elasticsearch文档建议的AND查询(实际上是“ bool(boolean) ”查询),在这里-
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-post-filter.html

GET /index_v1/user/_search
{
"query": {
"bool": {
"filter": {
{ "term": { "id": "101" }},
{ "term": { "firstName": "John" }}
}
}
}
}

这有效-
    GET /index_v1/user/_search
{
"query": {
"filtered": {
"query": {
"match": {
"id": "101"
}
}
}
}
}

并返回此-
    {
"took": 24,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 3.442347,
"hits": [
{
"_index": "index_v1",
"_type": "user",
"_id": "1",
"_score": 3.442347,
"_source": {
"id": "101",
"firstName": "John",
"guid": "1001",
"lastName": "Doe",
"email": "john.doe@company.com",
"entitlements": {
"id": "en2"
}
}
},
{
"_index": "index_v1",
"_type": "user",
"_id": "2",
"_score": 3.140066,
"_source": {
"id": "101",
"firstName": "John",
"guid": "1001",
"lastName": "Doe",
"email": "john.doe@company.com",
"tenants": [
{
"id": "12345",
"roles": [
"PrimaryAdmin"
]
}
],
"entitlements": {
"id": "en2"
}
}
}
]
}
}

这是映射文件-
{
"index_v1": {
"mappings": {
"user": {
"properties": {
"email": {
"type": "string"
},
"entitlements": {
"properties": {
"id": {
"type": "string"
}
}
},
"firstName": {
"type": "string"
},
"guid": {
"type": "string"
},
"id": {
"type": "string"
},
"lastName": {
"type": "string"
},
"tenants": {
"properties": {
"id": {
"type": "string"
},
"roles": {
"type": "string"
}
}
}
}
}
}
}
}

另外,如何将其添加到AND条件
["tenants"]["id"]="12345"

最佳答案

您必须运行过滤查询才能使用过滤器。您需要的相关示例是here

GET /index_v1/user/_search
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"and": [
{ "term": { "id": "101" }},
{ "term": { "firstName": "John" }},
{ "term": { "tenants.id": "12345" }}
]
}
}
}
}

大致上应该如此,不过我敢肯定您必须对其进行调整(我有些生疏了)。

为了使id字段完全匹配,您需要将那些字段设置为在映射中作为关键字进行分析,否则ES会尝试使其变得聪明,并为您带来意想不到的结果。

关于json - Elasticsearch中的AND查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35122467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com