分享 ElasticSearch 基础,请指教。
如你对技术也感兴趣,欢迎交流。
如有对阁下帮助,请点赞收藏分享
Elasticsearch: The Official Distributed Search & Analytics Engine | ElasticElasticsearch is the leading distributed, RESTful, free and open search and analytics engine designed for speed, horizontal scalability, reliability, and easy management. Get started for free.https://www.elastic.co/elasticsearch/
Elasticsearch是基于Apache Lucene的搜索服务器它的最新版本是8.10.2。
Elasticsearch是一个实时的分布式开放源代码全文本搜索和分析引擎。可从RESTful Web服务界面访问它,并使用无模式的JSON(JavaScript对象表示法)文档存储数据。它基于Java编程语言构建,因此Elasticsearch可以在不同平台上运行。它使用户能够以很高的速度浏览大量的数据。
对比关系型数据库,创建索引就等同于创建数据库
PUT /shopping
HTTP 的 PUT 请求
GET /shopping
HTTP 的 GET 请求
GET /_cat/indices?v
HTTP 的 GET 请求
删除
DELETE /shopping
HTTP 的 DELETE 请求
POST /shopping/_doc
{
"title":"华为meta60pro",
"category":"手机",
"images":"https://www.gulixueyuan.com/xm.jpg",
"price":"6499.00"
}
POST /shopping/_doc/1000
{
"title":"华为meta60pro max",
"category":"手机",
"images":"https://www.gulixueyuan.com/xm.jpg",
"price":"7499.00"
}
PUT /shopping/_doc/1002
{
"title":"华为meta60pro max尊享",
"category":"手机",
"images":"https://www.gulixueyuan.com/xm.jpg",
"price":"8499.00"
}
GET /shopping/_search
PUT /shopping/_doc/1002
{
"title":"华为meta60pro max尊享",
"category":"手机",
"images":"https://www.gulixueyuan.com/xm.jpg",
"price":"12999.00"
}
修改
修改后
POST /shopping/_update/1002
{
"doc":{
"title":"华为meta70pro max尊享"
}
}
DELETE /shopping/_doc/1000
GET /shopping/_search?q=category:手机
GET /shopping/_search
{
"query":{
"match": {
"category": "手机"
}
}
}
GET /shopping/_search
{
"query":{
"match_all": {}
}
}
GET /shopping/_search
{
"query":{
"match_all": {}
},
"from": 1,
"size": 2
}
GET /shopping/_search
{
"query":{
"match_all": {}
},
"from": 1,
"size": 2,
"_source": ["price","title"]
}
GET /shopping/_search
{
"query":{
"match_all": {}
},
"from": 1,
"size": 2,
"_source": ["title"],
"sort": [
{
"title": {
"order": "desc"
}
}
]
}
# 设置属性类型
PUT shopping/_mapping
{
"properties": {
"price": {
"type": "text"
},
"title": {
"fielddata": true,
"type": "text"
}
}
}
查询结果
## SQL: category="手机" AND price="6499.00"
GET /shopping/_search
{
"query":{
"bool": {
"must": [
{
"match": {
"category": "手机"
}
},
{
"match": {
"price": "6499.00"
}
}
]
}
}
}
## SQL: category="手机" OR price="6499.00"
GET /shopping/_search
{
"query":{
"bool": {
"should": [
{
"match": {
"category": "手机"
}
},
{
"match": {
"price": "6499.00"
}
}
]
}
}
}
## SQL: (category="手机" OR price=6499.00) and (price between 6500 and 10000)
GET /shopping/_search
{
"query":{
"bool": {
"should": [
{
"match": {
"category": "手机"
}
},
{
"match": {
"price": 6499.00
}
}
],
"filter": {
"range": {
"price": {
"gt": 6500,
"lt": 10000
}
}
}
}
}
}
# SQL: (category="手机" OR price=6499.00) and (price between 6500 and 10000) limit 2 order by price desc
GET /shopping/_search
{
"query":{
"bool": {
"should": [
{
"match": {
"category": "手机"
}
},
{
"match": {
"price": 6499.00
}
}
],
"filter": {
"range": {
"price": {
"gt": 6500,
"lt": 10000
}
}
}
}
},
"from": 0,
"size": 2,
"sort": [
{
"price": {
"order": "desc"
}
}
]
}
GET /shopping/_search
{
"query":{
"match_phrase": {
"category": "手机"
}
},
"highlight": {
"fields": {
"category": {}
}
}
}
GET /shopping/_search
{
"aggs": {
"cacl_avg": {
"terms": {
"field":"price"
}
}
},
"size": 0
}
GET /shopping/_search
{
"aggs": {
"cacl_avg": {
"avg": {
"field":"price"
}
}
},
"size": 0
}
PUT /user/_mapping
{
"properties": {
"user":{
"type": "text",
"index": true
},
"sex":{
"type": "keyword",
"index": false
},
"tel":{
"type": "keyword",
"index": true
}
}
}
type=text ,index=true 时,可分词可查询;
type=keyword ,index=true 时,不可分词可查询;
index=false 时,不可查询