2017-05-26 47 views
0

我無法使用數組字段執行重要的術語彙總。我的JavaScript查詢看起來是這樣的:elasticsearch中數組的重要術語彙總

client.search({ 
    index: myIndex, 
    body: { 
    query: { 
     terms: { 
     myField: ['someuserid'] 
     // also tried with same result... myField: 'someuserid' 
     } 
    }, 
    aggregations: { 
     recommendations: { 
     significant_terms: { 
      field: "myField", 
      min_doc_count: 1 
     } 
     } 
    } 
    } 
}) 

我得到這個錯誤:

(node:13105) UnhandledPromiseRejectionWarning: Unhandled promise rejection 
(rejection id: 1): Error: [illegal_argument_exception] Fielddata is disabled 
on text fields by default. Set fielddata=true on [myField] in order to 
load fielddata in memory by uninverting the inverted index. Note that this can 
however use significant memory. 

我的映射是這樣的:

{ 
    index: 'myIndex', 
    type: 'users', 
    body: { 
    properties: { 
     'myField': [] 
    } 
    } 
} 

我知道,我並不需要顯式地映射數組的數據類型,但我這樣做,所以我可以很容易地看到我對某個type有什麼字段。在錯誤消息後我會改變我的映射是這樣的:

... 
properties: { 
    myField: { 
    fielddata: "true" 
    } 
} 
... 

然而,這將導致該錯誤:

Error: [mapper_parsing_exception] No type specified for field [myField] 

如果我再增加一個類型: ... 屬性:{ MyField的:{ 類型:[], fielddata: 「真正的」 }} ... 我 會得到這個錯誤:

[mapper_parsing_exception] No handler for type [[]] declared on field [myField] 

目前,我彙總的數據是從通過JavaScript客戶端庫完全利用這種構造的更新API種子數據:從這個查詢curl -XGET 'localhost:9200/myIndex/users/_search?pretty'

const update = { 
    "upsert": { 
     "myField": ['myValue'] 
    }, 
    "script": { 
    "inline": "ctx._source.myField.add(params.itemField)", 
    "params": { 
     "itemField": 'itemValue' 
    } 
    } 
}; 

const req = { 
    index: 'myIndex', 
    type: 'users', 
    id: 'someuserid', 
    body: update 
} 

命中則是這樣的:

... 
{ 
    "_index" : "myIndex", 
    "_type" : "users", 
    "_id" : "someuserid", 
    "_score" : 1.0, 
    "_source" : { 
     "myField" : [ 
     "someFieldId1", 
     "someFieldId1", 
     "someFieldId2" 
     ] 
    } 
    }, 
... 

如何正確執行使用數組字段的重要術語聚合?

回答

1

https://www.elastic.co/guide/en/elasticsearch/reference/current/array.html

In Elasticsearch, there is no dedicated array type. Any field can contain zero or more values by default, however, all values in the array must be of the same datatype.

假設你正在使用ElasticSearch 5.x中,嘗試改變類型:[]類型: 「文字」類型: 「關鍵字」

對於兩者之間的差異,我會推薦閱讀:https://www.elastic.co/guide/en/elasticsearch/reference/current/text.html

但是在你的情況下,因爲它看起來像某種id,它可能不需要分析,所以我會建議「關鍵字」,而不是「文本」。

對於以前的ES版本,請改用「string」。 https://www.elastic.co/guide/en/elasticsearch/reference/2.4/string.html

+0

謝謝,這正是我最終做的。是的,我一遍又一遍地讀了那篇文檔,但我直覺地認爲,將該類型添加爲文本或關鍵字會使得如果我要更新文檔,該字段將被替換(而不是我想要的)而不是形成一個數組並添加到它。我需要更多地查看它,但是我想如果我想要使用索引API而不是更新API。 – writofmandamus

+0

您可以嘗試使用更新API和腳本執行 ctx._source.myField + = newValue https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html# _scripted_updates – deathyr