主节点配置:
#集群名称
cluster.name: chen
#节点名称
node.name: master
#是否为主节点
node.master: true
#节点ip
network.host: 127.0.0.1
从节点配置:
#集群名称
cluster.name: chen
#节点名称
node.name: slave1
#从节点ip
network.host: 127.0.0.1
#从节点端口
http.port: 8200
#主节点ip
discovery.zen.ping.unicast.hosts: ["127.0.0.1"]
1.创建结构化索引(put):
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"man": {
"properties": {
"name": {
"type": "text"
},
"country": {
"type": "keyword"
},
"age": {
"type": "integer"
},
"date": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}
}
}
2.插入数据(put 127.0.0.1:9200/people/man/1 自定id为1)(post 27.0.0.1:9200/people/man 自动生成id)
{
"name": "chenyaowu",
"country": "China",
"age": 20,
"date": "2017-7-7"
}
3.修改数据(post 127.0.0.1:9200/people/man/1/_update )
根据id修改:
{
"doc": {
"age": 21
}
}
根据脚本修改:
{
"script": {
"lang": "painless",
"inline": "ctx._source.age = params.age",
"params": {
"age": 100
}
}
}
4.删除数据(delete 127.0.0.1:9200/people/man/1)
5.删除索引(delete 127.0.0.1:9200/people)
6查询
①根据id查询(GET 127.0.0.1:9200/book/novel/11)
②条件查询(POST 127.0.0.1:9200/book/_search)
---------------------------查询所有-------------------------------------
{
"query": {
"match_all": {}
}
}
---------------------------条件查询-------------------------------------
{
"query": {
"match": {
"title": "ElasticSearch"
}
},
"sort": [
{
"publish_date": {
"order": "desc"
}
}
],
"from": 0,
"size": 3
}
--------------------模糊查询(1,匹配“ElasticSearch”和“入门”)----------------------
{
"query": {
"match": {
"title": "ElasticSearch入门"
}
}
}
--------------------模糊查询(2,其余匹配)-----------------------------------------
{
"query": {
"match_phrase": {
"title": "ElasticSearch入门"
}
}
}
--------------------模糊查询(3,多个字段)-----------------------------------------
{
"query": {
"multi_match": {
"query": "ElasticSearch入门",
"fields": [
"author",
"title"
]
}
}
}
--------------------模糊查询(4,语法查询)-----------------------------------------
{
"query": {
"query_string": {
"query": "ElasticSearch AND 大法"
}
}
}
--------------------模糊查询(5,对字段语法查询)-----------------------------------------
{
"query": {
"query_string": {
"query": "ElasticSearch OR 瓦力",
"fields": [
"title",
"author"
]
}
}
}
--------------------模糊查询(6,字段查询)-----------------------------------------
{
"query": {
"term": {
"author": "瓦力"
}
}
}
--------------------模糊查询(7,范围查询)-----------------------------------------
<!-- gte >= , lte <= ,gt > ,lt < -->
{
"query": {
"range": {
"word_count": {
"gte": 1000,
"lte": 2000
}
}
}
}
-----------------------------聚合查询-----------------------------------------------
-----------------------------单个聚合-----------------------------------------
{
"aggs": {
"group_by_word_count": {
"terms": {
"field": "word_count"
}
}
}
}
-----------------------------多个聚合1-----------------------------------------
{
"aggs": {
"group_by_word_count": {
"terms": {
"field": "word_count"
}
},
"group_by_publish_date": {
"terms": {
"field": "publish_date"
}
}
}
}
------------------------------统计-----------------------------------------
{
"aggs": {
"grades_word_count": {
"stats": {
"field": "word_count"
}
}
}
}
------------------------------filter-----------------------------------------
{
"query": {
"bool": {
"filter": {
"term": {
"word_count": 1000
}
}
}
}
}
#集群名称
cluster.name: chen
#节点名称
node.name: master
#是否为主节点
node.master: true
#节点ip
network.host: 127.0.0.1
从节点配置:
#集群名称
cluster.name: chen
#节点名称
node.name: slave1
#从节点ip
network.host: 127.0.0.1
#从节点端口
http.port: 8200
#主节点ip
discovery.zen.ping.unicast.hosts: ["127.0.0.1"]
1.创建结构化索引(put):
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"man": {
"properties": {
"name": {
"type": "text"
},
"country": {
"type": "keyword"
},
"age": {
"type": "integer"
},
"date": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}
}
}
2.插入数据(put 127.0.0.1:9200/people/man/1 自定id为1)(post 27.0.0.1:9200/people/man 自动生成id)
{
"name": "chenyaowu",
"country": "China",
"age": 20,
"date": "2017-7-7"
}
3.修改数据(post 127.0.0.1:9200/people/man/1/_update )
根据id修改:
{
"doc": {
"age": 21
}
}
根据脚本修改:
{
"script": {
"lang": "painless",
"inline": "ctx._source.age = params.age",
"params": {
"age": 100
}
}
}
4.删除数据(delete 127.0.0.1:9200/people/man/1)
5.删除索引(delete 127.0.0.1:9200/people)
6查询
①根据id查询(GET 127.0.0.1:9200/book/novel/11)
②条件查询(POST 127.0.0.1:9200/book/_search)
---------------------------查询所有-------------------------------------
{
"query": {
"match_all": {}
}
}
---------------------------条件查询-------------------------------------
{
"query": {
"match": {
"title": "ElasticSearch"
}
},
"sort": [
{
"publish_date": {
"order": "desc"
}
}
],
"from": 0,
"size": 3
}
--------------------模糊查询(1,匹配“ElasticSearch”和“入门”)----------------------
{
"query": {
"match": {
"title": "ElasticSearch入门"
}
}
}
--------------------模糊查询(2,其余匹配)-----------------------------------------
{
"query": {
"match_phrase": {
"title": "ElasticSearch入门"
}
}
}
--------------------模糊查询(3,多个字段)-----------------------------------------
{
"query": {
"multi_match": {
"query": "ElasticSearch入门",
"fields": [
"author",
"title"
]
}
}
}
--------------------模糊查询(4,语法查询)-----------------------------------------
{
"query": {
"query_string": {
"query": "ElasticSearch AND 大法"
}
}
}
--------------------模糊查询(5,对字段语法查询)-----------------------------------------
{
"query": {
"query_string": {
"query": "ElasticSearch OR 瓦力",
"fields": [
"title",
"author"
]
}
}
}
--------------------模糊查询(6,字段查询)-----------------------------------------
{
"query": {
"term": {
"author": "瓦力"
}
}
}
--------------------模糊查询(7,范围查询)-----------------------------------------
<!-- gte >= , lte <= ,gt > ,lt < -->
{
"query": {
"range": {
"word_count": {
"gte": 1000,
"lte": 2000
}
}
}
}
-----------------------------聚合查询-----------------------------------------------
-----------------------------单个聚合-----------------------------------------
{
"aggs": {
"group_by_word_count": {
"terms": {
"field": "word_count"
}
}
}
}
-----------------------------多个聚合1-----------------------------------------
{
"aggs": {
"group_by_word_count": {
"terms": {
"field": "word_count"
}
},
"group_by_publish_date": {
"terms": {
"field": "publish_date"
}
}
}
}
------------------------------统计-----------------------------------------
{
"aggs": {
"grades_word_count": {
"stats": {
"field": "word_count"
}
}
}
}
------------------------------filter-----------------------------------------
{
"query": {
"bool": {
"filter": {
"term": {
"word_count": 1000
}
}
}
}
}