安裝遇到的問題
java本地環境和es環境沖突
https://www.cnblogs.com/q1359720840/p/14077049.html
,看要使用jdk11,本機安裝了jdk8,修改默認jdk配置吧,好家伙es自帶jdk15,就用它了,
配置SE_JAVA_HOME(類似java_home),
接下來返回到elasticsearch,在bin目錄下找到elasticsearch-env文件,在39-40行處。將JAVA_HOME改成ES_JAVA_HOME
我將javahome都改成ES_JAVA_HOME起來了
if "%ES_JAVA_HOME%" == "" (
set JAVA="%ES_HOME%\jdk\bin\java.exe"
set ES_JAVA_HOME="%ES_HOME%\jdk"
set JAVA_TYPE=bundled jdk
) else (
set JAVA="%ES_JAVA_HOME%\bin\java.exe"
set JAVA_TYPE=ES_JAVA_HOME
)
另一種方式:直接使用本地java環境的11
在bin目錄下找到elasticsearch-env文件,set JAVA_HOME=jdk11的路徑,即可
es基本操作
es所支持的風格
get:獲取
post:修改
put:添加
delete:刪除
創建索引
在postman向es發送put請求 http:://127.0.0.1:9200/shopping
當你索引創建成功后-再想通過put創建相同的索引會報錯,put冪等
{
"error": {
"root_cause": [
{
"type": "resource_already_exists_exception",
"reason": "index [shoppingg/G5fil2wXS6On0_rJa1fYtw] already exists",
"index_uuid": "G5fil2wXS6On0_rJa1fYtw",
"index": "shoppingg"
}
],
"type": "resource_already_exists_exception",
"reason": "index [shoppingg/G5fil2wXS6On0_rJa1fYtw] already exists",
"index_uuid": "G5fil2wXS6On0_rJa1fYtw",
"index": "shoppingg"
},
"status": 400
}
那如果使用post呢,post不是冪等的,不允許這樣操作
{
"error": "Incorrect HTTP method for uri [/shoppingg] and method [POST], allowed: [HEAD, DELETE, GET, PUT]",
"status": 405
}
獲取索引
get請求:http:://127.0.0.1:9200/shopping
{
"shoppingg": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1618786330597",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "G5fil2wXS6On0_rJa1fYtw",
"version": {
"created": "7080099"
},
"provided_name": "shoppingg"
}
}
}
}
刪除索引
delete : http:://127.0.0.1:9200/shopping
獲取全部索引
get請求:http:127.0.0.1:9200/_cat/indinces?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open shoppingg G5fil2wXS6On0_rJa1fYtw 1 1 0 0 208b 208b
添加文檔數據
post請求: http:127.0.0.1:9200/shopping/_doc
數據為body中,請求方式為json:
{
"title":"小米手機","category":"小米","images":"http://www.gulixueyuan.com/xm.jpg","price":3900.00
}
添加成功后會返回數據集:
{
"_index": "shopping",
"_type": "_doc",
"_id": "YKtY53gBLVBklMqda4mp",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
post請求的id新增數據是隨機生成的,多次相同的請求會生效,因為id不一樣
我們一般都是通過id查詢數據的,那么如何使用自己的id呢?
新增文檔數據-自定義id
post請求: http:127.0.0.1:9200/shopping/_doc/1001
如果id存在,那么直接將數據全部update替換
數據為body中,請求方式為json:
{
"title":"小米手機","category":"小米","images":"http://www.gulixueyuan.com/xm.jpg","price":3900.00
}
對於post請求/_doc就是新增 / _update就是修改
修改數據
post : localhost:9200/shopping/_update/1002
BODY:
{
"doc": {
"title": "小米手機12",
"category": "小米12",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3900.00
}
}{
"_index": "shopping",
"_type": "_doc",
"_id": "1002",
"_version": 4,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 10,
"_primary_term": 1
}
查詢數據
get 請求:http:127.0.0.1:9200/shopping/_doc/1001
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_version": 1,
"_seq_no": 3,
"_primary_term": 1,
"found": true,
"_source": {
"title": "小米手機",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3900.00
}
}
修改文檔
put :http:127.0.0.1:9200/shopping/_doc/1003
{
"title":"小米手機222"
}
添加文檔數據-id不能重復
put :http:127.0.0.1:9200/shopping/_create/1003
id重復會出現異常
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[1003]: version conflict, document already exists (current version [1])",
"index_uuid": "oQ0XOQPvSQmtXijIHbb-4w",
"shard": "0",
"index": "shopping"
}
],
"type": "version_conflict_engine_exception",
"reason": "[1003]: version conflict, document already exists (current version [1])",
"index_uuid": "oQ0XOQPvSQmtXijIHbb-4w",
"shard": "0",
"index": "shopping"
},
"status": 409
}
如果不傳id會報錯
{
"error": "Incorrect HTTP method for uri [/shopping/_create] and method [PUT], allowed: [POST]",
"status": 405
}
查詢某個索引下的所有數據
get: localhost:9200/shopping/_search
{
"took": 33,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 6,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "YKtY53gBLVBklMqda4mp",
"_score": 1.0,
"_source": {
"title": "小米手機",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3900.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_score": 1.0,
"_source": {
"title": "小米手機",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3900.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "Yatg53gBLVBklMqdd4ku",
"_score": 1.0,
"_source": {
"title": "小米手機",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3900.00
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1002",
"_score": 1.0,
"_source": {
"title": "小米手機"
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1003",
"_score": 1.0,
"_source": {
"title": "小米手機"
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "serach",
"_score": 1.0,
"_source": {
"title": "小米手機2"
}
}
]
}
}
刪除文檔
復雜查詢-條件查詢-會中文亂碼
get :localhost:9200/shopping/_search?q=title:小米
這種搜索直接在參數拼接,容易出現中文亂碼
復雜查詢-條件查詢
get :localhost:9200/_search
{"query":{
"match":{"title":"小米"}
}}
復雜查詢-查詢全部
復雜查詢-條件查詢+分頁
get: localhost:9200/_search
{"query":{
"match_all":{}
},
"from":0,
"size":2
}
復雜查詢-條件查詢+分頁 +顯示某列內容
get : localhost:9200/_search
備注:分頁然后只查詢title
請求的 body:
{"query":{
"match_all":{}
},
"from":0,
"size":2,
"_source":["title"]
}
復雜查詢-條件查詢+對內容結果排序
get : localhost:9200/_search
備注:分頁然后只查詢title
請求的 body:
{"query":{
"match_all":{}
},
"sort":{
"price":{
"order":"asc"
}
}
}
多條件查詢-查數據同時存在
localhost:9200/_search
query:代表查詢
多個條件: bool
多個條件同時成立,必須的意思,里面是數組: must
或者的意思 should:
匹配的條件: match
get : localhost:9200/_search
body:
{"query":{
"bool":{
"must":[
{
"match":{
"category":"小米"
}}
,
{
"match":{
"price":"3900"
}
}
]
}
}
}
多條件查詢-查數據或者存在
get : localhost:9200/_search
{"query":{
"bool":{
"should":[
{"match":{
"title":"小米"
}},{"match":{
"title":"華為"
}}
]
}
}}
多條件查詢-查數據范圍查找
get : localhost:9200/_search
{"query":{
"bool":{
"filter":{
"range":{"price":{
"gt":4000
}}
}
}
}}
需要注意,即使查詢一個詞也會查詢出相關的數據,倒排索引的關系,全文檢索
完全匹配操作
localhost:9200/_search
如果match_phrase 這個查詢是完全匹配,如果match ,是倒排索引匹配,match_phrase 輸入的不全可以匹配到帶有相關名字的,但是不進行拆分,而match 是進行拆分匹配的。
{"query":{
"match_phrase":{
"title":"小米"
}
}
}
高亮顯示
localhost:9200/_search
{"query":{
"match":{
"title":"小華"
}
}
,"highlight":{
"fields":{
"title":{}
}
}
}
聚合查詢 aggs 分組terms
{
"aggs":{
"price_group":{//名稱隨意起
"terms":{
"field":"price"//分組字段
}
}
}
}
結果:是帶着原始數據的,如果不要原始數據,可以增加 size:0
{
"aggs":{
"price_group":{
"terms":{
"field":"price"
}
}
},"size":0
}
求平均值 avg
{"aggs":{
"price_avg":{
"avg":{
"field":"price"
}
}
},"size":0
}
查看es映射關系
1.創建索引
put http://localhost:9200/user
2.創建結構信息
put http://127.0.0.1:9200/user/_mapping
👇
TYPE:text是可以分詞,keyword是完整匹配, INDEX:true,可以被索引,false不能被索引
{"properties":{
"name":{
"type":"text",
"index":true
}
,
"sex":{
"type":"keyword",
"index":true
}
,"tel":{
"type":"keyword",
"index":false
}
}}
get http://127.0.0.1:9200/user/_mapping
看看上面傳的結果
添加數據:
post localhost:9200/user/_doc
{
"sex":"男的","name":"小米"
,"tel":1111 }
查詢數據:
get http://127.0.0.1:9200/user/_search
有結果
{"query":{
"match":{
"name":"小"
}
}}
沒結果
{"query":{
"match":{
"sex":"男"
}
}}有結果
{"query":{
"match":{
"sex":"男的"
}
}}
報錯
{"query":{
"match":{
"tel":1111
}
}}