elasticsearch-py 使用源过滤器搜索
elasticsearch-py search using source filter
我正在使用 elasticsearch-py(es 版本是 2.3)并且想 return 只是索引中所有文档中的 'title' 字段映射:演员,导演,类型、情节、标题、年份。
我目前正在尝试 messages = es.search(index="movies", _source=['hits.hits.title'])
,得到的响应是:
{u'hits': {u'hits': [{u'_score': 1.0, u'_type': u'movie', u'_id': u'tt0116996', u'_source': {}, u'_index': u'movies'}, {u'_score': 1.0, u'_type': u'movie', u'_id': u'1', u'_source': {}, u'_index': u'movies'}], u'total': 2, u'max_score': 1.0}, u'_shards': {u'successful': 1, u'failed': 0, u'total': 1}, u'took': 2, u'timed_out': False}
我尝试了不同版本的过滤器路径和源字段列表,但似乎无法正确使用。
您可以应用源过滤:
messages = es.search(index="movies", _source=["title"])
但您仍然需要解析响应。为此,您可以执行以下操作:
titles = [hit["title"] for hit in messages["hits"]["hits"]["_source"]]]
elasticsearch-py API(据我所知)中没有任何内容可以简化您从 Elasticsearch 获得的相当冗长的响应。
您现在可以在搜索功能中使用 _source_exclued
和 _source_include
kwargs 来限制返回的字段。
所以像这样:
messages = es.search(index="movies", _source=["title"], _source_include=['title'])
我遇到了类似的问题,我就是这样解决的。我在有点不同的上下文中需要它 - 我不得不在循环的后面使用有关标题的信息:
res = es.search(index="movies", body={"query": {"match_all": {}}})
for hit in res['hits']['hits']:
title = hit['_source'].get('title')
我正在使用 elasticsearch-py(es 版本是 2.3)并且想 return 只是索引中所有文档中的 'title' 字段映射:演员,导演,类型、情节、标题、年份。
我目前正在尝试 messages = es.search(index="movies", _source=['hits.hits.title'])
,得到的响应是:
{u'hits': {u'hits': [{u'_score': 1.0, u'_type': u'movie', u'_id': u'tt0116996', u'_source': {}, u'_index': u'movies'}, {u'_score': 1.0, u'_type': u'movie', u'_id': u'1', u'_source': {}, u'_index': u'movies'}], u'total': 2, u'max_score': 1.0}, u'_shards': {u'successful': 1, u'failed': 0, u'total': 1}, u'took': 2, u'timed_out': False}
我尝试了不同版本的过滤器路径和源字段列表,但似乎无法正确使用。
您可以应用源过滤:
messages = es.search(index="movies", _source=["title"])
但您仍然需要解析响应。为此,您可以执行以下操作:
titles = [hit["title"] for hit in messages["hits"]["hits"]["_source"]]]
elasticsearch-py API(据我所知)中没有任何内容可以简化您从 Elasticsearch 获得的相当冗长的响应。
您现在可以在搜索功能中使用 _source_exclued
和 _source_include
kwargs 来限制返回的字段。
所以像这样:
messages = es.search(index="movies", _source=["title"], _source_include=['title'])
我遇到了类似的问题,我就是这样解决的。我在有点不同的上下文中需要它 - 我不得不在循环的后面使用有关标题的信息:
res = es.search(index="movies", body={"query": {"match_all": {}}})
for hit in res['hits']['hits']:
title = hit['_source'].get('title')