写点什么

ElasticSearch

用户头像
云淡风轻
关注
发布于: 2021 年 04 月 27 日

简介

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
复制代码


Access http://localhost:9200/

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-headnpm installnpm 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:

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

https://github.com/elastic/elasticsearch

Doc

https://www.elastic.co/guide/index.html

https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html

Elasticsearch 权威指南

https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html

用户头像

云淡风轻

关注

云淡风轻 2018.08.18 加入

JAVA软件工程师

评论

发布
暂无评论
ElasticSearch