安装好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代码链接: