安装
单实例安装
mkdir imooccd imoocbrew install wgetwget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.0.0-darwin-x86_64.tar.gzlstar -vxf elasticsearch-7.0.0-darwin-x86_64.tar.gzcd elasticsearch-7.0.0java -versionsh ./bin/elasticsearchlocalhost:9200
cd elasticsearch-7.0.0lsvim config/elasticsearch.ymlhttp.cors.enabled: truehttp.cors.allow-origin: "*"./bin/elasticsearch -d
插件安装
github -> mobz/elasticsearch-head
cd imoocwget https://github.com/mobz/elasticsearch-head/archive/master.ziplsunzip master.ziplscd elasticsearch-head-masterlsnode -vnpm installnpm run startlocalhost:9100
分布式安装
cd elasticsearch-7.0.0vim config/elasticsearch.ymlcluster.name: bingnode.name: masternode.master: truenetwork.host: 127.0.0.1ps -ef | grep `pwd`kill -9 5531./bin/elasticsearch -dclear
cd imoocmkdir es_slavecp elasticsearch-7.0.0-darwin-x86_64.tar.gz es_slavecd es_slavelstar -vxf elasticsearch-7.0.0-darwin-x86_64.tar.gzcp -r elasticsearch-7.0.0 es_slave1cp -r elasticsearch-7.0.0 es_slave2cd es_slave1vim config/elasticsearch.ymlcluster.name: bingnode.name: slave1network.host: 127.0.0.1http.port: 10200discovery.zen.ping.unicast.hosts: ["127.0.0.1"]./bin/elasticsearch -dcd es_slave2vim config/elasticsearch.ymlcluster.name: bingnode.name: slave2network.host: 127.0.0.1http.port: 11200discovery.zen.ping.unicast.hosts: ["127.0.0.1"]./bin/elasticsearch -d
基础概念
集群/节点/索引(数据库)/类型(表)/文档(记录)/分片/备份
基本用法
索引创建
api格式:http://<ip>:<port>/<索引>/<类型>/<文档id> 非格式化索引: -> 索引 -> 新建索引 格式化索引:postman->put: -> body -> raw
{ "settings": { "number_of_shards": 3, "number_of_replicas": 1 }, "mappings": { "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" } } }}
插入
post: put:
{ "name": "walili", "country": "China", "age": 23, "date": "1988-08-08" }
修改
put:
删除
- 删除文档 delete:
- 删除索引 delete:
查询
- 简单查询 get:
- 条件查询 get:
{ "query": { "match_all": {} }}
{ "query": { "match_all": {} }, "from": 0, "size": 1}
{ "query": { "match": { "name": "walili" } }}
{ "query": { "match": { "country": "China" } }, "sort": [ { "date": {"order": "desc"} } ]}
- 聚合查询
{ "aggs": { "group_by_age": { "terms": { "field": "age" } }, "group_by_date": { "terms": { "field": "date" } } }}
{ "aggs": { "grades_age": { "stats": { "field": "age" } } }}
{ "aggs": { "grades_age": { "min": { "field": "age" } } }}
高级查询
query
get:
- 全文本查询:准对文本型数据
- 字段级别查询:针对结构化数据
模糊查询
{ "query": { "match": { "name": "wali大师" } }}
短语查询
{ "query": { "match_phrase": { "name": "wali大师" } }}
多个字段模糊查询
{ "query": { "multi_match": { "query": "wali", "fields": ["name", "country"] } }}
语法查询
{ "query": { "query_string": { "query": "lili OR 大师", "fields": ["name", "country"] } }}
字段查询
{ "query": { "range": { "age": { "gte": "30", "lte": "100" } } }}
filter
{ "query": { "bool": { "filter": { "term": { "age": "77" } } } }}
复合查询
get:
{ "query": { "match": { "name": "wali" } }}
固定分数查询
{ "query": { "constant_score": { "filter": { "match": { "age": "77" } }, "boost": 2 } }}
布尔查询
{ "query": { "bool": { "should": [ { "match": { "name": "wali" } }, { "match": { "country": "大师" } } ] } }}
{ "query": { "bool": { "must": [ { "match": { "name": "wali" } }, { "match": { "country": "大师" } } ] } }}
{ "query": { "bool": { "must_not": [ { "term": { "name": "wali" } } ] } }}
SB 集成 ES
SB 集成 ES
... 1.8 5.5.2 org.elasticsearch.client transport org.apache.logging.log4j log4j-core 2.7
package com.qianbill.admin_api;import org.elasticsearch.client.transport.TransportClient;import org.elasticsearch.common.settings.Settings;import org.elasticsearch.common.transport.InetSocketTransportAddress;import org.elasticsearch.transport.client.PreBuiltTransportClient;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import java.net.InetAddress;import java.net.UnknownHostException;/** * @Auther: beanho * @Date: 2019/4/17 15:51 * @Description: */@Configurationpublic class ElasticSearch { @Bean public TransportClient client() throws UnknownHostException { InetSocketTransportAddress node = new InetSocketTransportAddress( InetAddress.getByName("localhost"), 9300 ); Settings settings = Settings.builder() .put("cluster.name", "bing") .build(); TransportClient client = new PreBuiltTransportClient(settings); client.addTransportAddress(node); return client; }}