3

I have an index in elastic-search.

Sample structure :

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

From this index i want to retrieve selected fields: org and dateModified.

I want result like this

{
    "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"
                    }
                }
            }
        ]
    }
}

How to query elastic-search to get only selected specific fields ?

3 Answers 3

9

You can retrieve only a specific set of fields in the result hits using the _source parameter like this:

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

Or in this format:

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
    }
}'
Sign up to request clarification or add additional context in comments.

9 Comments

I write query like {"query":{ "_source":["org"] }} but it is giving error
It is giving output like { "_index": "couchrecords", "_type": "couchbaseDocument", "_id": "3", "_score": 1, "_source": { } }
Something's wrong with how your documents were indexed. What do you see when running this: curl -XGET localhost:9200/couchrecords/couchbaseDocument/3? Also can you update your question with the couchbaseDocument mapping you're using, i.e. do you have "_source" : {"enabled" : false} somewhere in them?
no I am not using "_source" : {"enabled" : false} my mapping is normal
What does this return curl -XGET localhost:9200/couchrecords/couchbaseDocument/3 ?
|
1

That's easy. Considering any query of this format :

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

You'll just need to add the fields field into your query which in your case will result in the following :

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

2 Comments

I write {"query":{ "match_all":{}}, "fields" : ["org","dateModified"]} and i get result as { "took": 32,"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 } , { "_index": "couchrecords", "_type": "couchbaseDocument", "_id": "8", "_score": 1 }
update with your question with your query. something might be wrong with it!
0
{
  "_source" : ["org","dateModified"],
  "query": {
  ...
  }
}

Check ElasticSearch source filtering.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.