ES 2结果过滤

默认情况下,elasticsearch在搜索的结果中,会把文档中保存在_source的所有字段都返回。

如果我们只想获取其中的部分字段,我们可以添加_source的过滤。

2.1.直接指定字段

GET /jobinfo/_search
{
  "_source": ["jobAddr","companyAddr"],
  "query": {
   "terms": {
     "jobAddr": [
       "杭州",
       "北京"
     ]
   }
  }
}

返回回来的结果

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 18,
    "max_score": 3.0074053,
    "hits": [
      {
        "_index": "jobinfo",
        "_type": "JobInfoField",
        "_id": "49",
        "_score": 3.0074053,
        "_source": {
          "jobAddr": "杭州  "
        }
      },
      {
        "_index": "jobinfo",
        "_type": "JobInfoField",
        "_id": "145",
        "_score": 2.7992365,
        "_source": {
          "jobAddr": "杭州  "
        }
      },
      {
        "_index": "jobinfo",
        "_type": "JobInfoField",
        "_id": "45",
        "_score": 2.56708,
        "_source": {
          "jobAddr": "杭州-西湖区  "
        }
      }
      {
        "_index": "jobinfo",
        "_type": "JobInfoField",
        "_id": "69",
        "_score": 2.3755863,
        "_source": {
          "jobAddr": "北京  "
        }
      },
      {
        "_index": "jobinfo",
        "_type": "JobInfoField",
        "_id": "74",
        "_score": 2.3755863,
        "_source": {
          "jobAddr": "北京  "
        }
      },
      {
        "_index": "jobinfo",
        "_type": "JobInfoField",
        "_id": "67",
        "_score": 2.129149,
        "_source": {
          "jobAddr": "北京-海淀区  "
        }
      }
      }
    ]
  }
}

2.2.指定includes和excludes

我们也可以通过:

  • includes:来指定想要显示的字段
  • excludes:来指定不想像是的字段

示例:

GET /jobinfo/_search

{
  "_source": {
    "include": ["jobAddr","companyAddr"]
  },
  "query": {
   "terms": {
     "jobAddr": [
       "杭州",
       "北京"
     ]
   }
  }
}

GET /jobinfo/_search

{
  "_source": {
    "excludes": ["jobAddr","companyAddr"]
  },
  "query": {
   "terms": {
     "jobAddr": [
       "杭州",
       "北京"
     ]
   }
  }
}

你可能感兴趣的:(ES 2结果过滤)