如何获取至少具有 Elasticsearch 查询中指定属性的所有文档?
How can I get all the documents which have at least the properties specified in an Elasticsearch query?
是否可以 select 索引中的某个项目与某个子项目的多个值相匹配?我认为这不是很清楚,但我在下面添加了更多详细信息。
我有以下索引:
{
"mappings":{
"entity":{
"properties":{
"name" : {"type" : "string"},
"features":{
"type" : "nested",
"include_in_parent":错误,
"properties":{
"id" : {"type" : "integer"},
"value_int" : {"type" : "integer"},
"value_text" : {"type" : "string"},
"value_decimal" : {"type" : "integer"}<br>
}
}
}
}
},
"settings":{
"number_of_shards" : 1,
"number_of_replicas":0
}
}
索引中的一些项目
{
"name" : "Bazar",
"features" : [
{
"id" : 1,
"value_text" : null,
"value_decimal" : null,
"value_int": 51
},
{
"id" : 9,
"value_text" : "Amsterdam",
"value_decimal" : null,
"value_int": null
}
]
}
{
"name" : "Bazar Test",
"features" : [
{
"id" : 1,
"value_text" : null,
"value_decimal" : null,
"value_int": 52
},
{
"id" : 9,
"value_text" : "Leiden",
"value_decimal" : null,
"value_int": null
}
]
}
{
"name" : "Bazar no city",
"features" : [
{
"id" : 1,
"value_text" : null,
"value_decimal" : null,
"value_int": 51
},
]
}
我需要的是一种方法来找到 features.id = 1 和 features.id = 2 的项目(例如:"Bazar" 和 "Bazar Test" 项目) .
我得到的查询是
{
"query" : {
"nested" : {
"path" : "features",
"query" : {
"bool" : {
"must" : [
{ "terms" : { "features.id" : [1, 9]} }
]
}
}
}
}
}
此查询的问题在于它 select 包含 features.id = 1 或 features.id = 9 的项目,因此返回所有项目。
编辑
尝试了一个新查询
{
"query" : {
"nested" : {
"path" : "features",
"query" : {
"bool" : {
"must" : [
{ "terms" : {
"features.id" : [1, 9],
"minimum_should_match": 2
}
}
]
}
}
}
}
}
但是我没有结果。
编辑:
在我合并答案后,我设法让它工作了。
谢谢你的帮助:)
这是我的查询(稍作修改)
{
"from": 0,
"size": 20,
"query": {
"filtered": {
"query": {
"bool": {
"must": [
{
"match_phrase_prefix": {
"title": {
"query": "deli",
"max_expansions": 5
}
}
},
{
"match": {
"entity_type_id": 5
}
}
]
}
},
"filter": {
"and": {
"filters": [
{
"nested": {
"path": "features",
"query": {
"bool": {
"must": [
{
"match": {
"features.id": 31
}
},
{
"match": {
"features.value_int": {
"query": [
56, 57
],
"operator": "and"
}
}
}
]
}
}
}
}
]
}
}
}
}
}
谢谢。
嵌套文档更难查询。这应该是你想要的:
{
"query": {
"filtered": {
"filter": {
"and": {
"filters": [
{
"nested": {
"path": "features",
"query": {
"term": {
"features.id": {
"value": "1"
}
}
}
}
},
{
"nested": {
"path": "features",
"query": {
"term": {
"features.id": {
"value": "9"
}
}
}
}
}
]
}
}
}
}
}
match
查询支持布尔值 operator
参数。您还应该将查询包装在 nested
查询中,因为 features
字段在您的映射中是 nested
。
试试这个查询:
{
"query": {
"nested": {
"query": {
"match": {
"features.id": {
"query": "1 9",
"operator": "and"
}
}
},
"path": "features"
}
}
}
是否可以 select 索引中的某个项目与某个子项目的多个值相匹配?我认为这不是很清楚,但我在下面添加了更多详细信息。
我有以下索引:
{
"mappings":{
"entity":{
"properties":{
"name" : {"type" : "string"},
"features":{
"type" : "nested",
"include_in_parent":错误,
"properties":{
"id" : {"type" : "integer"},
"value_int" : {"type" : "integer"},
"value_text" : {"type" : "string"},
"value_decimal" : {"type" : "integer"}<br>
}
}
}
}
},
"settings":{
"number_of_shards" : 1,
"number_of_replicas":0
}
}
索引中的一些项目
{
"name" : "Bazar",
"features" : [
{
"id" : 1,
"value_text" : null,
"value_decimal" : null,
"value_int": 51
},
{
"id" : 9,
"value_text" : "Amsterdam",
"value_decimal" : null,
"value_int": null
}
]
}
{
"name" : "Bazar Test",
"features" : [
{
"id" : 1,
"value_text" : null,
"value_decimal" : null,
"value_int": 52
},
{
"id" : 9,
"value_text" : "Leiden",
"value_decimal" : null,
"value_int": null
}
]
}
{
"name" : "Bazar no city",
"features" : [
{
"id" : 1,
"value_text" : null,
"value_decimal" : null,
"value_int": 51
},
]
}
我需要的是一种方法来找到 features.id = 1 和 features.id = 2 的项目(例如:"Bazar" 和 "Bazar Test" 项目) .
我得到的查询是
{
"query" : {
"nested" : {
"path" : "features",
"query" : {
"bool" : {
"must" : [
{ "terms" : { "features.id" : [1, 9]} }
]
}
}
}
}
}
此查询的问题在于它 select 包含 features.id = 1 或 features.id = 9 的项目,因此返回所有项目。
编辑 尝试了一个新查询
{
"query" : {
"nested" : {
"path" : "features",
"query" : {
"bool" : {
"must" : [
{ "terms" : {
"features.id" : [1, 9],
"minimum_should_match": 2
}
}
]
}
}
}
}
}
但是我没有结果。
编辑:
在我合并答案后,我设法让它工作了。 谢谢你的帮助:)
这是我的查询(稍作修改)
{
"from": 0,
"size": 20,
"query": {
"filtered": {
"query": {
"bool": {
"must": [
{
"match_phrase_prefix": {
"title": {
"query": "deli",
"max_expansions": 5
}
}
},
{
"match": {
"entity_type_id": 5
}
}
]
}
},
"filter": {
"and": {
"filters": [
{
"nested": {
"path": "features",
"query": {
"bool": {
"must": [
{
"match": {
"features.id": 31
}
},
{
"match": {
"features.value_int": {
"query": [
56, 57
],
"operator": "and"
}
}
}
]
}
}
}
}
]
}
}
}
}
}
谢谢。
嵌套文档更难查询。这应该是你想要的:
{
"query": {
"filtered": {
"filter": {
"and": {
"filters": [
{
"nested": {
"path": "features",
"query": {
"term": {
"features.id": {
"value": "1"
}
}
}
}
},
{
"nested": {
"path": "features",
"query": {
"term": {
"features.id": {
"value": "9"
}
}
}
}
}
]
}
}
}
}
}
match
查询支持布尔值 operator
参数。您还应该将查询包装在 nested
查询中,因为 features
字段在您的映射中是 nested
。
试试这个查询:
{
"query": {
"nested": {
"query": {
"match": {
"features.id": {
"query": "1 9",
"operator": "and"
}
}
},
"path": "features"
}
}
}