文档章节

elaselasticsearch2.3 python search超时bug

 张欢19933
发布于 2017/08/17 13:44
字数 886
阅读 802
收藏 0

#程序员薪资揭榜#你做程序员几年了?月薪多少?发量还在么?>>>

本地安装elasticsearch python包

pip install elasticsearch==2.3.0

注意指明版本号

建立连接

from elasticsearch import Elasticsearch
es = Elasticsearch()
或
es = Elasticsearch(
    [
        'ip1:9200','ip2:9200'
    ]
)

不指明ip和端口号,默认连接本地9200端口,连接多台可以保证高可用

执行操作

特别说明search操作

官网函数说明

search(*args, **kwargs)
Execute a search query and get back search hits that match the query. http://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html

Parameters:	
index – A comma-separated list of index names to search; use _all or empty string to perform the operation on all indices

doc_type – A comma-separated list of document types to search; leave empty to perform the operation on all types

body – The search definition using the Query DSL

_source – True or false to return the _source field or not, or a list of fields to return

_source_exclude – A list of fields to exclude from the returned _source field

_source_include – A list of fields to extract and return from the _source field

allow_no_indices – Whether to ignore if a wildcard indices expression resolves into no concrete indices. (This includes _all string or when no indices have been specified)

analyze_wildcard – Specify whether wildcard and prefix queries should be analyzed (default: false)

analyzer – The analyzer to use for the query string

default_operator – The default operator for query string query (AND or OR), default ‘OR’, valid choices are: ‘AND’, ‘OR’

df – The field to use as default where no field prefix is given in the query string

expand_wildcards – Whether to expand wildcard expression to concrete indices that are open, closed or both., default ‘open’, valid choices are: ‘open’, ‘closed’, ‘none’, ‘all’

explain – Specify whether to return detailed information about score computation as part of a hit

fielddata_fields – A comma-separated list of fields to return as the field data representation of a field for each hit

fields – A comma-separated list of fields to return as part of a hit

from_ – Starting offset (default: 0)

ignore_unavailable – Whether specified concrete indices should be ignored when unavailable (missing or closed)

lenient – Specify whether format-based query failures (such as providing text to a numeric field) should be ignored

lowercase_expanded_terms – Specify whether query terms should be lowercased

preference – Specify the node or shard the operation should be performed on (default: random)

q – Query in the Lucene query string syntax

request_cache – Specify if request cache should be used for this request or not, defaults to index level setting

routing – A comma-separated list of specific routing values

scroll – Specify how long a consistent view of the index should be maintained for scrolled search

search_type – Search operation type, valid choices are: ‘query_then_fetch’, ‘dfs_query_then_fetch’, ‘count’, ‘scan’

size – Number of hits to return (default: 10)

sort – A comma-separated list of <field>:<direction> pairs

stats – Specific ‘tag’ of the request for logging and statistical purposes

suggest_field – Specify which field to use for suggestions

suggest_mode – Specify suggest mode, default ‘missing’, valid choices are: ‘missing’, ‘popular’, ‘always’

suggest_size – How many suggestions to return in response

suggest_text – The source text for which the suggestions should be returned

terminate_after – The maximum number of documents to collect for each shard, upon reaching which the query execution will terminate early.

timeout – Explicit operation timeout

track_scores – Whether to calculate and return scores even if they are not used for sorting

version – Specify whether to return document version as part of a hit

代码

rs = es.search(index=index,body=body,size=0,timeout="60s")

当我们执行search的时候会报read timeout的错误

Traceback (most recent call last):
  File "es_test.py", line 39, in <module>
    rs = es.search(index=index,body=body,size=0,timeout="60s")
  File "/home/xiaoju/weijian/anaconda2/lib/python2.7/site-packages/elasticsearch/client/utils.py", line 69, in _wrapped
    return func(*args, params=params, **kwargs)
  File "/home/xiaoju/weijian/anaconda2/lib/python2.7/site-packages/elasticsearch/client/__init__.py", line 548, in search
    doc_type, '_search'), params=params, body=body)
  File "/home/xiaoju/weijian/anaconda2/lib/python2.7/site-packages/elasticsearch/transport.py", line 329, in perform_request
    status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout)
  File "/home/xiaoju/weijian/anaconda2/lib/python2.7/site-packages/elasticsearch/connection/http_urllib3.py", line 102, in perform_request
    raise ConnectionTimeout('TIMEOUT', str(e), e)
elasticsearch.exceptions.ConnectionTimeout: ConnectionTimeout caused by - ReadTimeoutError(HTTPConnectionPool(host='bigdata-ara-ser200.gz01', port=8200): Read timed out. (read timeout=10))

我们明明设置了timeout为60s,为什么报错显示为10s,timeout为什么没有生效,我百思不得其解,后来我按照报错在源码包里的位置找到对应的函数

如图所示,它取得是request_timeout这个参数,但是search这个函数的参数里却只有timeout这个参数,估计是官网没有更新这个bug,然后我们修改代码

rs = es.search(index=index,body=body,size=0,request_timeout=60)

这时候我们发现运行成功。

更多api请查看

http://elasticsearch-py.readthedocs.io/en/2.3.0/api.html#elasticsearch

© 著作权归作者所有

粉丝 48
博文 644
码字总数 369103
作品 0
海淀
私信 提问
加载中

评论(0)

Python中的Modules和pip

Modules 对module的理解 和Java中一个类就是一个文件不同(当然不考虑内部类),python中一个.py文件可以包含多个class。Modules的目的是代码的划分、管理以及代码重用。一个module是一个包含...

gangzz
2014/02/20
3K
1
Python3环境(Windows10)单独配置Spyder——记录我的吐血之路

由于近期项目要用到python,个人喜好spyder编译器,但是奈何python2的语法读取中文列表有很多很多的bug(多到我无力吐槽) 因此打算重新卸载了原来请大神安装的spyder自己再重新摸索一遍(事实...

osc_0zs17uxd
2018/05/28
12
0
RHEL7环境下安装python3.3

RHEL7环境下安装python3.3 背景:rhel7.3 系统中已经安装python2.7,现在要实现python的升级; 首先下载源tar包 http://www.python.org/ftp/python 下载完成后到下载目录下,解压 tar xf Pyt...

huanzi2017
2018/08/10
0
0
Python+Selenium笔记(一):环境配置+简单的例子

#环境配置基于windows操作系统 #学习selenium要有一些HTML和xpth的基础,完全不会的建议先花点时间学点基础(不然元素定位,特别是xpth可能看的有点懵) #HTML : http://www.runoob.com/html...

osc_e4gzv47r
2018/02/09
4
0
web.py 0.40 发布,小巧灵活的 Python 框架

web.py 0.40 发布了。web.py 是一个小巧灵活的 Python 框架,设计理念力求精简(Keep it simple and powerful),源码很简短,只提供一个框架所必须的东西,不依赖大量的第三方模块,它没有 ...

h4cd
2019/09/30
3.1K
5

没有更多内容

加载失败,请刷新页面

加载更多

屏蔽nginx默认页面或者openresty默认页面

openresty是对nginx做了一层封装,屏蔽nginx或者openresty的默认页面的方法是一样的,就是对nginx/html/index.html进行修改即可。 ssh nginx@126.10.*.* // 使用ssh进行登录,根据提示输入密...

osc_2wznp7fr
5分钟前
7
0
基于Python的arcgis二次开发和ENVI二次开发

https://www.cnblogs.com/jhlong/p/5394530.html https://search.bilibili.com/all?keyword=arcgis%20python...

osc_gkcftr6g
6分钟前
4
0
[Go] gorm 返回指定模型数据的处理方式

重新 var 声明一个变量,类型为包含指定字段的结构体。 查询的时候,还是使用原始模型类型的变量。 example: // For return data var retMember struct { Hash string `json...

osc_eoffv2le
7分钟前
3
0
Spring boot Access-Control-Allow-Origin 问题解决

import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.cors.CorsConfiguration;import or......

osc_3361hjxk
8分钟前
8
0
通过人才测评系统,对程序员岗位进行招聘测评

一、 程序员的基本工作内容 1、 负责项目组内的代码维护和更新迭代,保证研发效率,对于运营产品提出的需求应积极沟通并实现。 2、 规范相关开发文档等相关资料,对于有变更的代码和功能需求...

蛤蟆丸子
9分钟前
7
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部