2016-10-03 31 views
0

我爲了得到哪些值正在使用此查詢中有一個字段(SQLfying將是一個SELECT field, count(field) GROUP BY fieldElasticSearch聚集:字符串用空格當作兩個字符串

爲了做到這一點我。發送該請求,ES:

{ 
    "query" : { 
    "bool" : { 
     "must" : { 
     "exists" : { 
      "field" : "metainfos.ceeaacceaeaaccebeaacceceaaccedeaac" 
     } 
     } 
    } 
    }, 
    "aggregations" : { 
    "followUpActivity.metainfo.metainfos.ceeaacceaeaaccebeaacceceaaccedeaac" : { 
     "terms" : { 
     "field" : "metainfos.ceeaacceaeaaccebeaacceceaaccedeaac", 
     "missing" : "null" 
     } 
    } 
    } 
} 

有一個關於這個集合只有一個文件:

{ 
    "took" : 1, 
    "timed_out" : false, 
    "_shards" : { 
    "total" : 1, 
    "successful" : 1, 
    "failed" : 0 
    }, 
    "hits" : { 
    "total" : 1, 
    "max_score" : 1.0, 
    "hits" : [ { 
     "_index" : "living_v1", 
     "_type" : "fuas", 
     "_id" : "a2cb0ba1-8955-11e6-8a00-0242ac110007", 
     "_score" : 1.0, 
     "_routing" : "user2", 
     "_source" : { 
     "user" : "user2", 
     "timestamp" : "2016-10-03T11:08:30.074Z", 
     "startTimestamp" : "2016-10-03T11:08:30.074Z", 
     "dueTimestamp" : null, 
     "closingTimestamp" : null, 
     "matter" : "Fua 1", 
     "comment" : null, 
     "status" : 0, 
     "backlogStatus" : 20, 
     "metainfos" : { 
      "ceeaacceaeaaccebeaacceceaaccedeaac" : [ "Living Digital" ] 
     }, 
     "resources" : [ ], 
     "notes" : null 
     } 
    } ] 
    } 
} 

正如你可以看到doc.metainfos.ceeaacc... = ["Living Digital"]

{ 
    "took" : 3, 
    "timed_out" : false, 
    "_shards" : { 
    "total" : 1, 
    "successful" : 1, 
    "failed" : 0 
    }, 
    "hits" : { 
    "total" : 1, 
    "max_score" : 1.0, 
    "hits" : [ { 
     "_index" : "living_v1", 
     "_type" : "fuas", 
     "_id" : "a2cb0ba1-8955-11e6-8a00-0242ac110007", 
     "_score" : 1.0, 
     "_routing" : "user2", 
     "_source":{"user":"user2","timestamp":"2016-10-03T11:08:30.074Z","startTimestamp":"2016-10-03T11:08:30.074Z","dueTimestamp":null,"closingTimestamp":null,"matter":"Fua 1","comment":null,"status":0,"backlogStatus":20,"metainfos":{"ceeaacceaeaaccebeaacceceaaccedeaac":["Living Digital"]},"resources":[],"notes":null} 
    } ] 
    }, 
    "aggregations" : { 
    "followUpActivity.metainfo.metainfos.ceeaacceaeaaccebeaacceceaaccedeaac" : { 
     "doc_count_error_upper_bound" : 0, 
     "sum_other_doc_count" : 0, 
     "buckets" : [ { 
     "key" : "digital", 
     "doc_count" : 1 
     }, { 
     "key" : "living", 
     "doc_count" : 1 
     } ] 
    } 
    } 
} 

ES是讓我的兩個值:一個爲"living",另一個用於"digital"。我想要使​​用shole值"Living Digital"進行聚合。

映射方法是:

{ 
    "living_v1" : { 
    "mappings" : { 
     "fuas" : { 
     "properties" : { 
      "backlogStatus" : { 
      "type" : "long" 
      }, 
      "comment" : { 
      "type" : "string" 
      }, 
      "matter" : { 
      "type" : "string" 
      }, 
      "metainfos" : { 
      "properties" : { 
       "ceeaacceaeaaccebeaacceceaaccedeaac" : { 
       "type" : "string" 
       } 
      } 
      }, 
      "startTimestamp" : { 
      "type" : "date", 
      "format" : "strict_date_optional_time||epoch_millis" 
      }, 
      "status" : { 
      "type" : "long" 
      }, 
      "timestamp" : { 
      "type" : "date", 
      "format" : "strict_date_optional_time||epoch_millis" 
      }, 
      "user" : { 
      "type" : "string", 
      "index" : "not_analyzed" 
      } 
     } 
     } 
    } 
    } 
} 

正如你可以看到:

"metainfos" : { 
    "properties" : { 
     "ceeaacceaeaaccebeaacceceaaccedeaac" : { 
      "type" : "string" 
     } 
    } 
} 

對我來說,問題是「ceeaacceaeaaccebeaacceceaaccedeaac」是用戶點播屬性創建,我不知道我怎麼能設置not-analyzed到任何metainfos.*字段。

#curl -XPUT 'http://localhost:9200/living_v1/' -d ' 
{ 
    "mappings": { 
    "fuas": { 
     "dynamic_templates": [ 
     { 
      "metainfos": { 
      "path_match": "metainfos.*", 
      "match_mapping_type": "string", 
      "mapping": { 
       "type": "string", 
       "index": "not_analyzed" 
      } 
      } 
     } 
     ] 
    } 
    } 
} 
' 

它告訴我,living_v1指數已經存在:

編輯

我已經與測試。據我已經能夠找出對here我需要發送一個PUTindex

{ 
    "error":{ 
    "root_cause":[ 
     { 
      "type":"index_already_exists_exception", 
      "reason":"already exists", 
      "index":"living_v1" 
     } 
    ], 
    "type":"index_already_exists_exception", 
    "reason":"already exists", 
    "index":"living_v1" 
}, 
"status":400 
} 
+0

我認爲您正在尋找動態索引模板:http://stackoverflow.com/a/23370138/693546 – mblaettermann

回答

1

正如你已經注意到了,搜索行爲是用一個默認應用映射導致的。該映射不會對所有未定義不同的字符串值字段進行分析。

所以,如果你還不知道哪些屬性(=鍵)將在metainfos對象,你可以使用動態模板功能描述herehere定義映射關係應該被應用於這些領域等覆蓋分析字符串字段的默認行爲。

您可以應用的映射,看起來有點像這樣(未測試):

{ 
    "mappings": { 
    "fuas": { 
     "dynamic_templates": [ 
     { 
      "metainfos": { 
      "path_match": "metainfos.*", 
      "match_mapping_type": "string", 
      "mapping": { 
       "type": "string", 
       "index": "not_analyzed", 
      } 
      } 
     } 
     ] 
    } 
    } 
} 
+0

非常感謝@Andreas 。我試過了,但是它出現了一些問題。模板和你的方法有什麼區別?他們是一樣的嗎? – Jordi

1

正如其他人所指出的,動態模板是要走的路。唯一的問題是,在索引一些文檔後,您無法更改索引模板。您將需要重新創建索引(刪除索引,創建映射,饋送新文檔)

+0

好的@oldbam,我明白了。有沒有一些直接的方法來重建從index_v1到index_v2的索引? – Jordi

+1

你可以考慮在http://stackoverflow.com/questions/28626803/how-to-rename-an-index-in-a-cluster查看答案。當我改變索引模板時,我總是刪除一個索引並開始再次提供文檔 – oldbam