2012-08-12 36 views
3

我现在在使用弹性搜索时遇到问题。当我尝试执行搜索并只希望返回一部分字段时,如果字段嵌套,则需要使用点符号指定字段。下面是它映射我的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,但说我想指的名字的内容,我就不得不提到它作为content.name。这样做的问题是,当搜索输出结束时,json输出包含字段名称:“content.name”。

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

回答

4

您可以使用partial_fields来做到这一点。

举例来说,如果你的索引这样的文档:

curl -XPUT 'http://127.0.0.1:9200/test/test/1?pretty=1' -d ' 
{ 
    "email" : "[email protected]", 
    "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,而不是外部标识)是在错误的层面 - 它应该是在与_type相同。如果它是一个外部ID,那么它处于正确的级别。
  • 你为什么要存储所有的字段?真的没有必要 - 它只是使用额外的资源。除非您有大量的_source字段,否则检索该字段并解析该字段的速度要快得多,而不是每个字段的磁盘都要打到磁盘上。
+0

首先我要感谢您回复此问题。这是我迄今收到的关于弹性搜索的第一个体面的答案!即使是邮件列表或IRC频道也没有多大帮助! 是的,_id字段是外部ID。 为什么我要存储所有字段?我真的不知道;听起来好像我不存储它们,意味着它们不会被索引。谢谢你的提示。总而言之,我觉得这些文档在一些真实世界的例子或非常详细的例子中是不存在的。你已经帮了我很多,谢谢! – Mark 2012-08-14 03:26:03

+0

嗯......我想接受这个答案,但是请你帮我解释一下我的意见吗? – Mark 2012-08-24 08:07:18