结果过滤
結果過濾
默認情況下,elasticsearch在搜索的結果中,會把文檔中保存在_source的所有字段都返回。
如果我們只想獲取其中的部分字段,我們可以添加_source的過濾
直接指定字段
示例:
GET /learn/_search {"_source": ["title","price"],"query": {"term": {"price": 2699}} }返回的結果:
{"took": 12,"timed_out": false,"_shards": {"total": 3,"successful": 3,"skipped": 0,"failed": 0},"hits": {"total": 1,"max_score": 1,"hits": [{"_index": "learn","_type": "goods","_id": "r9c1KGMBIhaxtY5rlRKv","_score": 1,"_source": {"price": 2699,"title": "小米手機"}}]} }指定includes和excludes
我們也可以通過:
-
includes:來指定想要顯示的字段
-
excludes:來指定不想要顯示的字段
二者都是可選的。
示例:
GET /learn/_search {"_source": {"includes":["title","price"]},"query": {"term": {"price": 2699}} }與下面的結果將是一樣的:
GET /learn/_search {"_source": {"excludes": ["images"]},"query": {"term": {"price": 2699}} }?
總結