ElasticSearch:html
ES是一个基于Lucene实现的开源、分布式、Restful的全文本搜索引擎;此外,它仍是一个分布式实时文档存储,其中每一个文档的每一个field均是被索引的数据,且可被搜索;也是一个带实时分析功能的分布式搜索引擎,可以扩展至数以百计的节点实时处理PB级的数据。java
Lucene介绍:http://www.cnblogs.com/python-gm/p/8400001.html
基本组件:node
ES的集群组件:python
ES Cluster工做过程:json
官方站点:https://www.elastic.co/
ElasticSearch依赖于JDK环境:能够安装配置 Oracle JDK 或 OpenJDK ubuntu
ES的默认端口:缓存
Restful API:bash
Cluster APIs:app
curl -XGET 'http://172.16.100.67:9200/_cluster/health?pretty'
curl -XGET 'http://172.16.100.67:9200/_cluster/state/<metrics>?pretty'
curl -XGET 'http://172.16.100.67:9200/_cluster/stats'
curl -XGET 'http://172.16.100.67:9200/_nodes/stats'
Plugins:负载均衡
/usr/share/elasticsearch/bin/plugin -h 参数: -l -i, --install -r, --remove
http://HOST:9200/_plugin/plugin_name
部署elasticsearch集群:
主机:Ubuntu16.04 elasticsearch版本:6.4.2
sudo apt-get install default-jre # 查看java版本 java -version openjdk version "1.8.0_181" OpenJDK Runtime Environment (build 1.8.0_181-8u181-b13-0ubuntu0.16.04.1-b13) OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)
# 首先须要添加 Apt-key: wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add - # 而后添加 Elasticsearch 的 Repository 定义: echo "deb https://artifacts.elastic.co/packages/6.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-6.x.list # 安装 Elasticsearch: sudo apt-get update sudo apt-get install elasticsearch # 全部主机完成 Elasticsearch 的安装
cluster.name: evescn ##集群名称,全部主机配置必须相同 node.name: node-1 ##节点名称,不一样主机此处名字不能相同 path.data: /var/lib/elasticsearch path.logs: /var/log/elasticsearch network.host: 0.0.0.0 http.port: 9200 discovery.zen.ping.unicast.hosts: ["192.168.20.102", "192.168.20.103", "192.168.0.104"] ## 集群地址设置,配置以后集群的主机之间能够自动发现
# curl -XGET 'http://localhost:9200/_cluster/state?pretty' | less { "cluster_name" : "evescn", "compressed_size_in_bytes" : 10114, "cluster_uuid" : "lmV_adT3TTGt3kz5_IWeSA", "version" : 14, "state_uuid" : "c1pcj3nAS5-DyWoCcWXy5w", "master_node" : "LdRkSjSMSLKH7PtOwDHQIw", "blocks" : { }, "nodes" : { "LdRkSjSMSLKH7PtOwDHQIw" : { "name" : "node-2", "ephemeral_id" : "nf0KaKZlQduuz3rWVzxhew", "transport_address" : "192.168.20.103:9300", "attributes" : { "ml.machine_memory" : "1021595648", "ml.max_open_jobs" : "20", "xpack.installed" : "true", "ml.enabled" : "true" } }, "LR0zlOMbRF-kxX33VQLtCw" : { "name" : "node-3", "ephemeral_id" : "gX8srlZWR9GdU2W-B63KIw", "transport_address" : "192.168.20.104:9300", "attributes" : { "ml.machine_memory" : "1021595648", "ml.max_open_jobs" : "20", "xpack.installed" : "true", "ml.enabled" : "true" } ...... }
## 6.X新版本规则,须要指定 -H "Content-Type: application/json" # A主机插入数据 # curl -H "Content-Type: application/json" -XPUT 'localhost:9200/students/class1/2?pretty' -d ' > { > "first_name": "gm", > "last_name": "evescn", > "gender": "Man", > "age": 23, > "courses": "ELK" > }' # B主机查看 # curl -XGET 'localhost:9200/students/class1/2?pretty' { "_index" : "students", "_type" : "class1", "_id" : "2", "_version" : 1, "found" : true, "_source" : { "first_name" : "gm", "last_name" : "evescn", "gender" : "Man", "age" : 23, "courses" : "ELK" }
CRUD操做相关的API:
curl -XPUT 'localhost:9200/students/class1/2?pretty' -d ' > { > "first_name": "Rong", > "last_name": "Huang", > "gender": "Female", > "age": 23, > "courses": "Luoying Shenjian" > }' { "_index" : "students", "_type" : "class1", "_id" : "2", "_version" : 1, "created" : true }
~]# curl -XGET 'localhost:9200/students/class1/2?pretty' { "_index" : "students", "_type" : "class1", "_id" : "2", "_version" : 1, "found" : true, "_source": { "first_name": "Rong", "last_name": "Huang", "gender": "Female", "age": 23, "courses": "Luoying Shenjian" }
~]# curl -XPOST 'localhost:9200/students/class1/2/_update?pretty' -d ' { "doc": { "age": 22 } }' { "_index" : "students", "_type" : "class1", "_id" : "2", "_version" : 2 }
DETELE ~]# curl -XDELETE 'localhost:9200/students/class1/2'
~]# curl -XDELETE 'localhost:9200/students' ~]# curl -XGET 'localhost:9200/_cat/indices?v'
查询数据:
一、经过Restful request API查询; ~]# curl -XGET 'localhost:9200/students/_search?pretty' 二、经过发送REST request body进行; ~]# curl -XGET 'localhost:9200/students/_search?pretty' -d ' > { > "query": { "match_all": {} } > }'
/_search:全部索引; /INDEX_NAME/_search:单索引; /INDEX1,INDEX2/_search:多索引; /s*,t*/_search:正则匹配搜索 /students/class1/_search:单类型搜索 /students/class1,class2/_search:多类型搜索
Mapping和Analysis:
GET /_search?q='Xianglong' :全部域中出现此字符串的值 GET /_search?q='Xianglong%20Shiba%20Zhang' GET /_search?q=courses:'Xianglong%20Shiba%20Zhang' :在courses域中查询此字符串 GET /_search?q=courses:'Xianglong' 前两个:表示在_all域搜索; 后两个:在指定的域上搜索;
~]# curl 'localhost:9200/students/_mapping/class1?pretty'
types:exact full-text 精确值:指未经加工的原始值;在搜索时进行精确匹配; full-text:用于引用文本中数据;判断文档在多大程序上匹配查询请求;即评估文档与用户请求查询的相关度;
Standard analyzer: Simple analyzer Whitespace analyzer Language analyzer
request body: 分红两类: query dsl:执行full-text查询时,基于相关度来评判其匹配结果;查询执行过程复杂,且不会被缓存; filter dsl:执行exact查询时,基于其结果为“yes”或“no”进行评判;速度快,且结果缓存;
查询语句的结构: { QUERY_NAME: { AGGUMENT: VALUE, ARGUMENT: VALUE,... } } { QUERY_NAME: { FIELD_NAME: { ARGUMENT: VALUE,... } } }
term filter:精确匹配包含指定term的文档;
{ "term": {"name": "Guo"} } curl -XGET 'localhost:9200/students/_search?pretty' -d { "query": { "term": { "name": "Guo" } } }
terms filter:用于多值精确匹配; { "terms": { "name": ["Guo", "Rong"] }} range filters:用于在指定的范围内查找数值或时间。 { "range": "age": { "gte": 15, "lte": 25 } } gt, lt, gte, lte
exists and missing filters:存在或不存在
{ "exists": { "age": 25 } }
boolean filter: 基于boolean的逻辑来合并多个filter子句; must:其内部全部的子句条件必须同时匹配,即and; must: { "term": { "age": 25 } "term": { "gender": "Female" } }
must_not:其全部子句必须不匹配,即not must_not: { "term": { "age": 25 } }
should:至少有一个子句匹配,即or should: { "term": { "age": 25 } "term": { "gender": "Female" } }
match_all Query: 用于匹配全部文档,没有指定任何query,默认即为match_all query. { "match_all": {} }
match Query: 在几乎任何域上执行full-text或exact-value查询; 若是执行full-text查询:首先对查询时的语句作分析; { "match": {"students": "Guo" }} 若是执行exact-value查询:搜索精确值;此时,建议使用过滤,而非查询; { "match": {"name": "Guo"} }
multi_match Query: 用于在多个域上执行相同的查询; { "multi_match": "query": full-text search "field": {'field1', 'field2'} } { "multi_match": "query": { "students": "Guo" } "field": { "name", "description" } }
bool query: 基于boolean逻辑合并多个查询语句;与bool filter不一样的是,查询子句不是返回"yes"或"no",而是其计算出的匹配度分值。所以,boolean Query会为各子句合并其score; must: must_not: should:
合并filter和query: { "filterd": { query: { "match": {"gender": "Female"} } filter: { "term": {"age": 25}} } }
GET /INDEX/_validate/query?pretty { ... } GET /INDEX/_validate/query?explain&pretty { ... }