如何在elasticsearch中的特定索引处获取元素?

时间:2017-07-21 11:54:24

标签: elasticsearch

我在elasticsearch中存储了三个json对象,每个对象都有一个def b(n): r,a=[],'' for i in range(2*n):c='* '[i%2];a+=c;l=a.ljust(2*n,c);r+=[l+l[-2::-1]] return'\n'.join(r+r[-2::-1]) for i in range(1, 5):print(i,b(i),sep='\n') title数组。

projects

我编写了一个查询,以{"name": "haris","projects": [{"title": "Splunk"},{"title": "QRadar"},{"title": "LogAnalysis"}]} {"name": "khalid","projects": [{"title": "MS"},{"title": "Google"},{"title": "Apple"}]} {"name": "Hamid","projects": [{"title": "Toyota"},{"title": "Honda"},{"title": "Kia"}]} 及其特定属性_id提取特定对象

projects

正如预期的那样,它返回项目对象

curl -XGET 'localhost:9200/jsontest/_search?pretty' -d '{"query" : { "match" : {"_id":"AV1kzzZqAzHWQ2S7B8f1"} }, "_source": ["projects"]}'

问题:有没有办法在{ "took" : 3, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 1.0, "hits" : [ { "_index" : "jsontest", "_type" : "json", "_id" : "AV1kzzZqAzHWQ2S7B8f1", "_score" : 1.0, "_source" : { "projects" : [{"title" : "Splunk"},{"title" : "QRadar"},{"title" : "LogAnalysis"} ] } } ] } }的特定索引处检索值?这是虚拟数据,在我的实际场景中projects可以有大量元素,每个元素本身都是一个具有很多属性的json对象。我只需要在projects的某个索引处检索值。

1 个答案:

答案 0 :(得分:0)

这就是我要做的事。

首先是映射

PUT test/my_objects/_mapping
{
  "properties": {
    "name":{
      "type": "string",
      "index": "not_analyzed"
    },
    "projects": {
      "type": "nested"
    }
  }
}

第二个项目已编入索引

PUT test/my_objects/1111
{
"name": "haris",
      "projects": [
              {"title": "Splunk"},
              {"title": "QRadar"},
              {"title": "LogAnalysis"}
 ]
}

最后是聚合查询

GET test/my_objects/_search

{
  "aggs": {
    "by_name": {
      "terms": {
        "field": "name"
      },
      "aggs": {
        "by_project": {
          "nested": {
            "path": "projects"
          },
          "aggs": {
            "by_title": {
              "terms": {
                "field": "projects.title"
              }
            }
          }
        }
      }
    }
  }
}
由于嵌套的aggs,它没有经过测试,有点乏味,但是如果你为了你的要求进一步操作它应该可以工作