上一篇我們已經安裝好了es+kibana
這裡我們将簡單的說一下增删查的使用
solr和elasticsearch差別:
1、solr建立索引庫慢、查詢快 ||||||||||| elasticsearch建立索引快、查詢慢
2、elasticsearch支援分布式,solr可以使用zookeeper做分布式
elasticsearch概念:
node:節點,部署服務的伺服器
cluster:叢集, 多個節點組成的架構
index: 索引庫,相當于關系資料庫中的database
shards:索引分片數,預設為5
replicas:索引副本,預設為1
type:索引類型,相當于關系資料庫中的table
document:文檔,相當于關系資料庫中的row
_index:文檔對應的索引
_type: 資料類型
_id: 文檔的唯一辨別
elasticsearch請求方式:
GET: 查詢資料
POST: 查詢資料、新增資料、修改資料
PUT: 新增資料、修改資料
DELETE:删除資料
資料類型
設定index 插入資料
PUT estest1
{
"settings": {
"number_of_replicas": 1,
"number_of_shards": 5
},
"mappings": {
"user":{
"properties": {
"id":{
"type": "long"
},
"name":{
"type": "text",
"analyzer": "standard"
},
"age":{
"type": "long"
}
}
}
}
}
DELETE estest1
PUT estest1/user/1
{
"id":2,
"name":"張三",
"age":30
}
post estest1/user
{
"id":1,
"name":"張4",
"age":40
}
GET estest1/_search
修改資料需要指定資料的id
PUT estest1/user/1
{
"name":"update"
}
查詢全部索引 GET _cat/indices
查詢
分為過濾查詢:bool+filter查詢比對,但是不打分,多為精準查詢
比對查詢:使用match,打分機制,0-1
range子句 作用于數字範圍查詢 get大于 let小于
GET estest1/user/_search
{
"query": {
"range": {
"age": {
"gte": 20,
"lte": 40
}
}
}
}
組合查詢:同時多個字段進行比對檢索,使用bool子句:
must:必須符合條件,(肯定包含)
must_not:必須不比對什麼條件(肯定不包含)
should:文檔符合條件的将添加score得分,
filter:使用過濾來查詢
分頁參數(與query同級)
from:分頁開始位置
size:分頁大小
中文分詞器
中文分詞我們使用的都是ik_max_word 也就是最大粒度的分詞
我們來測試一下啊
POST _analyze
{
"analyzer": "ik_max_word",
"text": "中華人民共和國"
}
結果
{
"tokens": [
{
"token": "中華人民共和國",
"start_offset": 0,
"end_offset": 7,
"type": "CN_WORD",
"position": 0
},
{
"token": "中華人民",
"start_offset": 0,
"end_offset": 4,
"type": "CN_WORD",
"position": 1
},
{
"token": "中華",
"start_offset": 0,
"end_offset": 2,
"type": "CN_WORD",
"position": 2
},
{
"token": "華人",
"start_offset": 1,
"end_offset": 3,
"type": "CN_WORD",
"position": 3
},
{
"token": "人民共和國",
"start_offset": 2,
"end_offset": 7,
"type": "CN_WORD",
"position": 4
},
{
"token": "人民",
"start_offset": 2,
"end_offset": 4,
"type": "CN_WORD",
"position": 5
},
{
"token": "共和國",
"start_offset": 4,
"end_offset": 7,
"type": "CN_WORD",
"position": 6
},
{
"token": "共和",
"start_offset": 4,
"end_offset": 6,
"type": "CN_WORD",
"position": 7
},
{
"token": "國",
"start_offset": 6,
"end_offset": 7,
"type": "CN_CHAR",
"position": 8
}
]
}
一個小測試
我們以 "中華人民共和國"為例 分别添加 中華1,中華 ,中華人名共和國三條資料
7.5.2版本中
PUT estest
{
"settings": {
"number_of_replicas": 1,
"number_of_shards": 1
}
, "mappings": { #mapping内不寫表名
"properties": {
"id":{
"type": "long"
},
"name":{
"type": "text",
"analyzer": "standard"
}
}
}
}
POST estest/_doc/2 #沒有表名用_doc
{
"id":2,
"name":"張1"
}
GET estest/_search
DELETE estest
HEAD estest
GET estest/_search #查詢上嵌套與6有很大差別
{
"query": {
"range": {
"id": {
"gte": 1,
"lte": 2
}
}
}
}
以XXX開頭的
GET es/es/_search
{
"query": {
"prefix": {
"字段": {
"value": "漓江"
}
}
},
"_source": "字段"
}