Elastic search 5.1.1 Get Specific fields

I 'am using Elastic search 5.1.1 ,how to get the data of specific field of an Index .

I tried this

POST /rawdata/feed/_search?pretty=true
{
"_source": ["FeatureValue", "FeatureName"],
"query": {
"match_all":{}
}
}

and sample result is

{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 386424,
"max_score": 1,
"hits": [
{
"_index": "rawdata",
"_type": "feed",
"_id": "591031",
"_score": 1,
"_source": {}
},

how to get all the data specified for these fields (FeatureValue,FeatureName)

It looks like it's returning documents that do not have these fields and thus the _source is empty.
Rather than using the match_all, you should use an exists query , so the search hits only the docs that do have these fields.

E.g.:

GET /logstash-000002/_search?pretty=true
{
"_source": [
"temp_cpu"
],
"query": {
"match_all": {}
}
}

returns:

"hits": [
{
"_index": "logstash-000002",
"_type": "test",
"_id": "AVl05TMTGyqdsCsv30pl",
"_score": 1,
"_source": {}
},

while

GET /logstash-000002/_search?pretty=true
{
"_source": [
"temp_cpu"
],
"query": {
"exists" : { "field" : "temp_cpu" }
}
}

returns

"hits": [
{
"_index": "logstash-000002",
"_type": "sensors",
"_id": "AVl0y3pARruJw4Xh37Tf",
"_score": 1,
"_source": {
"temp_cpu": 50
}
},
{

In my case, only some docs have the temp_cpu field and the first query (match_all) returned docs without this field, thus causing the empty _source. While the second one only hit the docs that have this field.

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.