muthu kutty
muthu kutty

Reputation: 47

How to filter specific field from elastic search

Is it possible to filter only specific field not full documents from elastic search.below i have mentioned sample data for example,please check.

Ex:My data.

    {
  "took": 65,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 4.89784,
    "hits": [
      {
        "_index": "mytest",
        "_type": "bank",
        "_id": "1",
        "_score": 4.89784,
        "_source": {
          "account_number": 1,
          "balance": 39225,
          "firstname": "Amber",
          "lastname": "Duke",
          "age": 32,
          "gender": "M",
          "address": "880 Holmes Lane",
          "employer": "Pyrami",
          "email": "[email protected]",
          "city": "Brogan",
          "state": "IL"
        }
      }
    ]
  }

Expected Result.

{
   [
{
"account_number": 1,
"balance": 39225,
}
   ]
}

my rest API

http://localhost:9200/mytest/bank/_search?q=firstname:Virginia%20AND%20age:39

Output:

{"took":2,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":5.882802,"hits":[{"_index":"mytest","_type":"bank","_id":"25","_score":5.882802,"_source":{"account_number":25,"balance":40540,"firstname":"Virginia","lastname":"Ayala","age":39,"gender":"F","address":"171 Putnam Avenue","employer":"Filodyne","email":"[email protected]","city":"Nicholson","state":"PA"}}]}}

EDIT 1

http://localhost:9200/mytest/bank/_search?_source=account_number&q=account_number:25

output:

{"took":7,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":1.0,"hits":[{"_index":"mytest","_type":"bank","_id":"25","_score":1.0,"_source":{"account_number":25}}]}}

Upvotes: 0

Views: 301

Answers (1)

ceth
ceth

Reputation: 45325

Do you need to retrieve a specific set of fields from your document ? You can use the _source parameter like this:

http://localhost:9200/mytest/bank/_search?q=firstname:Virginia%20AND%20age:39&_source=account_number,balance

Update:

I will use REST API. Create the index:

curl -XPUT 'localhost:9200/test'
{"acknowledged":true,"shards_acknowledged":true,"index":"test"}

Add a document to index:

curl -XPUT 'localhost:9200/test/test/1' -H'Content-Type: application/json'  -d '{"one": "10", "two": "20", "three": "30", "four": "40"}'
{"_index":"test","_type":"test","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}

Get all fields in document:

curl -GET 'localhost:9200/test/test/1'
{"_index":"test","_type":"test","_id":"1","_version":1,"found":true,"_source":{"one": "10", "two": "20", "three": "30", "four": "40"}}

Get selected fields in document:

curl -GET 'localhost:9200/test/test/1?_source=one,four'
{"_index":"test","_type":"test","_id":"1","_version":1,"found":true,"_source":{"four":"40","one":"10"}}

If you still have a problem you need to post full example as I do.

Update 2:

Get all fields using search request:

curl -XGET  -H'Content-Type: application/json' localhost:9200/test/test/_search -d'{"query":{"match_all":{}}}'
{"took":180,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":1.0,"hits":[{"_index":"test","_type":"test","_id":"1","_score":1.0,"_source":{"one": "10", "two": "20", "three": "30", "four": "40"}}]}}

Get selected fields using search request:

curl -XGET  -H'Content-Type: application/json' 'localhost:9200/test/test/_search?_source=one,four' -d'{"query":{"match_all":{}}}'
{"took":6,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":1.0,"hits":[{"_index":"test","_type":"test","_id":"1","_score":1.0,"_source":{"four":"40","one":"10"}}]}}

Update 3:

curl -XGET  -H'Content-Type: application/json' 'localhost:9200/test/test/_search?_source=one&q=one:10'
    {"took":11,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":0.2876821,"hits":[{"_index":"test","_type":"test","_id":"1","_score":0.2876821,"_source":{"one":"10"}}]}}

Upvotes: 1

Related Questions