2012-08-12 37 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