我正在使用Elasticsearch v2.3.0。假设我有一个带映射的索引:
{
"mappings": {
"post": {
"dynamic": false,
"_source": {
"enabled": true
},
"properties": {
"text": {
"norms": {
"enabled": false
},
"fielddata": {
"format": "disabled"
},
"analyzer": "text_analyzer",
"type": "string"
},
"subfield": {
"properties": {
"subsubfield": {
"properties": {
"subtext": {
"index": "no",
"analyzer": "text_analyzer",
"type": "string",
"copy_to": "text"
}
}
}
}
}
},
"_all": {
"enabled": false
}
}
}
因此,由于subfield.subsubfield.subtext
,来自field
的文字被复制到文字copy_to
。如果您查询帖子,则text
字段中仅显示来自text
的数据,因为_source
未被修改。如果要获取所有文本,则应聚合客户端上的所有字段。如果存在许多子场,则这可能是不方便的。
是否有魔术查询,允许获取text
字段并将所有文本复制到其中?
答案 0 :(得分:10)
在text
字段集"store":"yes"
的映射中
您应该可以使用fields
示例强>:
put test/test/_mapping
{
"properties" : {
"name" : { "type" : "string","copy_to": "text"},
"text" : {"type" : "string" ,"store" :"yes"},
"subfield": {
"properties": {
"subsubfield": {
"properties": {
"subtext": {
"index": "no",
"type": "string",
"copy_to": "text"
}
}
}
}
}
}
}
put test/test/1
{
"name" : "hello",
"subfield" : {
"subsubfield" : [
{"subtext" : "soundgarden" },
{"subtext" : "alice in chains" },
{"subtext" : "dio" }
]
}
}
post test/_search
{
"fields": [
"text"
]
}
<强>结果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "1",
"_score": 1,
"fields": {
"text": [
"hello",
"soundgarden",
"alice in chains",
"dio"
]
}
}
]
}
}