ElasticSearch

2021-05-11 1211629

简介

MySql&ES 比较

安装

ES 安装

Let’s download the Elasticsearch 5.6.4 tar as follows:
curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.4.tar.gz
Then extract it as follows:
tar -xvf elasticsearch-5.6.4.tar.gz
It will then create a bunch of files and folders in your current directory. We then go into the bin directory as follows:
cd elasticsearch-5.6.4/bin
And now we are ready to start our node and single cluster:
./elasticsearch

ES API

集群健康状态:
curl -X GET "localhost:9200/_cat/health?v"
集群节点列表:
curl -X GET "localhost:9200/_cat/nodes?v"
查看所有索引:
curl -X GET "localhost:9200/_cat/indices?v"
创建一个customer索引:
curl -X PUT "localhost:9200/customer?pretty"
查看customer索引:
curl -X GET "localhost:9200/_cat/indices?v"
Put一些数据到我们的"customer"索引:
curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'{"name": "John Doe"}'
检索这个索引:
curl -X GET "localhost:9200/customer/_doc/1?pretty"
删除索引:
curl -X DELETE "localhost:9200/customer?pretty"
更新索引:
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d' { "doc": { "name": "Jane Doe", "age": 20 } } '
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d' { "script" : "ctx._source.age += 5" } '
删除文档:
curl -X DELETE "localhost:9200/customer/_doc/2?pretty"

Elastcsearch-head 安装

git clone git://github.com/mobz/elasticsearch-head.git
//elasticsearch-head是一个基于node.js的前端工程,启动elasticsearch-head的步骤:
cd /Users/xxx/software/elasticsearch-head
npm install
npm run start
访问: http://localhost:9100/

ElasticSearch-Head 使用实践

创建索引模型

PUT http://localhost:9200/test_index_01
{
"mappings": {
"properties": {
"id": {
"type": "long"
},
"name": {
"type": "keyword"
},
"description": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}

查看索引模型

GET http://localhost:9200/test_index_01

创建索引

POST http://localhost:9200/test_index_01/_doc
{
"id": 1,
"name": "Zhang San",
"description": "This is detail information"
}

查询索引

//根据ID查询
POST http://localhost:9200/test_index_01/_search
{
"query": {
"match": {
"id": 1
}
}
}

更新索引

POST http://localhost:9200/test_index_01/_doc/geziEnkBkShgFG4suzh6
{
"id": 1,
"name": "Zhang San",
"description": "This is detail information v2"
}

删除索引

DELETE http://localhost:9200/test_index_01/_doc/f-zKEnkBkShgFG4sIjgA

ES 查询表达式

Query DSL

Elasticsearch provides a full Query DSL (Domain Specific Language) based on JSON to define queries. Think of the Query DSL as an AST (Abstract Syntax Tree) of queries, consisting of two types of clauses:
Leaf query clauses
Leaf query clauses look for a particular value in a particular field, such as the matchterm or range queries. These queries can be used by themselves.
Compound query clauses
Compound query clauses wrap other leaf or compound queries and are used to combine multiple queries in a logical fashion (such as the bool or dis_max query), or to alter their behaviour (such as the constant_score query).
Allow expensive queries
Certain types of queries will generally execute slowly due to the way they are implemented, which can affect the stability of the cluster. Those queries can be categorised as follows:
Queries that need to do linear scans to identify matches:
Queries that have a high up-front cost:
fuzzy queries (except on wildcard fields)
regexp queries (except on wildcard fields)
prefix queries (except on wildcard fields or those without index_prefixes)
wildcard queries (except on wildcard fields)
range queries on text and keyword fields
Queries that may have a high per-document cost:
The execution of such queries can be prevented by setting the value of the search.allow_expensive_queries setting to false (defaults to true).

附录

GitHub

Doc

Elasticsearch 权威指南

 写留言

精选留言

由作者筛选后的优质留言将会公开显示,欢迎踊跃留言。