构建我的第一个RESTful api,并认为我会尝试使用elasticsearch作为基础。有没有办法在Elasticsearch中自定义API,只返回查询结果中的某些字段。例如,如果我有fname,lname,city,state,zip,email的数据,我只想为每个与city字段匹配的查询返回一个fnames和cities列表。所以像这样:
curl -XPOST "http://localhost:9200/custom_call/_search" -d'
{
"query": {
"query_string": {
"query": "Toronto",
"fields": ["city"]
}
}
}'
理想情况下会返回类似的内容:
{"took": 52, "timed_out": false, "_shards": {
"total": 35,
"successful": 35,
"failed": 0
}, "hits": {
"total": 1,
"max_score": 0.375,
"hits": [
{
"_index": "persons",
"_type": "person",
"_id": "6",
"_score": 0.375,
"_source": {
"fname": "Bob",
"city": "Toronto",
}
},
{
"_index": "persons",
"_type": "person",
"_id": "13",
"_score": 0.375,
"_source": {
"fname": "Sue",
"city": "Toronto",
}
},
{
"_index": "persons",
"_type": "person",
"_id": "21",
"_score": 0.375,
"_source": {
"fname": "Jose",
"city": "Toronto",
}
}
]
}}
不确定Elasticsearch是否设置为执行此操作,或者即使您想要它也是如此。我第一次尝试构建RESTful API。我想如果NPR StackOverflow喜欢它,它值得一试!谢谢你的帮助。
答案 0 :(得分:0)
是的,你可以,我想你没有试过自己找出来。
以下是如何做到这一点,
POST localhost:9200/index/type/_search
{
"query": {
"query_string": {
"query": "Toronto",
"fields": ["city"]
}
},
"_source" :["fields_you_want_to_get"]
}
您正在寻找的术语是source filtering.