ElasticSearch:获取嵌套字段的所有值
ElasticSearch: Getting all values for nested field
我是 ElasticSearch 的新手,使用 v5.1.2 尝试获取嵌套字段的所有值,在我的示例中是 firstname
中的值。我的数据:
PUT my_index/my_type/1
{
"group" : "fans",
"user" : [
{
"firstname" : "John",
"lastname" : "Smith"
},
{
"firstname" : "Alice",
"lastname" : "White"
},
{
"lastname": "Muller"
}
]
}
我希望我的查询结果是名字 "John" 和 "Alice"。
我尝试了几个聚合查询,例如:
GET my_index/my_type/_search
{
"size":0,
"aggs": {
"myagg": {
"terms": {
"field": "user.firstname"
}
}
}
}
但没有成功。我该如何进行此类查询?
您必须先声明一个 mappings for your index with user as nested 字段
PUT my_index3
{
"mappings": {
"my_type": {
"properties": {
"user": {
"type": "nested",
"properties": {
"firstname":{
"type":"keyword"
}
}
}
}
}
}
}
PUT my_index3/my_type/1
{
"group" : "fans",
"user" : [
{
"firstname" : "John",
"lastname" : "Smith"
},
{
"firstname" : "Alice",
"lastname" : "White"
},
{
"lastname": "Muller"
}
]
}
声明映射后,您可以像下面这样使用 nested aggregations。
POST my_index3/_search
{
"size": 0,
"aggs": {
"nested_user": {
"nested": {
"path": "user"
},
"aggs": {
"firstname": {
"terms": {
"field": "user.firstname",
"size": 10
}
}
}
}
}
}
希望对您有所帮助
我是 ElasticSearch 的新手,使用 v5.1.2 尝试获取嵌套字段的所有值,在我的示例中是 firstname
中的值。我的数据:
PUT my_index/my_type/1
{
"group" : "fans",
"user" : [
{
"firstname" : "John",
"lastname" : "Smith"
},
{
"firstname" : "Alice",
"lastname" : "White"
},
{
"lastname": "Muller"
}
]
}
我希望我的查询结果是名字 "John" 和 "Alice"。 我尝试了几个聚合查询,例如:
GET my_index/my_type/_search
{
"size":0,
"aggs": {
"myagg": {
"terms": {
"field": "user.firstname"
}
}
}
}
但没有成功。我该如何进行此类查询?
您必须先声明一个 mappings for your index with user as nested 字段
PUT my_index3
{
"mappings": {
"my_type": {
"properties": {
"user": {
"type": "nested",
"properties": {
"firstname":{
"type":"keyword"
}
}
}
}
}
}
}
PUT my_index3/my_type/1
{
"group" : "fans",
"user" : [
{
"firstname" : "John",
"lastname" : "Smith"
},
{
"firstname" : "Alice",
"lastname" : "White"
},
{
"lastname": "Muller"
}
]
}
声明映射后,您可以像下面这样使用 nested aggregations。
POST my_index3/_search
{
"size": 0,
"aggs": {
"nested_user": {
"nested": {
"path": "user"
},
"aggs": {
"firstname": {
"terms": {
"field": "user.firstname",
"size": 10
}
}
}
}
}
}
希望对您有所帮助