elasticsearch使用笔记(一)
刚刚接触elasticsearch,真的是超好用了,本笔记主要记录使用Python调用es的基本操作
elasticsearch重启
重启一般都可以解决connection error之类的问题
- cd到elasricsearch文件路径下的bin文件中
- su es
- nohup ./elasticsearch >/dev/null 2>&1 &
- ps -ef|grep elasticsearch
- 搞定
加载
from elasticsearch import Elasticsearch, helpers
es = Elasticsearch(['http://localhost'], port=9200, timeout=60)
Python API 得到并修改mapping,setting
es.indices.get_mapping(index=index)
es.indices.get_settings(index=index)
es.indices.put_mapping(index=index, doc_type=type, body=new_mapping)
Python API 创建、删除索引
es.indices.create(index=index)
es.indices.delete(index=index)
Python API 批量导入数据
data = [{"_index": index,"_type": type, "_source":x } for x in raw_data]
helpers.bulk(self.es, data)
Python API 复制一个索引的数据至一个新的索引
helpers.reindex(client=client, source_index=source_index, target_index=target_index, target_client=client,
query= {"query": {"match_all": {} }, chunk_size=1000, scroll='15m')
Python API 搜索
results = es.search(index=index, body=query)
搜索并打印每段query的得分(explain)
es.search(index=index, body=query, explain=True)
但是速度会变慢
更换相似度模型
以为默认的是TFIDF,后来mapping设置为bm25之后发现没有差别。才晓得目前版本默认bm25相似度打分,之后用到再补充