elasticsearch 如何在elasticsearch中同时搜索嵌套字段中单个对象的两个字段
我的elasticsearch中有一个嵌套字段。设想一些类似的数据存储在此字段中:elasticsearch 如何在elasticsearch中同时搜索嵌套字段中单个对象的两个字段,elasticsearch,elasticsearch,我的elasticsearch中有一个嵌套字段。设想一些类似的数据存储在此字段中: { "id" : 1, "students":[ {"name": "John", "age": 20},{"name": "Alexander", "age":25},{"name": "Elizabeth&quo
{
"id" : 1,
"students":[
{"name": "John", "age": 20},{"name": "Alexander", "age":25},{"name": "Elizabeth", "age": 15}
]
},
"id" : 2,
"students":[
{"name": "John", "age": 23},{"name": "Thomas", "age":30}
]
}
我如何查询那些有学生“John”且其“年龄”超过21岁的文档。如果查询工作正常,第一个文档将不会作为答案重新检索,因为“John”已经20岁了,我们正在寻找一个至少21岁的“John”。因此,必须检索第二个文档。感谢您的帮助。您需要使用以更好的格式获得预期的输出
另外,请参阅,以便您可以对查询和搜索结果执行高级操作
索引映射,其中包括创建。关键字
字段,以便您可以检索文本字段的字段值
{
"mappings": {
"properties": {
"id": {
"type": "long"
},
"students": {
"type" : "nested",
"properties": {
"age": {
"type": "long"
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
根据您的问题为示例文档编制索引,并搜索查询以检索嵌套文档
{
"query": {
"nested": {
"path": "students",
"query": {
"bool": {
"must": [
{
"match": {
"students.name": "John"
}
},
{
"range": {
"students.age": {
"gt": 20
}
}
}
]
}
},
"inner_hits": {
"_source": false,
"docvalue_fields": [
{
"field": "students.name.keyword",
"format": "use_field_mapping"
}
]
}
}
}
}
"inner_hits": {
"students": {
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.8754687,
"hits": [
{
"_index": "nestedstudent",
"_type": "_doc",
"_id": "2",
"_nested": {
"field": "students",
"offset": 0
},
"_score": 1.8754687, // note this
"fields": {
"students.name.keyword": [
"John"
]
}
}
]
}
}
}
}
和内部搜索结果
{
"query": {
"nested": {
"path": "students",
"query": {
"bool": {
"must": [
{
"match": {
"students.name": "John"
}
},
{
"range": {
"students.age": {
"gt": 20
}
}
}
]
}
},
"inner_hits": {
"_source": false,
"docvalue_fields": [
{
"field": "students.name.keyword",
"format": "use_field_mapping"
}
]
}
}
}
}
"inner_hits": {
"students": {
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.8754687,
"hits": [
{
"_index": "nestedstudent",
"_type": "_doc",
"_id": "2",
"_nested": {
"field": "students",
"offset": 0
},
"_score": 1.8754687, // note this
"fields": {
"students.name.keyword": [
"John"
]
}
}
]
}
}
}
}
添加带有索引映射、搜索查询和搜索结果的工作示例 索引映射:
{
"mappings": {
"properties": {
"students": {
"type": "nested"
}
}
}
}
{
"query": {
"nested": {
"path": "students",
"query": {
"bool": {
"must": [
{ "match": { "students.name": "John" } },
{ "range": { "students.age": { "gt": 21 } } }
]
}
}
}
}
}
"hits": [
{
"_index": "64460956",
"_type": "_doc",
"_id": "2",
"_score": 1.8754687,
"_source": {
"id": 2,
"students": [
{
"name": "John",
"age": 23
},
{
"name": "Thomas",
"age": 30
}
]
}
}
]
搜索查询:
{
"mappings": {
"properties": {
"students": {
"type": "nested"
}
}
}
}
{
"query": {
"nested": {
"path": "students",
"query": {
"bool": {
"must": [
{ "match": { "students.name": "John" } },
{ "range": { "students.age": { "gt": 21 } } }
]
}
}
}
}
}
"hits": [
{
"_index": "64460956",
"_type": "_doc",
"_id": "2",
"_score": 1.8754687,
"_source": {
"id": 2,
"students": [
{
"name": "John",
"age": 23
},
{
"name": "Thomas",
"age": 30
}
]
}
}
]
搜索结果:
{
"mappings": {
"properties": {
"students": {
"type": "nested"
}
}
}
}
{
"query": {
"nested": {
"path": "students",
"query": {
"bool": {
"must": [
{ "match": { "students.name": "John" } },
{ "range": { "students.age": { "gt": 21 } } }
]
}
}
}
}
}
"hits": [
{
"_index": "64460956",
"_type": "_doc",
"_id": "2",
"_score": 1.8754687,
"_source": {
"id": 2,
"students": [
{
"name": "John",
"age": 23
},
{
"name": "Thomas",
"age": 30
}
]
}
}
]
你能提供你的映射吗?或者我可以自己假设映射,并提供相应的解决方案?请接受你的!非常感谢你,它很有效!但是它是如何工作的呢??为什么它与第一项不匹配?弹性搜索是否考虑了第一种情况对它的正确对象?“SaEEdNaseHi很高兴这对你有用:”嵌套查询搜索嵌套的字段对象,就像它们被索引为单独的文档一样。因此,在这里,如果对象与
must
子句中的两个条件都匹配,那么只有它才会返回文档。@saeednasehi由于age
字段中的条件,它将与文档1不匹配(还有必须
子句。如果要进行搜索,则查询应在名称
字段或年龄
字段上匹配,然后可以使用应该
子句。哦!谢谢。我想elasticsearch会分别检查第一个条件和第二个条件,文档中的两个对象可以与h一起。真的谢谢!@saeednasehi Welcome这是一个很好的答案。对我来说很有用!谢谢。@saeednasehi谢谢,请浏览我提供的链接,这些链接将有助于提前使用案例。非常感谢你,我的朋友!@saeednasehi Welcome,我的ES专家同事和朋友