安裝好ElasticSearch之後,head插件之後,啓動服務。訪問http://localhost:9100/,可以看見如下效果:
說明:上圖中,部署了一個master,2個slave。創建了people和book兩個索引。其中people1個分區,2個副本。book索引3個分區,2個副本。people創建了5個文檔,book創建了8個文檔。
1、創建索引(非結構化),可以在界面中創建索引
2、創建索引結構
說明:創建索引結構也可以在其他的請求工具比如postman中操作
3、添加索引(用postman)
{
"settings": {
"number_of_shards": "3",
"number_of_replicas": "1"
},
"mappings": {
"properties": {
"word_count": {
"type": "integer"
},
"author": {
"type": "text"
},
"title": {
"type": "keyword"
},
"publish_date": {
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis",
"type": "date"
}
}
}
}
4、添加文檔,不指定id就是ES生成id
http://localhost:9200/book/_doc
{
"author":"張三丰",
"title":"java從入門到精通",
"word_count":1000,
"publish_date":"1999-09-01",
"id":"1"
}
5、修改文檔
http://localhost:9200/book/_doc/1/_update
{
"doc":{
"name":"小跳同志"
}
}
6、條件查詢
http://localhost:9200/book/_search
{
"query":{
"match":{
"title":"ElasticSearch"
}
} ,
"sort":{
"publish_date":{"order":"asc"}
}
}
7、複雜查詢
{
"query":{
"bool":{
"must":[
{
"match":{
"author":"李四"
}
},
{
"match":{
"title":"ElasticSearch"
}
}
],
"filter":{
"term":{
"word_count":1000
}
}
}
}
}
8、聚合查詢
http://localhost:9200/people/_search
{
"aggs":{
"grades_word_count":{
"stats":{
"field":"word_count"
}
}
}
}
說明:ElasticSearch查詢語法很多,以上列出的只是一部分,更加詳細的可以參考API文檔QUERY DSL
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html
最後,附上Springboot與ElasticSearch整合練習的demo代碼鏈接: