是否可以仅过滤弹性搜索中的特定字段而不是完整文档。下面我提到了示例数据,请检查。
Ex:我的数据。
{
"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": "amberduke@pyrami.com",
"city": "Brogan",
"state": "IL"
}
}
]
}
预期结果。
{
[
{
"account_number": 1,
"balance": 39225,
}
]
}
我的其余API
http://localhost:9200/mytest/bank/_search?q=firstname:Virginia%20AND%20age:39
输出:
{"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":"virginiaayala@filodyne.com","city":"Nicholson","state":"PA"}}]}}
编辑1
http://localhost:9200/mytest/bank/_search?_source=account_number&q=account_number:25
输出:
{"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}}]}}
答案 0 :(得分:1)
您是否需要从文档中检索一组特定字段? 您可以像这样使用_source参数:
http://localhost:9200/mytest/bank/_search?q=firstname:Virginia%20AND%20age:39&_source=account_number,balance
<强>更新强>:
我将使用REST API。创建索引:
curl -XPUT 'localhost:9200/test'
{"acknowledged":true,"shards_acknowledged":true,"index":"test"}
将文档添加到索引:
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}
获取文档中的所有字段:
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"}}
获取文档中的选定字段:
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"}}
如果您仍然遇到问题,则需要像我一样发布完整示例。
更新2:
使用搜索请求获取所有字段:
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"}}]}}
使用搜索请求获取所选字段:
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"}}]}}
更新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"}}]}}