从elasticsearch中的索引中获取特定字段

时间:2015-06-01 11:34:25

标签: elasticsearch

我在弹性搜索中有一个索引。

样本结构:

{
    "Article": "Article7645674712",
    "Genre": "Genre92231455",
    "relationDesc": [
        "Article",
        "Genre"
    ],
    "org": "user",
    "dateCreated": {
        "date": "08/05/2015",
        "time": "16:22 IST"
    },
    "dateModified": "08/05/2015"
}

我希望从此索引中检索所选字段: org dateModified

我想要这样的结果

{
    "took": 265,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 28,
        "max_score": 1,
        "hits": [
            {
                "_index": "couchrecords",
                "_type": "couchbaseDocument",
                "_id": "3",
                "_score": 1,
                "_source": {
                    "doc": {
                        "org": "user",
                        "dateModified": "08/05/2015"
                    }
                }
            },
            {
                "_index": "couchrecords",
                "_type": "couchbaseDocument",
                "_id": "4",
                "_score": 1,
                "_source": {
                    "doc": {
                        "org": "user",
                        "dateModified": "10/05/2015"
                    }
                }
            }
        ]
    }
}

如何查询弹性搜索以仅获取选定的特定字段?

3 个答案:

答案 0 :(得分:3)

您可以使用_source参数检索结果点击中的一组特定字段,如下所示:

curl -XGET localhost:9200/couchrecords/couchbaseDocument/_search?_source=org,dateModified

或采用以下格式:

curl -XPOST localhost:9200/couchrecords/couchbaseDocument/_search -d '{
    "_source": ["doc.org", "doc.dateModified"],  <---- you just need to add this
    "query": {
        "match_all":{}   <----- or whatever query you have
    }
}'

答案 1 :(得分:0)

这很容易。考虑对此格式的任何查询:

{
  "query": {
  ...
  },
}

您只需要在查询中添加fields字段,在您的情况下会产生以下结果:

{
  "query": {
  ...
  },
  "fields" : ["org","dateModified"]
}

答案 2 :(得分:0)

{
  "_source" : ["org","dateModified"],
  "query": {
  ...
  }
}

检查ElasticSearch source filtering