一、什么是Elasticsearch
Elasticsearch是一个开源搜索引擎,可以处理各种文档类型的分布式数据,以实现数据的快速检索和分析。Elasticsearch的主要功能包括全文检索、结构化查询、分析和可视化。它使用json格式进行数据存储和检索,因此易于使用和扩展。
二、基本使用
1、创建索引
PUT /my_first_index { "settings": { "number_of_shards": 3, "number_of_replicas": 1 }, "mappings": { "properties": { "title": { "type": "text" }, "content": { "type": "text" }, "date": { "type": "date" } } } }
2、添加文档
PUT /my_first_index/_doc/1 { "title": "Elasticsearch Tutorial", "content": "This is Elasticsearch Tutorial", "date": "2021-01-01" }
3、搜索
GET /my_first_index/_search?q=Elasticsearch
三、查询DSL
1、match查询
GET /articles/_search { "query": { "match": { "content": "Elasticsearch Tutorial" } } }
2、match_phrase查询
GET /articles/_search { "query": { "match_phrase": { "content": "Elasticsearch Tutorial" } } }
3、bool查询
GET /articles/_search { "query": { "bool": { "must": [ { "match": { "title": "Search" }}, { "match": { "content": "Elasticsearch" }} ], "filter": [ { "term": { "status": true }} ], "must_not": [ { "match": { "content": "Elasticsearch Introduction" }} ] } } }
四、聚合
1、terms聚合
GET /articles/_search { "aggs": { "tags": { "terms": { "field": "tags" } } } }
2、range聚合
GET /articles/_search { "aggs": { "price_range": { "range": { "field": "price", "ranges": [ { "to": 10 }, { "from": 10, "to": 20 }, { "from": 20 } ] } } } }
五、特殊功能
1、分页
GET /articles/_search { "from": 0, "size": 10, "query" : { "match_all" : {} } }
2、scroll查询
POST /articles/_search?scroll=1m { "query": { "match_all": {} } }以上是Elasticsearch中文文档的简要介绍和使用,您可以进一步深入学习Elasticsearch的DSL、聚合、分页、scroll查询等更高级的功能以及实践中的应用,提高数据存储和检索的效率和准确性。