ElasticSearch常见用法

ElasticSearch是由Java开发的开源搜索引擎,因为它具有实时搜索、安全可靠、安装快捷、使用方便的特点,所以在Java开发社区中得到了广泛的认可和应用。

Elasticsearch还具有全文搜索的能力,它可以在一个平台上包装布尔查询、句子查询、过滤器、排序、分页等复杂的搜索功能。

这样,Java开发者就可以轻松地满足复杂的搜索需求,提高用户体验和系统功能。

下面是ElasticSearch的基本操作样例,学习新技术,先用起来,其它的再说~

一、索引

1、创建

# 1.创建索引
- PUT /索引名 ====> PUT /products
- 注意: 
 1.ES中索引健康转态  red(索引不可用) 、yellwo(索引可用,存在风险)、green(健康)
 2.默认ES在创建索引时回为索引创建1个备份索引和一个primary索引
  
# 2.创建索引 进行索引分片配置
- PUT /products
{
  "settings": {
    "number_of_shards": 1, #指定主分片的数量
    "number_of_replicas": 0 #指定副本分片的数量
  }
}

字符串类型: keyword 关键字 关键词 、text 一段文本

数字类型:integer long

小数类型:float double

布尔类型:boolean

日期类型:date

创建索引&映射

PUT /products
{ 
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  }, 
  "mappings": {
    "properties": {
      "title":{
        "type": "keyword"
      },
      "price":{
        "type": "double"
      },
      "created_at":{
        "type": "date"
      },
      "description":{
        "type": "text"
      }
    }
  }
}
图片[1]-ElasticSearch常见用法-不念博客

说明: ES中支持字段类型非常丰富,如:text、keyword、integer、long、ip 等。更多参见https://www.elastic.co/guide/en/elasticsearch/reference/7.15/mapping-types.html

2、查询

# 查询索引
- GET /_cat/indices?v
图片[2]-ElasticSearch常见用法-不念博客
// 查看某个索引的映射
GET /索引名/_mapping =====> GET /products/_mapping

3、删除

# 3.删除索引
- DELETE /索引名 =====> DELETE /products
- DELETE /*     `*代表通配符,代表所有索引`

二、文档

1、添加文档

POST /products/_doc/1 #指定文档id 
{
  "title":"iphone13",
  "price":8999.99,
  "created_at":"2021-09-15",
  "description":"iPhone 13屏幕采用6.1英寸OLED屏幕。"
}
POST /products/_doc/ #自动生成文档id
{
  "title":"iphone14",
  "price":8999.99,
  "created_at":"2021-09-15",
  "description":"iPhone 13屏幕采用6.8英寸OLED屏幕"
}
{
  "_index" : "products",
  "_type" : "_doc",
  "_id" : "sjfYnXwBVVbJgt24PlVU",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 3,
  "_primary_term" : 1
}

2、查询文档

GET /products/_doc/1
{
  "_index" : "products",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "title" : "iphone13",
    "price" : 8999.99,
    "created_at" : "2021-09-15",
    "description" : "iPhone 13屏幕采用6.1英寸OLED屏幕"
  }
}

3、删除文档

DELETE /products/_doc/1
{
  "_index" : "products",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,
  "result" : "deleted",
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 1
}

4、更新文档

PUT /products/_doc/sjfYnXwBVVbJgt24PlVU
{
  "title":"iphon15"
}

说明: 这种更新方式是先删除原始文档,再将更新文档以新的内容插入。

POST /products/_doc/sjfYnXwBVVbJgt24PlVU/_update
{
    "doc" : {
        "title" : "iphon15"
    }
}

说明: 这种方式可以将数据原始内容保存,并在此基础上更新。

5、批量操作

POST /products/_doc/_bulk #批量索引两条文档
  {"index":{"_id":"1"}}
    {"title":"iphone14","price":8999.99,"created_at":"2021-09-15","description":"iPhone 13屏幕采用6.8英寸OLED屏幕"}
 {"index":{"_id":"2"}}
    {"title":"iphone15","price":8999.99,"created_at":"2021-09-15","description":"iPhone 15屏幕采用10.8英寸OLED屏幕"}
POST /products/_doc/_bulk #更新文档同时删除文档
 {"update":{"_id":"1"}}
  {"doc":{"title":"iphone17"}}
 {"delete":{"_id":2}}
 {"index":{}}
  {"title":"iphone19","price":8999.99,"created_at":"2021-09-15","description":"iPhone 19屏幕采用61.8英寸OLED屏幕"}

说明:批量时不会因为一个失败而全部失败,而是继续执行后续操作,在返回时按照执行的状态返回!

三、高级查询

ES中提供了一种强大的检索数据方式,这种检索方式称之为Query DSL,Query DSL是利用Rest API传递JSON格式的请求体(Request Body)数据与ES进行交互,这种方式的丰富查询语法让ES检索变得更强大,更简洁。

1、语法

# GET /索引名/_doc/_search {json格式请求体数据}
# GET /索引名/_search {json格式请求体数据}

2、测试数据

# 1.创建索引 映射
PUT /products/
{
  "mappings": {
    "properties": {
      "title":{
        "type": "keyword"
      },
      "price":{
        "type": "double"
      },
      "created_at":{
        "type":"date"
      },
      "description":{
        "type":"text"
      }
    }
  }
}
# 2.测试数据
PUT /products/_doc/_bulk
{"index":{}}
  {"title":"iphone12 pro","price":8999,"created_at":"2020-10-23","description":"iPhone 12 Pro采用超瓷晶面板和亚光质感玻璃背板,搭配不锈钢边框,有银色、石墨色、金色、海蓝色四种颜色。宽度:71.5毫米,高度:146.7毫米,厚度:7.4毫米,重量:187克"}
{"index":{}}
  {"title":"iphone12","price":4999,"created_at":"2020-10-23","description":"iPhone 12 高度:146.7毫米;宽度:71.5毫米;厚度:7.4毫米;重量:162克(5.73盎司) [5]  。iPhone 12设计采用了离子玻璃,以及7000系列铝金属外壳。"}
{"index":{}}
  {"title":"iphone13","price":6000,"created_at":"2021-09-15","description":"iPhone 13屏幕采用6.1英寸OLED屏幕;高度约146.7毫米,宽度约71.5毫米,厚度约7.65毫米,重量约173克。"}
{"index":{}}
  {"title":"iphone13 pro","price":8999,"created_at":"2021-09-15","description":"iPhone 13Pro搭载A15 Bionic芯片,拥有四种配色,支持5G。有128G、256G、512G、1T可选,售价为999美元起。"}

四、常见检索

1、查询所有[match_all]

match_all关键字:  返回索引中的全部文档

GET /products/_search
{
  "query": {
    "match_all": {}
  }
}

2、关键词查询(term)

term 关键字: 用来使用关键词查询

GET /products/_search
{
 "query": {
   "term": {
     "price": {
       "value": 4999
     }
   }
 }
}

NOTE1:  通过使用term查询得知ES中默认使用分词器为标准分词器(StandardAnalyzer),标准分词器对于英文单词分词,对于中文单字分词。

NOTE2:  通过使用term查询得知,在ES的Mapping Type 中 keyword , date ,integer, long , double , boolean or ip 这些类型不分词,只有text类型分词。

3、范围查询[range]

range 关键字: 用来指定查询指定范围内的文档

GET /products/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 1400,
        "lte": 9999
      }
    }
  }
}

4、前缀查询[prefix]

prefix 关键字: 用来检索含有指定前缀的关键词的相关文档

GET /products/_search
{
  "query": {
    "prefix": {
      "title": {
        "value": "ipho"
      }
    }
  }
}

5、通配符查询[wildcard]

wildcard 关键字: 通配符查询     ? 用来匹配一个任意字符  * 用来匹配多个任意字符

GET /products/_search
{
  "query": {
    "wildcard": {
      "description": {
        "value": "iphon*"
      }
    }
  }
}

6、多id查询[ids]

ids 关键字 : 值为数组类型,用来根据一组id获取多个对应的文档

GET /products/_search
{
  "query": {
    "ids": {
      "values": ["verUq3wBOTjuBizqAegi","vurUq3wBOTjuBizqAegk"]
    }
  }
}

7、模糊查询[fuzzy]

fuzzy 关键字: 用来模糊查询含有指定关键字的文档

GET /products/_search
{
  "query": {
    "fuzzy": {
      "description": "iphooone"
    }
  }
}

注意: fuzzy 模糊查询 最大模糊错误 必须在0-2之间

  1. 搜索关键词长度为 2 不允许存在模糊
  2. 搜索关键词长度为3-5 允许一次模糊
  3. 搜索关键词长度大于5 允许最大2模糊

8、布尔查询[bool]

  1. bool 关键字: 用来组合多个条件实现复杂查询
  2. must: 相当于&& 同时成立
  3. should: 相当于|| 成立一个就行
  4. must_not: 相当于!  不能满足任何一个
GET /products/_search
{
  "query": {
    "bool": {
      "must": [
        {"term": {
          "price": {
            "value": 4999
          }
        }}
      ]
    }
  }
}

9、多字段查询[multi_match]

GET /products/_search
{
  "query": {
    "multi_match": {
      "query": "iphone13 毫",
      "fields": ["title","description"]
    }
  }
}
注意: 字段类型分词,将查询条件分词之后进行查询改字段  如果该字段不分词就会将查询条件作为整体进行查询

10、默认字段分词查询[query_string]

GET /products/_search
{
  "query": {
    "query_string": {
      "default_field": "description",
      "query": "屏幕真的非常不错"
    }
  }
}
注意: 查询字段分词就将查询条件分词查询  查询字段不分词将查询条件不分词查询

11、高亮查询[highlight]

(1)highlight 关键字: 可以让符合条件的文档中的关键词高亮

GET /products/_search
{
  "query": {
    "term": {
      "description": {
        "value": "iphone"
      }
    }
  },
  "highlight": {
    "fields": {
      "*":{}
    }
  }
}

(2)自定义高亮html标签: 可以在highlight中使用pre_tags和post_tags

GET /products/_search
{
  "query": {
    "term": {
      "description": {
        "value": "iphone"
      }
    }
  },
  "highlight": {
    "post_tags": ["</span>"], 
    "pre_tags": ["<span style='color:red'>"],
    "fields": {
      "*":{}
    }
  }
}

(3)多字段高亮 使用require_field_match开启多个字段高亮

GET /products/_search
{
  "query": {
    "term": {
      "description": {
        "value": "iphone"
      }
    }
  },
  "highlight": {
    "require_field_match": "false",
    "post_tags": ["</span>"], 
    "pre_tags": ["<span style='color:red'>"],
    "fields": {
      "*":{}
    }
  }
}

12、返回指定条数[size]

size 关键字: 指定查询结果中返回指定条数。 默认返回值10条

GET /products/_search
{
  "query": {
    "match_all": {}
  },
  "size": 5
}

13、分页查询[form]

from 关键字: 用来指定起始返回位置,和size关键字连用可实现分页效果

GET /products/_search
{
  "query": {
    "match_all": {}
  },
  "size": 5,
  "from": 0
}

14、指定字段排序[sort]

GET /products/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ]
}

15、返回指定字段[_source]

_source 关键字: 是一个数组,在数组中用来指定展示那些字段

GET /products/_search
{
  "query": {
    "match_all": {}
  },
  "_source": ["title","description"]
}
© 版权声明
THE END
喜欢就支持一下吧
点赞55赞赏 分享
评论 抢沙发
头像
欢迎光临不念博客,留下您的想法和建议,祝您有愉快的一天~
提交
头像

昵称

取消
昵称

    暂无评论内容