Select Elasticsearch 中嵌套了任何提供的对象的文档 属性
Select documents in Elasticsearch that have ANY of the provided objects within it's nested property
在我的索引中,我有一个包含类型 "nested" 的 locations
属性 的映射。 属性 包含一个位置数组。每个位置都是具有以下结构的对象:
{
"id": string,
"type": string
}
在我的网站上,我想 select 多个位置并找到包含我 select 编辑的这些位置中的 ANY 的所有文档。我能够按一个位置进行搜索,但我必须以某种方式修改我的查询以搜索多个位置。
我设置了以下索引:
"mappings": {
"users": {
"properties": {
"locations": {
"type": "nested",
"properties": {
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"type": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"id": {
"type": "long"
}
}
}
}
示例文档:
{
"locations": [
{
"id": "UA",
"type": "country"
},
{
"id": "RU",
"type": "country"
},
{
"id": "OC",
"type": "continent"
}
]
}
我的按位置搜索用户的查询:
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "locations",
"query": {
"bool": {
"must": [
{
"match": { "locations.id": "RU" }
},
{
"match": { "locations.type": "country" }
}
]
}
}
}
}
]
}
}
}
编辑:
我还想检索 locations.type
等于 continent
的文档。在 SQL 中看起来像
WHERE (id IN ('UA', 'RU') AND type = 'country')
OR (id IN ('EU', 'OC', 'NA') AND type = 'continent')
对于多值搜索使用 Terms and for OR condition use should
(see here):
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "locations",
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"terms": {
"locations.id": [
"UA",
"RU"
]
}
},
{
"term": {
"locations.type": "country"
}
}
]
}
},
{
"bool": {
"must": [
{
"terms": {
"locations.id": [
"EU",
"OC"
]
}
},
{
"term": {
"locations.type": "continent"
}
}
]
}
}
]
}
}
}
}
]
}
}
}
在我的索引中,我有一个包含类型 "nested" 的 locations
属性 的映射。 属性 包含一个位置数组。每个位置都是具有以下结构的对象:
{
"id": string,
"type": string
}
在我的网站上,我想 select 多个位置并找到包含我 select 编辑的这些位置中的 ANY 的所有文档。我能够按一个位置进行搜索,但我必须以某种方式修改我的查询以搜索多个位置。
我设置了以下索引:
"mappings": {
"users": {
"properties": {
"locations": {
"type": "nested",
"properties": {
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"type": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"id": {
"type": "long"
}
}
}
}
示例文档:
{
"locations": [
{
"id": "UA",
"type": "country"
},
{
"id": "RU",
"type": "country"
},
{
"id": "OC",
"type": "continent"
}
]
}
我的按位置搜索用户的查询:
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "locations",
"query": {
"bool": {
"must": [
{
"match": { "locations.id": "RU" }
},
{
"match": { "locations.type": "country" }
}
]
}
}
}
}
]
}
}
}
编辑:
我还想检索 locations.type
等于 continent
的文档。在 SQL 中看起来像
WHERE (id IN ('UA', 'RU') AND type = 'country')
OR (id IN ('EU', 'OC', 'NA') AND type = 'continent')
对于多值搜索使用 Terms and for OR condition use should
(see here):
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "locations",
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"terms": {
"locations.id": [
"UA",
"RU"
]
}
},
{
"term": {
"locations.type": "country"
}
}
]
}
},
{
"bool": {
"must": [
{
"terms": {
"locations.id": [
"EU",
"OC"
]
}
},
{
"term": {
"locations.type": "continent"
}
}
]
}
}
]
}
}
}
}
]
}
}
}