gpt4 book ai didi

elasticsearch - Elasticsearch DSL查询嵌套字段和普通字段

转载 作者:行者123 更新时间:2023-12-03 02:20:28 25 4
gpt4 key购买 nike

我需要帮助来编写查询以过滤具有以下结构的文档

{
"enviID" : 123,
"empID" : 456,
"projects" : [{"id": 123, "name":"abc"},{"id": 456, "name":"xyz"}],
tests : [{"id": 999, "name":"xxx"},{"id": 000, "name":"yyy"}]
}
我想过滤
envId, empId, project.Id, tests.id
我也在检查映射文件,测试字段未映射,但文档具有此字段

最佳答案

i want to filter on envId, empId, project.Id, tests.id



由于未指定要应用于字段的任何特定过滤器,因此下面显示的搜索查询根据文档的Id 过滤文档

映射:
{
"mappings": {
"properties": {
"enviID": {
"type": "integer"
},
"empID": {
"type": "integer"
},
"projects": {
"type": "nested",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "text"
}
}
},
"tests": {
"type": "nested",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "text"
}
}
}
}
}
}

搜索查询
{
"query": {
"bool": {
"filter": [
{
"bool": {
"must": [
{
"term": {
"enviID": {
"value": "123"
}
}
},
{
"term": {
"empID": {
"value": "456"
}
}
},
{
"nested": {
"path": "projects",
"query": {
"bool": {
"must": [
{
"match": {
"projects.id": 123
}
}
]
}
}
}
},
{
"nested": {
"path": "tests",
"query": {
"bool": {
"must": [
{
"match": {
"tests.id": 999
}
}
]
}
}
}
}
]
}
}
]
}
}
}

搜索结果
"hits": [
{
"_index": "my_index",
"_type": "_doc",
"_id": "1",
"_score": 0.0,
"_source": {
"enviID": 123,
"empID": 456,
"projects": [
{
"id": 123,
"name": "abc"
},
{
"id": 456,
"name": "xyz"
}
],
"tests": [
{
"id": 999,
"name": "xxx"
},
{
"id": 0,
"name": "yyy"
}
]
}
}
]

您可以引用此书以进一步了解 Query-filter contextNested Query filter

关于elasticsearch - Elasticsearch DSL查询嵌套字段和普通字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62479590/

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