gpt4 book ai didi

python - 当在查询中使用fields选项时,elasticsearch-py搜索返回列表对象

转载 作者:行者123 更新时间:2023-12-03 00:16:21 24 4
gpt4 key购买 nike

中使用Elasticsearch.search方法elasticsearch-py

搜索1:在搜索中不使用fields选项:

search_body =   { 
"filter" :
{
"term" :
{
"user.id" : 19900726
}
},
"size" : 1
}

结果如下:
{u'_shards': {u'failed': 0, u'successful': 5, u'total': 5},
u'hits': {u'hits': [{u'_id': u'657184533922451456',
u'_index': u'tweets-index-2016-9',
u'_score': 1.0,
u'_source': {u'created_at': u'2015-10-22T13:19:37',
u'entities': {u'hashtags': [{u'indices': [5, 13], u'text': u'IndvsSA'}],
u'symbols': [],
u'urls': [],
u'user_mentions': []},
u'favorite_count': 0,
u'favorited': False,
u'id': 657184533922451456,
u'id_str': u'657184533922451456',
u'is_quote_status': False,
u'lang': u'und',
u'outdated': u'No',
u'retweet_count': 0,
u'retweeted': False,
u'saved_at': u'2016-03-03T15:02:06.157149',
u'source': u'<a href="http://twitter.com/download/android" rel="nofollow">Twitter for Android</a>',
u'text': u'Out. #IndvsSA',
u'truncated': False,
u'user': {u'id': 19900726, u'id_str': u'19900726'}},
u'_type': u'tweet'}],
u'max_score': 1.0,
u'total': 2981},
u'timed_out': False,
u'took': 10}

搜索2:设置了 fields搜索选项:
search_body =   { 
"filter" :
{
"term" :
{
"user.id" : 19900726
}
},
"fields" :
[
'id',
'created_at',
'retweet_count',
'favorite_count'
],
"size" : 1
}

结果变为:
{u'_shards': {u'failed': 0, u'successful': 5, u'total': 5},
u'hits': {u'hits': [{u'_id': u'657184533922451456',
u'_index': u'tweets-index-2016-9',
u'_score': 1.0,
u'_type': u'tweet',
u'fields': {u'created_at': [u'2015-10-22T13:19:37'],
u'favorite_count': [0],
u'id': [657184533922451456],
u'retweet_count': [0]}}],
u'max_score': 1.0,
u'total': 2981},
u'timed_out': False,
u'took': 6}

在第二次搜索中,为什么每个字段都以 list的形式返回,而在搜索1中返回了 longstring(索引时使用的类型)。如何纠正 搜索2 中的行为?

最佳答案

您需要使用_source(source filtering)代替 fields ,不建议使用后者。

     search_body =   { 
"filter" :
{
"term" :
{
"user.id" : 19900726
}
},
'_source' : <--- change this
[
'id',
'created_at',
'retweet_count',
'favorite_count'
],
"size" : 1
}

关于python - 当在查询中使用fields选项时,elasticsearch-py搜索返回列表对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35893586/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com