2016-10-03 91 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