Elasticsearch7.7 基礎教程 1
以下簡稱es7.7
es7.7的安裝:
1 官網下載 https://www.elastic.co/cn/downloads/elasticsearch
2 解壓文件
3 在安裝文件夾下的bin目錄下啓動windows 批處理文件
(elasticsearch-7.7.0-windows-x86_64\elasticsearch-7.7.0\bin)
4 啓動後訪問http://localhost:9200/ 若看見對應的頁面出現:
{
"name" : "LAPTOP-0V6FD18V",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "epUEbKDwRAKVZqVIx9boSw",
"version" : {
"number" : "7.7.0",
"build_flavor" : "default",
"build_type" : "zip",
"build_hash" : "81a1e9eda8e6183f5237786246f6dced26a10eaf",
"build_date" : "2020-05-12T02:01:37.602180Z",
"build_snapshot" : false,
"lucene_version" : "8.5.1",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
即安裝成功
注意
啓動的時候可能會出現種種問題 看安裝目錄下的日誌文件 解決問題
我遇到的問題
1 java版本不對應
2 堆棧異常
前者將jdk 由11 調整到 14成功解決
後者重啓解決
圖形化界面head的安裝
由於es沒有圖形化操作界面 所以要安裝head圖形化界面
1 下載位置:https://github.com/mobz/elasticsearch-head/tree/v5.0.0
2 下載之後開始解壓縮到任意的目錄 但是要和es7.7 的安裝目錄分開
3 下載node.js 並安裝
4 node -v 出現版本號 即安裝成功node.js
5 head界面 需要依賴於 grunt 需要npm全局安裝grant
6 全局安裝grunt的命令: npm install -g grunt-cil 安裝速度太慢記得換源
7 進入elasticsearch-head-master 目錄中 安裝相關的依賴 npm install
8 啓動 grunt server
9 地址 http://localhost:9100/
10 在es7.7 的安裝目錄下的H:\elasticsearch-7.7.0-windows-x86_64\elasticsearch-7.7.0\config 下的 elasticsearch.yml 文件 在文件末追加
http.cors.enabled: true
http.cors.allow-origin: "*"
這兩行 作用是: 允許 跨域連接
11 在 http://localhost:9100/ 的連接框中輸入 http://localhost:9200/ (es7.7 的地址)
es中的幾個名詞解釋
index (索引) 相當於數據庫中的 database
type (類型) 相當於數據庫中的table
document (文檔) 相當於數據庫中的row (記錄)
fields (字段) 相當於數據庫中的column (字段)
mapping 對處理數據的方式和規則做一些限制
分片和複製 (分佈式中將數據進行分片以及備份)
安裝postman
目的是方便進行發送各種請求
下載位置https://www.postman.com/downloads/ 傻瓜式安裝 沒有難度
創建index+index下的type請求:
url(post)
http://localhost:9200/blog1?include_type_name=true
url 的含義是 創建blog1 這個index
put請求包含的json文件
{
"mappings": {
"article": {
"properties": {
"id": {
"type": "long",
"store": true,
"index": false
},
"title": {
"type": "text",
"store": true,
"index": true,
"analyzer": "standard"
},
"content": {
"type": "text",
"store": true,
"index": true,
"analyzer": "standard"
}
}
}
}
}
json文件的含義是 創建一個article type 裏面包含字段 id title content
爲type添加一個document
url(post)
http://localhost:9200/blog1/article/3
url的含義是 爲 blog1 (index) 中的 article (type)
添加主鍵爲3 的一條document (數據庫中的記錄)
json
{
"id":3,
"title":"孔小格",
"content":"孔小格"
}
json 中的id 實際上指的是 定義字段的id 和上面的主鍵要分開
刪除一條document 就是選中一條document 通過postman 發送 del請求即可
修改一條document 就是創建一條document 只要將主鍵對應起來 會自動覆蓋
對某一個type(table)進行搜索(三種搜索方式)
1 關鍵字搜索
url(post)
http://localhost:9200/blog/hello/_search
json
{
"query":{
"term":{
"title":"孔"
}
}
}
2 id直接查找
url(get)
http://localhost:9200/blog1/article/1
3 query_string 查找
query的特點:對條件進行先分詞 例如“小孔” 會先分詞爲“小”和“孔” 進行命中
url(post)
http://localhost:9200/blog1/article/_search
json
{
"query":{
"query_string":{
"default_field":"title",
"query":"今天小孔很開心"
}
}
}
es7中的分析器(standard)
url(post)
http://localhost:9200/_analyze
json
{
"analyzer": "standard",
"text":"小孔"
}
顯示結果
{
"tokens": [
{
"token": "小",
"start_offset": 0,
"end_offset": 1,
"type": "<IDEOGRAPHIC>",
"position": 0
},
{
"token": "孔",
"start_offset": 1,
"end_offset": 2,
"type": "<IDEOGRAPHIC>",
"position": 1
}
]
}
es7 集成 IK 分析器
集成步驟:
1 下載ik的zip 下載地址
https://github.com/medcl/elasticsearch-analysis-ik/releases
2 解壓縮 放到es安裝下的plugins 文件夾下
3 將文件夾改名爲 ik_analyzer (這一步並沒有實質性的用途 只是爲了方便辨析這是ik分析器)
使用步驟
集成好了之後,使用方法和 standard 一樣 只不過把 json 中的 “analyzer”: “standard”, 改爲
“analyzer”: “ik_smart”,
或者
“analyzer”: “ik_max_word”,
這是兩種不同。0算法