文章目录
1 介绍
官方文档:
https://www.elastic.co/guide/en/enterprise-search-clients/python/7.17/index.html
pypi文档:
https://pypi.org/project/elasticsearch/7.17.0/
2 安装 连接
pip install elasticsearch==7.17.0
异步 async/await
pip install elasticsearch[async]==7.17.0
from elasticsearch import Elasticsearch
client = Elasticsearch(hosts=['http://192.168.56.20:9200'],
http_auth=("elastic", "密码"))
3 索引操作
3.1 创建索引
def create_index(index, doc, index_id):
client.create(index=index, document=doc, id=index_id)
doc = {
"mappings": {
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "long"
},
"birthday": {
"type": "date"
}
}
}
}
create_index("test6", doc, "1")
3.2 判断索引是否存在
def index_id_exists(index, id):
return client.exists(index=index, id=id)
if index_id_exists("test1", "2") == True:
print("索引存在")
else:
print("索引不存在")
4 新增数据
def add_to_es(index, doc, id):
try:
client.index(index=index, document=doc, id=id)
return '1'
except Exception as e:
print(e)
return '0'
doc = {
"name": "灰太狼",
"age": 22,
"birthday":"2000-02-02",
"tags": ["男"]
}
res = add_to_es("test1", doc, "10")
5 删除数据
def delete_by_index_and_id(index, id):
try:
res = client.delete(index=index, id=id)
print(res['_shards']['successful'])
return '1'
except Exception as e:
print(e)
return '0'
if delete_by_index_and_id("test1", "1") == "1":
print("删除成功")
else:
print("删除失败或不存在")
5 修改数据
def update_by_index_and_id(index, id, doc):
try:
client.update(index=index, id=id, doc=doc)
return '1'
except Exception as e:
print(e)
return '0'
doc = {
"name": "有勇气的牛排",
"age": 22,
"birthday": "2000-05-20",
"tags": ["男"]
}
res = update_by_index_and_id("test1", "1", doc=doc)
if res == "1":
print("更新成功")
else:
print("数据不存在")
6 查询数据
6.1 查询所有数据
查询 通过 索引->_id
def find_by_index_and_id(index, id):
try:
res = client.get(index=index, id=id)
return res
except Exception as e:
print(e)
return '0'
res = find_by_index_and_id("test1", "1")
print(res)
查询所有数据
# 查询 索引index 所有数据
async def find_by_index_all(self, index):
client = UtilES().es_connect()
body = {}
# item = ["uid", "ip"]
try:
# res = client.get(index=index, id=id)
res = client.search(index=index, body=body, scroll='5m', size=1000)
return res
except Exception as e:
print(e)
return '0'
6.2 search数据
def find_search_article(index, key, source, highlight=None):
"""
:param index: 索引 source = ["a_title"]
:param query: query
:param source: 取出的字段
:param highlight: 高亮
:return:
"""
query = {"bool": {"should": [{"match": {"a_title": key}}, {"match": {"a_content": key}}]}}
client = connect_elk()
try:
res = client.search(index=index, query=query, _source=source, highlight=highlight)
return res
except Exception as e:
print(e)
return '0'
res = find_search_article("article", "人民教育", ["a_title","a_html"], highlight_red())
if res != 0:
print(res["hits"])
total = res["hits"]["total"]["value"]
res = res["hits"]["hits"]
参考文档:
https://www.cnblogs.com/lshan/p/15510018.html
<p><h3>文章目录</h3><ul><ul><li><a href="#1__2">1 介绍</a></li><li><a href="#2___10">2 安装 连接</a></li><li><a href="#3__25">3 索引操作</a></li><ul><li><a href="#31__26">3.1 创建索引</a></li><li><a href="#32__53">3.2 判断索引是否存在</a></li></ul><li><a href="#4__67">4 新增数据</a></li><li><a href="#5__88">5 删除数据</a></li><li><a href="#5__109">5 修改数据</a></li><li><a href="#6__135">6 查询数据</a></li><ul><li><a href="#61__136">6.1 查询所有数据</a></li><li><a href="#62_search_170">6.2 search数据</a></li></ul></ul></ul></p>
<h2><a id="1__2"></a>1 介绍</h2>
<p>官方文档:<br />
https://www.elastic.co/guide/en/enterprise-search-clients/python/7.17/index.html</p>
<p>pypi文档:<br />
https://pypi.org/project/elasticsearch/7.17.0/</p>
<h2><a id="2___10"></a>2 安装 连接</h2>
<pre><div class="hljs"><code class="lang-python">pip install elasticsearch==<span class="hljs-number">7.17</span><span class="hljs-number">.0</span>
</code></div></pre>
<p>异步 async/await</p>
<pre><div class="hljs"><code class="lang-python">pip install elasticsearch[<span class="hljs-keyword">async</span>]==<span class="hljs-number">7.17</span><span class="hljs-number">.0</span>
</code></div></pre>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-keyword">from</span> elasticsearch <span class="hljs-keyword">import</span> Elasticsearch
client = Elasticsearch(hosts=[<span class="hljs-string">'http://192.168.56.20:9200'</span>],
http_auth=(<span class="hljs-string">"elastic"</span>, <span class="hljs-string">"密码"</span>))
</code></div></pre>
<h2><a id="3__25"></a>3 索引操作</h2>
<h3><a id="31__26"></a>3.1 创建索引</h3>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-keyword">def</span> <span class="hljs-title function_">create_index</span>(<span class="hljs-params">index, doc, index_id</span>):
client.create(index=index, document=doc, <span class="hljs-built_in">id</span>=index_id)
</code></div></pre>
<pre><div class="hljs"><code class="lang-powershell">doc = {
<span class="hljs-string">"mappings"</span>: {
<span class="hljs-string">"properties"</span>: {
<span class="hljs-string">"name"</span>: {
<span class="hljs-string">"type"</span>: <span class="hljs-string">"text"</span>
},
<span class="hljs-string">"age"</span>: {
<span class="hljs-string">"type"</span>: <span class="hljs-string">"long"</span>
},
<span class="hljs-string">"birthday"</span>: {
<span class="hljs-string">"type"</span>: <span class="hljs-string">"date"</span>
}
}
}
}
create_index(<span class="hljs-string">"test6"</span>, doc, <span class="hljs-string">"1"</span>)
</code></div></pre>
<h3><a id="32__53"></a>3.2 判断索引是否存在</h3>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-keyword">def</span> <span class="hljs-title function_">index_id_exists</span>(<span class="hljs-params">index, <span class="hljs-built_in">id</span></span>):
<span class="hljs-keyword">return</span> client.exists(index=index, <span class="hljs-built_in">id</span>=<span class="hljs-built_in">id</span>)
</code></div></pre>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-keyword">if</span> index_id_exists(<span class="hljs-string">"test1"</span>, <span class="hljs-string">"2"</span>) == <span class="hljs-literal">True</span>:
<span class="hljs-built_in">print</span>(<span class="hljs-string">"索引存在"</span>)
<span class="hljs-keyword">else</span>:
<span class="hljs-built_in">print</span>(<span class="hljs-string">"索引不存在"</span>)
</code></div></pre>
<h2><a id="4__67"></a>4 新增数据</h2>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-keyword">def</span> <span class="hljs-title function_">add_to_es</span>(<span class="hljs-params">index, doc, <span class="hljs-built_in">id</span></span>):
<span class="hljs-comment"># 重复添加,数据覆盖</span>
<span class="hljs-keyword">try</span>:
client.index(index=index, document=doc, <span class="hljs-built_in">id</span>=<span class="hljs-built_in">id</span>)
<span class="hljs-keyword">return</span> <span class="hljs-string">'1'</span>
<span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
<span class="hljs-built_in">print</span>(e)
<span class="hljs-keyword">return</span> <span class="hljs-string">'0'</span>
</code></div></pre>
<pre><div class="hljs"><code class="lang-python">doc = {
<span class="hljs-string">"name"</span>: <span class="hljs-string">"灰太狼"</span>,
<span class="hljs-string">"age"</span>: <span class="hljs-number">22</span>,
<span class="hljs-string">"birthday"</span>:<span class="hljs-string">"2000-02-02"</span>,
<span class="hljs-string">"tags"</span>: [<span class="hljs-string">"男"</span>]
}
res = add_to_es(<span class="hljs-string">"test1"</span>, doc, <span class="hljs-string">"10"</span>)
</code></div></pre>
<h2><a id="5__88"></a>5 删除数据</h2>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-keyword">def</span> <span class="hljs-title function_">delete_by_index_and_id</span>(<span class="hljs-params">index, <span class="hljs-built_in">id</span></span>):
<span class="hljs-keyword">try</span>:
res = client.delete(index=index, <span class="hljs-built_in">id</span>=<span class="hljs-built_in">id</span>)
<span class="hljs-built_in">print</span>(res[<span class="hljs-string">'_shards'</span>][<span class="hljs-string">'successful'</span>])
<span class="hljs-keyword">return</span> <span class="hljs-string">'1'</span>
<span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
<span class="hljs-built_in">print</span>(e)
<span class="hljs-keyword">return</span> <span class="hljs-string">'0'</span>
</code></div></pre>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-comment"># 删除数据</span>
<span class="hljs-keyword">if</span> delete_by_index_and_id(<span class="hljs-string">"test1"</span>, <span class="hljs-string">"1"</span>) == <span class="hljs-string">"1"</span>:
<span class="hljs-built_in">print</span>(<span class="hljs-string">"删除成功"</span>)
<span class="hljs-keyword">else</span>:
<span class="hljs-built_in">print</span>(<span class="hljs-string">"删除失败或不存在"</span>)
</code></div></pre>
<h2><a id="5__109"></a>5 修改数据</h2>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-keyword">def</span> <span class="hljs-title function_">update_by_index_and_id</span>(<span class="hljs-params">index, <span class="hljs-built_in">id</span>, doc</span>):
<span class="hljs-keyword">try</span>:
client.update(index=index, <span class="hljs-built_in">id</span>=<span class="hljs-built_in">id</span>, doc=doc)
<span class="hljs-keyword">return</span> <span class="hljs-string">'1'</span>
<span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
<span class="hljs-built_in">print</span>(e)
<span class="hljs-keyword">return</span> <span class="hljs-string">'0'</span>
</code></div></pre>
<pre><div class="hljs"><code class="lang-python">doc = {
<span class="hljs-string">"name"</span>: <span class="hljs-string">"有勇气的牛排"</span>,
<span class="hljs-string">"age"</span>: <span class="hljs-number">22</span>,
<span class="hljs-string">"birthday"</span>: <span class="hljs-string">"2000-05-20"</span>,
<span class="hljs-string">"tags"</span>: [<span class="hljs-string">"男"</span>]
}
res = update_by_index_and_id(<span class="hljs-string">"test1"</span>, <span class="hljs-string">"1"</span>, doc=doc)
<span class="hljs-keyword">if</span> res == <span class="hljs-string">"1"</span>:
<span class="hljs-built_in">print</span>(<span class="hljs-string">"更新成功"</span>)
<span class="hljs-keyword">else</span>:
<span class="hljs-built_in">print</span>(<span class="hljs-string">"数据不存在"</span>)
</code></div></pre>
<h2><a id="6__135"></a>6 查询数据</h2>
<h3><a id="61__136"></a>6.1 查询所有数据</h3>
<p>查询 通过 索引->_id</p>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-keyword">def</span> <span class="hljs-title function_">find_by_index_and_id</span>(<span class="hljs-params">index, <span class="hljs-built_in">id</span></span>):
<span class="hljs-keyword">try</span>:
res = client.get(index=index, <span class="hljs-built_in">id</span>=<span class="hljs-built_in">id</span>)
<span class="hljs-keyword">return</span> res
<span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
<span class="hljs-built_in">print</span>(e)
<span class="hljs-keyword">return</span> <span class="hljs-string">'0'</span>
</code></div></pre>
<pre><div class="hljs"><code class="lang-python">res = find_by_index_and_id(<span class="hljs-string">"test1"</span>, <span class="hljs-string">"1"</span>)
<span class="hljs-built_in">print</span>(res)
</code></div></pre>
<p>查询所有数据</p>
<pre><div class="hljs"><code class="lang-shell"><span class="hljs-meta"># </span><span class="language-bash">查询 索引index 所有数据</span>
async def find_by_index_all(self, index):
client = UtilES().es_connect()
body = {}
<span class="hljs-meta"> # </span><span class="language-bash">item = [<span class="hljs-string">"uid"</span>, <span class="hljs-string">"ip"</span>]</span>
try:
# res = client.get(index=index, id=id)
res = client.search(index=index, body=body, scroll='5m', size=1000)
return res
except Exception as e:
print(e)
return '0'
</code></div></pre>
<h3><a id="62_search_170"></a>6.2 search数据</h3>
<pre><div class="hljs"><code class="lang-python"><span class="hljs-keyword">def</span> <span class="hljs-title function_">find_search_article</span>(<span class="hljs-params">index, key, source, highlight=<span class="hljs-literal">None</span></span>):
<span class="hljs-string">"""
:param index: 索引 source = ["a_title"]
:param query: query
:param source: 取出的字段
:param highlight: 高亮
:return:
"""</span>
<span class="hljs-comment"># should: 条件满足一个即可</span>
query = {<span class="hljs-string">"bool"</span>: {<span class="hljs-string">"should"</span>: [{<span class="hljs-string">"match"</span>: {<span class="hljs-string">"a_title"</span>: key}}, {<span class="hljs-string">"match"</span>: {<span class="hljs-string">"a_content"</span>: key}}]}}
<span class="hljs-comment"># query = {"bool": {"should": [{"match": key}]}}</span>
client = connect_elk()
<span class="hljs-keyword">try</span>:
res = client.search(index=index, query=query, _source=source, highlight=highlight)
<span class="hljs-keyword">return</span> res
<span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
<span class="hljs-built_in">print</span>(e)
<span class="hljs-keyword">return</span> <span class="hljs-string">'0'</span>
</code></div></pre>
<pre><div class="hljs"><code class="lang-python">res = find_search_article(<span class="hljs-string">"article"</span>, <span class="hljs-string">"人民教育"</span>, [<span class="hljs-string">"a_title"</span>,<span class="hljs-string">"a_html"</span>], highlight_red())
<span class="hljs-keyword">if</span> res != <span class="hljs-number">0</span>:
<span class="hljs-built_in">print</span>(res[<span class="hljs-string">"hits"</span>])
total = res[<span class="hljs-string">"hits"</span>][<span class="hljs-string">"total"</span>][<span class="hljs-string">"value"</span>]
res = res[<span class="hljs-string">"hits"</span>][<span class="hljs-string">"hits"</span>]
</code></div></pre>
<p>参考文档:<br />
https://www.cnblogs.com/lshan/p/15510018.html</p>
留言