一、什麼是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查詢等更高級的功能以及實踐中的應用,提高數據存儲和檢索的效率和準確性。
原創文章,作者:XJODG,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/331546.html
微信掃一掃
支付寶掃一掃