elasticsearch java_java Elasticsearch详细介绍及使用实例分析
集群健康檢查
取得健康狀態
GET /_cat/health?v
返回:
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent1535248805 10:00:05 elasticsearch yellow 1 1 1 1 0 0 1 0 - 50.0%
健康狀態分類
green:索引的primary shard和replica shard都是active狀態的
yellow:索引的primary shard都是active狀態的,但是部分replica shard不是active狀態,處于不可用的狀態、
red:不是所有索引的primary shard都是active狀態的,部分索引有數據丟失了
當前處于yellow狀態是因為目前只啟動了一個es進程,只有一個節點node,這是不安全的。這個節點的primary shard 有分配,但是replica shard 沒有節點分配,因此是yellow狀態。
快速查看集群有哪些節點
GET /_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open .kibana xSbUffi_SYGYIhPtiMxH5w1 1 1 0 3.1kb 3.1kb
簡單的索引操作
創建索引:PUT /test_index?pretty
返回:
{"acknowledged": true,"shards_acknowledged": true}
刪除索引:DELETE /test_index?pretty
返回:
{"acknowledged": true}
CURD 操作Demo
(1)新增商品:新增文檔,建立索引
PUT /ecommerce/product/1{"name" : "xi jie jing","desc" : "gaoxiao qing jie","price" : 30,"producer" : "xijiejing producer","tags": [ "qingxi", "wuzhi"]
}
返回:
{"_index": "ecommerce","_type": "product","_id": "1","_version": 1,"result": "created","_shards": {"total": 2,"successful": 1,"failed": 0},"created": true}
新增
PUT /ecommerce/product/2{"name" : "niunai","desc" : "mengniu niunai","price" : 25,"producer" : "mengniuniunai producer","tags": [ "niunai"]
}
返回:
{"_index": "ecommerce","_type": "product","_id": "2","_version": 1,"result": "created","_shards": {"total": 2,"successful": 1,"failed": 0},"created": true}
(2)查詢商品:檢索文檔
GET /ecommerce/product/1
返回:
{"_index": "ecommerce","_type": "product","_id": "1","_version": 1,"found": true,"_source": {"name": "xi jie jing","desc": "gaoxiao qing jie","price": 30,"producer": "xijiejing producer","tags": ["qingxi","wuzhi"]
}
}
(3)修改商品:替換文檔
PUT /ecommerce/product/1{"name" : "jiaqiangban gaolujie yagao","desc" : "gaoxiao meibai","price" : 30,"producer" : "gaolujie producer","tags": [ "meibai", "fangzhu"]
}
返回:
{"_index": "ecommerce","_type": "product","_id": "1","_version": 2,"result": "updated","_shards": {"total": 2,"successful": 1,"failed": 0},"created": false}
(5)刪除商品:刪除文檔
DELETE /ecommerce/product/1
{"found": true,"_index": "ecommerce","_type": "product","_id": "1","_version": 3,"result": "deleted","_shards": {"total": 2,"successful": 1,"failed": 0}
}
多種搜索方式
1?query string search
GET /ecommerce/product/_search
{"took": 12,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 1,"max_score": 1,"hits": [
{"_index": "ecommerce","_type": "product","_id": "2","_score": 1,"_source": {"name": "niunai","desc": "mengniu niunai","price": 25,"producer": "mengniuniunai producer","tags": ["niunai"]
}
}
]
}
}
字段說明:
took:耗時
timed_out:是否超時
_shards:數據拆成了5個分片
hits.total:查詢結果的數量,3個document
hits.max_score:匹配分數,越相關,就越匹配,分數也高
hits.hits:包含了匹配搜索的document的詳細數據
此方法只是臨時在命令行使用,例如crul,不適用于復雜查詢。
2?query DSL
可以用json語法查詢語法
查詢所有的商品:
GET /ecommerce/product/_search
{"query": { "match_all": {} }
}
按照價格排序:
GET /ecommerce/product/_search
{"query": {"match": {"name" : "yagao"}
},"sort": [
{"price": "desc"}
]
}
總結
以上是生活随笔為你收集整理的elasticsearch java_java Elasticsearch详细介绍及使用实例分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java Ofd 转图片_OFD文件怎么
- 下一篇: java UCI,UCI数据集