資料匯總:
偏向理論原理 方面
https://www.cnblogs.com/lit10050528/p/12178822.html
https://www.cnblogs.com/Ace-suiyuan008/p/9958331.html
https://blog.csdn.net/weixin_42633131/article/details/82873731
https://zhuanlan.zhihu.com/p/109578675
http://www.ruanyifeng.com/blog/2017/08/elasticsearch.html
Es 是一個(gè)專門做搜索的數(shù)據(jù)庫系統(tǒng)
Elasticsearch是一個(gè)基于Lucene的搜索服務(wù)器
4.4 Lucene、Solr、Elasticsearch關(guān)系
Lucene:底層的API,工具包
Lucene資料
Solr:基于Lucene開發(fā)的企業(yè)級的搜索引擎產(chǎn)品
Elasticsearch:基于Lucene開發(fā)的企業(yè)級的搜索引擎產(chǎn)品
Solr 和Elasticsearch 是并列的兩種產(chǎn)品。
全文檢索:
計(jì)算機(jī)索引程序通過掃描文章中國呢的每一個(gè)詞,對每一個(gè)詞建立一個(gè)索引,并指明該詞在文章中出現(xiàn)的位置和次數(shù),當(dāng)用戶查詢時(shí),檢測程序就根據(jù)事先建立的索引進(jìn)行查找,并將查找的結(jié)果反饋給用戶的檢索方式 。
lucene 全文檢索就是對文檔的全部內(nèi)容進(jìn)行分詞,然后對所有的單詞建立倒排索引的過程。
kibana 是es 客戶端
Kibana是一個(gè)針對Elasticsearch的開源分析及可視化平臺,用來搜索、查看交互存儲在Elasticsearch索引中的數(shù)據(jù)。使用Kibana,可以通過各種圖表進(jìn)行高級數(shù)據(jù)分析及展示。
Kibana讓海量數(shù)據(jù)更容易理解。它操作簡單,基于瀏覽器的用戶界面可以快速創(chuàng)建儀表板(dashboard)實(shí)時(shí)顯示Elasticsearch查詢動(dòng)態(tài)。

- INDEX
Elastic 會索引所有字段,經(jīng)過處理后寫入一個(gè)反向索引(Inverted Index)。查找數(shù)據(jù)的時(shí)候,直接查找該索引。
所以,Elastic 數(shù)據(jù)管理的頂層單位就叫做 Index(索引)。它是單個(gè)數(shù)據(jù)庫的同義詞。每個(gè) Index (即數(shù)據(jù)庫)的名字必須是小寫。
-
Document
Index 里面單條的記錄稱為 Document(文檔)。許多條 Document 構(gòu)成了一個(gè) Index。
Document 使用 JSON 格式表示,下面是一個(gè)例子。
image.png Type
Document 可以分組,比如weather這個(gè) Index 里面,可以按城市分組(北京和上海),也可以按氣候分組(晴天和雨天)。這種分組就叫做 Type,它是虛擬的邏輯分組,用來過濾 Document。
Elastic 6.x 版只允許每個(gè) Index 包含一個(gè) Type,7.x 版將會徹底移除 Type
類型(Type)
在 7.0 之前,每一個(gè)索引是可以設(shè)置多個(gè) Types 的,每個(gè) Type 會擁有相同結(jié)構(gòu)的文檔,但是在 6.0 開始,Type 已經(jīng)被廢除,在 7.0 開始,一個(gè)索引只能創(chuàng)建一個(gè) Type,也就是 _doc
安裝es 和kibana
參考資料
es 數(shù)據(jù)庫語法 (在kibana 客戶端的操作 )
在這個(gè)之前需要再一次重新贅述一遍,
index 相當(dāng)于是數(shù)據(jù)庫;
文檔 相當(dāng)于是一行數(shù)據(jù);
- 建立index
PUT /wangyd/
{
"settings":{
"index":{
"number_of_shards": 3,
"number_of_replicas": 0
}
}
"number_of_shards": 分片數(shù)
"number_of_replicas": 被分?jǐn)?shù)
wangyd是創(chuàng)建的索引名
也可以使用默認(rèn)配置
put wangyd
# 或者 put /wangyd/
結(jié)果執(zhí)行后為:

注意在7.0之后可以直接創(chuàng)建并添加文檔 ,用post
因?yàn)?版本之后,ES不再支持一個(gè)索引(index)可以創(chuàng)建多個(gè)類型(type),所以cmcc/后邊不再需要寫入類型名稱,而是統(tǒng)一使用_create代替即可,同樣的,查詢操作使用_doc代替即可,右側(cè)看到如下圖所示類似形式表示創(chuàng)建成功

- 刪除index
delete wangyd
# 或者delete /wangyd/
- 查看index 配置
GET /wangyd/_settings
- 增加文檔
put /lib/user/1
{
"first_name":"Jane",
"last_name":"Smith",
"age":32,
"about":"I like to collect rock albums",
"interests":["music"]
user為該文檔的類型
1是該文檔的id
也可不指定id,但需要使用post命令,id會自動(dòng)生成 [!!!!!!!!!!]
post /lib/user
{
"first_name":"zhou",
"last_name":"Lucky",
"age":18,
"about":"I like to collect rock albums",
"interests":["music"]
- 更新文檔
1.直接覆蓋
PUT /lib/user/1
{
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 36,
"about" : "I like to collect rock albums",
"interests": [ "music" ]
}
2.只更新需要更新的字段(post)
POST /lib/user/1/_update
{
"doc":{
"age":33
}
- 查詢
- 按照條件查詢

-
1 或 邏輯(should)
image.png -
2 并邏輯 (must)
image.png -
3 范圍查詢并排序
image.png
更多查詢方法
https://zhuanlan.zhihu.com/p/46407263
- 6.條件刪除文檔
post lib/user/_delete_by_query
{
"query": {
"match": { "first_name": "zhou"}
}
利用類, elasticsearch-dsl 進(jìn)行存儲數(shù)據(jù)
from elasticsearch_dsl import connections
from datetime import datetime
from elasticsearch_dsl import Document, Date, Text,Float,Keyword
connections.create_connection(hosts=['ip:端口'], timeout=60)
class My_class(Document):
index_code = Keyword()
fund_code = Keyword()
fund_cname = Text()
update_time = Date()
class Index:
name = "name_fix-*"
settings = {
"number_of_shards": 2
}
def save(self, **kwargs):
# assign now if no timestamp given
if not self.update_time:
self.update_time = datetime.now()
# override the index to go to the proper timeslot
kwargs['index'] = self.update_time.strftime('name_fix-%Y%m%d')
return super().save(**kwargs)
使用方法 :
- 在index 類中對name 聲明,即通過這種方式創(chuàng)建對index 的名稱都是name-fix- z作為前綴開始的。在save 的函數(shù)中,kwargs['index'] = self.update_time.strftime('name_fix-%Y%m%d')是準(zhǔn)確的對index 進(jìn)行命名。
- 在kibana(es 的客戶端)中不需要類似sql 數(shù)據(jù)庫先建表,然后再插入,這里在My_class 中聲明幾個(gè)變量后,可以直接創(chuàng)建并存入。
- 我們在使用中需要對My_class 中的幾個(gè)變量根據(jù)實(shí)際進(jìn)行修改。
問題匯總
空值 處理原則
空值的就不傳入進(jìn)去,參加python 的參數(shù) 章節(jié)第一次插入數(shù)據(jù)的時(shí)候會自動(dòng)初始化,在數(shù)據(jù)庫中建立index ,但是有的行會沒有數(shù)據(jù),就導(dǎo)致建立的mapping 部分是沒有相應(yīng)的字段的 。所以解決方法是,在插入數(shù)據(jù)之前先進(jìn)行初始化,建立mapping,然后再去插入值
# 1. create the mappings in elasticsearch
index_suffix = '2020'
index_name = DemoDoc._index._name[:-1] + index_suffix
if not indices_client.exists(index_name):
DemoDoc.init(index=index_name)
# 2. once, as part of application setup, during deploy/migrations:
# template用來查詢
template_name = 'strategy-doc-demo'
docTemplate = DemoDoc._index.as_template(template_name)
# 生成indexTemplate
if not indices_client.exists_index_template(template_name):
docTemplate.save()
# 3. 創(chuàng)建DemoDoc文檔
doc = DemoDoc(meta={'id': 43}, title='Hello world!', tags=['test'])
doc.body = ''' looong text '''
doc.published_from = datetime.now()
# 4.1 單個(gè)保存
doc.save()



