是否可以在 Elasticsearch 中获取 copy_to 字段的内容?
Is it possible to get contents of copy_to field in Elasticsearch?
我正在使用 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
}
}
}
因此,由于 copy_to
,来自 subfield.subsubfield.subtext
的文本被复制到文本 field
。如果查询 post,则只有来自 text
的数据显示在 text
字段中,因为 _source
未被修改。如果要获取所有文本,则应在客户端聚合所有字段。如果有很多子字段,这可能会很不方便。
是否有一个神奇的查询,可以让 text
字段复制所有文本?
在 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
{
"stored_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"
]
}
}
]
}
}
老问题,新简单测试。下面介绍如何使用简单的索引对其进行测试。
请求
DELETE test-index
PUT test-index
{
"mappings": {
"properties": {
"first_name": {
"type": "text",
"copy_to": "full_name"
},
"last_name": {
"type": "text",
"copy_to": "full_name"
},
"full_name": {
"type": "text",
"store": true
}
}
}
}
PUT test-index/_doc/1
{
"first_name": "John",
"last_name": "Doe"
}
GET test-index/_search
{
"query": {
"match": {
"full_name": {
"query": "John Doe"
}
}
},
"stored_fields": [
"full_name"
]
}
回应
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "test-index",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.5753642,
"fields" : {
"full_name" : [
"John",
"Doe"
]
}
}
]
}
}
参考资料
我正在使用 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
}
}
}
因此,由于 copy_to
,来自 subfield.subsubfield.subtext
的文本被复制到文本 field
。如果查询 post,则只有来自 text
的数据显示在 text
字段中,因为 _source
未被修改。如果要获取所有文本,则应在客户端聚合所有字段。如果有很多子字段,这可能会很不方便。
是否有一个神奇的查询,可以让 text
字段复制所有文本?
在 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
{
"stored_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"
]
}
}
]
}
}
老问题,新简单测试。下面介绍如何使用简单的索引对其进行测试。
请求
DELETE test-index
PUT test-index
{
"mappings": {
"properties": {
"first_name": {
"type": "text",
"copy_to": "full_name"
},
"last_name": {
"type": "text",
"copy_to": "full_name"
},
"full_name": {
"type": "text",
"store": true
}
}
}
}
PUT test-index/_doc/1
{
"first_name": "John",
"last_name": "Doe"
}
GET test-index/_search
{
"query": {
"match": {
"full_name": {
"query": "John Doe"
}
}
},
"stored_fields": [
"full_name"
]
}
回应
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "test-index",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.5753642,
"fields" : {
"full_name" : [
"John",
"Doe"
]
}
}
]
}
}
参考资料