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

标签 python elasticsearch elasticsearch-py

中使用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/

相关文章:

Python Pandas - 替换与正则表达式匹配的单元格值

python - 如何在 QMenu 中隐藏子菜单

elasticsearch - 分析器自动完成名称

elasticsearch - 有效地获取 Elasticsearch 索引中的所有文档

javascript - 在 Elasticsearch 中,是否可以对索引执行临时 POST?

python - 在单元测试中模拟解析器对象

python - arcgis 联合 python 错误

elasticsearch - 索引 :admin/exists 的错误 Search Guard 未初始化 (SG11)

python - 从Elasticseacrch-dsl-py中删除默认的 'match_all'查询

python - elasticsearch-py搜索查询比等效的curl慢得多