gpt4 book ai didi

elasticsearch - 重命名 Elasticsearch 输出字段

转载 作者:行者123 更新时间:2023-11-29 02:55:20 25 4
gpt4 key购买 nike

我目前在使用 Elasticsearch 时遇到问题。当我尝试执行搜索并且只希望返回字段的子集时,如果字段是嵌套的,我需要使用点符号指定字段。这是映射我的 couchDB 文档的映射器 json 文档的示例:

{
"product": {
"_type": {"store": "yes"},
"_source": {"compress": true},
"index_analyzer": "standard",
"search_analyzer": "standard",
"dynamic_date_formats": ["date_time_no_millis", "date_optional_time"],
"properties": {
"_id": {"type": "string", "store": "yes", "index": "not_analyzed"},
"key": {"type": "string", "store": "yes"},
"content": {
"type": "object",
"path": "just_name",
"properties": {
"key": {"type": "string", "store": "yes"},
"name": {"type": "string", "store": "yes", "index_name": "name"},
"description": {"type": "string", "store": "yes", "index_name": "description"},
"brand": {
"type": "object",
"index_name": "brand",
"properties": {
"abbreviation": {"type": "string", "store": "yes", "index_name": "brand_abbreviation"},
"name": {"type": "string", "store": "yes", "index_name": "brand_name"}
}
}
}
}
}
}
}

引用 _id 只是一个简单的 _id,但如果我想在内容中引用 name,我将不得不将其引用为 content.name。问题在于,当搜索输出结束时,json 输出包含字段名称:“content.name”。

是否可以将其重命名为“名称”而不包含“内容”。字首?你可以看到,我尝试指定 index_name,但这似乎没有用。

最佳答案

您可以使用 partial_fields 做这个。

例如,如果您索引这样的文档:

curl -XPUT 'http://127.0.0.1:9200/test/test/1?pretty=1'  -d '
{
"email" : "john@foo.com",
"name" : "john",
"foo" : {
"bar" : {
"baz" : 1
}
}
}
'

您可以像这样包含您想要的字段/对象:

curl -XGET 'http://127.0.0.1:9200/test/test/_search?pretty=1'  -d '
{
"partial_fields" : {
"doc" : {
"include" : [
"name",
"foo.*"
]
}
}
}
'

这会给你这样的结果:(注意缺少的 email 字段,并且该字段 foo 仍然是一个散列 - 它没有用点符号展平)

{
"hits" : {
"hits" : [
{
"_score" : 1,
"fields" : {
"doc" : {
"name" : "john",
"foo" : {
"bar" : {
"baz" : 1
}
}
}
},
"_index" : "test",
"_id" : "1",
"_type" : "test"
}
],
"max_score" : 1,
"total" : 1
},
"timed_out" : false,
"_shards" : {
"failed" : 0,
"successful" : 5,
"total" : 5
},
"took" : 1
}

旁注,关于您的映射的一些评论:

  • 你的 _id字段(我假设是 elasticsearch ID,而不是外部 ID)处于错误的级别 - 它应该与 _type 处于同一级别.如果它是外部 ID,则它处于正确的级别。
  • 为什么要存储所有字段?真的没有必要 - 它只是使用额外的资源。除非你有大量 _source字段,只检索该字段并解析它要快得多,而不是为每个单独的字段访问磁盘。

关于elasticsearch - 重命名 Elasticsearch 输出字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11923193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com