6.elasticsearch遇到的bug以及python对接elasticsearch

运行时错误

Could not find any executable java binary. Please install java in your PATH or set JAVA_HOME

安装java环境即可:

sudo wget --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u181-b13/96a7b8442fe848ef90c96a2fad6ed6d1/jdk-8u181-linux-x64.tar.gz

解压后,进入jdk目录下,执行以下命令

JAVA_HOME=/data/server/java/jdk1.8.0_181/   #你的jdk路径
JRE_HOME=$JAVA_HOME/jre
CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib:.
CLASSPATH=:$CLASSPATH:$JAVA_HOME/lib:$JRE_HOME/lib
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
source ~/.bashrc

由于elasticsearch默认分词器不支持中文分词,所以要下载中文分词插件https://github.com/medcl/elasticsearch-analysis-ik,找到对应版本的ik,复制zip压缩包路径,在elasticsearch目录下执行

 bin/plugin install 路径

ps:如果是es2.x版本及以下,需要在elasticsearch.yam文件中添加一行(注意首位有空格)

 index.analysis.analyzer.ik.type: "ik"

重启,配置基本完毕。

python环境下使用elasticsearch需要安装第三方库,到这里查找对应版本。官方API文档

#不传参,默认localhost:9200
es = Elasticsearch(hosts="http://58.87.113.211:9200")

创建索引

#ignore,忽略index already exist错误
es.indices.create(index = 'my-index',ignore = 400)

创建一个document

es.create(index='my-index',doc_type = "news",body={"title":title,"content":content})

搜索,查找title或content包含某值的文档,其他条件可以写在body中(DSL)

body = '{"query":{"multi_match":{"query":"%s","fields":["title","content"]}}}'%request.form["search"]
res = es.search(index="my-index", doc_type="news", body=body)

修改一个文档,index为全文修改,update为部分修改

es.update(index='news',doc_type = "news",body=body,id=news.id)

删除一个文档

es.delete(index='news',doc_type = "news",id=id)

查看一个文档是否存在

es.exists(index='news',doc_type = "news",id=id)

 

你可能感兴趣的:(elasticsearch)