elasticsearch 基础知识
正向索引和倒排索引
与 MYSQL 的关系
环境
下载windows版本
https://www.elastic.co/cn/downloads/elasticsearch
HTTPS报错
elastic search 报错
[2023-09-24T21:04:50,988][WARN ][o.e.h.n.Netty4HttpServerTransport] [HOMEDESKTOP-TG] received plaintext http traffic on an https channel, closing connection Netty4HttpChannel
于Windows版本的Elasticsearch,你需要在elasticsearch.yml
文件中进行相关配置的修改。
elasticsearch.yml
文件通常位于以下两个位置之一:
- Elasticsearch安装目录的
config
文件夹下(例如:E:\elasticsearch-8.10.2\config\elasticsearch.yml
)。 - Windows系统的环境变量
ES_HOME
指向的目录的config
文件夹下。
你可以使用文本编辑器打开该文件,根据需要进行相关的配置修改。
至于你提到的报错信息,看起来是Elasticsearch的HTTPS配置问题。Elasticsearch默认配置为只接受HTTPS连接,如果你的连接方式是HTTP,就会出现这样的警告。
如果你不想使用HTTPS连接,可以尝试关闭Elasticsearch的SSL验证。这可以通过在elasticsearch.yml
文件中添加以下配置项来实现:
xpack.security.enabled: false
Kibana
exception
Root causes:
index_not_found_exception: no such index [.kibana]
好吧,是硬盘空间的问题,如果是健康 是red 的时候,就会出现这些问题
HTTP - 查询
索引
创建一个 shopping 的索引
创建索引
PUT http://localhost:9200/shopping
返回
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "shopping"
}
如果用POST
返回
{
"error": "Incorrect HTTP method for uri [/shopping] and method [POST], allowed: [GET, PUT, HEAD, DELETE]",
"status": 405
}
查询索引
GET http://localhost:9200/shopping
返回
{
"shopping": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"number_of_shards": "1",
"provided_name": "shopping",
"creation_date": "1695658514232",
"number_of_replicas": "1",
"uuid": "3_vwUeDVSqK2G6_eudu0pg",
"version": {
"created": "8100299"
}
}
}
}
}
删除索引
DELETE http://localhost:9200/shopping
返回
{
"acknowledged": true
}
查询索引浏览
GET http://localhost:9200/_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .kibana-observability-ai-assistant-conversations-000001 GwjgkNC5Q3OqJzgUX6jMLw 1 0 0 0 248b 248b
green open .kibana-observability-ai-assistant-kb-000001 R_ljpbbkTCK1eAqmn0Vltg 1 0 0 0 248b 248b
yellow open iot_log GBJeKhPxTMqzf78iaMevBg 1 1 0 0 248b 248b
yellow open shopping 3_vwUeDVSqK2G6_eudu0pg 1 1 0 0 226b 226b
这里的 _cat
是查询的意思
文档
创建文档 PUT/POST
POST http://localhost:9200/shopping/_doc
{
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999.00
}
结果
{
"_index": "shopping",
"_id": "pBInzYoBv9F9H8lF0S9_",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
查询文档全局 '_search'
GET http://localhost:9200/shopping/_search
返回
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "shopping",
"_id": "pBInzYoBv9F9H8lF0S9_",
"_score": 1,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999
}
}
]
}
}
查询主键 '_doc'
"found": true 表示找到
返回
GET http://localhost:9200/shopping/_doc/pBInzYoBv9F9H8lF0S9_
{
"_index": "shopping",
"_id": "pBInzYoBv9F9H8lF0S9_",
"_version": 1,
"_seq_no": 0,
"_primary_term": 1,
"found": true,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999
}
}
更新: 按id (PUT全量更新,不常用)
PUT http://localhost:9200/shopping/_doc/pBInzYoBv9F9H8lF0S9_
{
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 4999
}
结果,更新成功 , "result": "updated"
{
"_index": "shopping",
"_id": "pBInzYoBv9F9H8lF0S9_",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 2
}
更新: 按id (POST局部更新,常用)
POST http://localhost:9200/shopping/_update/pBInzYoBv9F9H8lF0S9_
{
"doc": {
"title": "小米手机2"
}
}
结果
{
"_index": "shopping",
"_id": "pBInzYoBv9F9H8lF0S9_",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 2
}
查询
查询 - 条件分词 match
GET http://localhost:9200/shopping/_search
{
"query":{
"match":{
"category":"小米"
}
}
}
查询 - 全量查询 match_all
GET http://localhost:9200/shopping/_search
{
"query": {
"match_all": {
}
}
}
查询 - 分页 'from' 'size'
GET http://localhost:9200/shopping/_search
{
"query": {
"match_all": {
}
},
"from":0,
"size":3
}
查询 - 返回数据过滤 '_source'
只返回 title
GET http://localhost:9200/shopping/_search
{
"query": {
"match_all": {
}
},
"from":0,
"size":3,
"_source": ["title"]
}
结果
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 9,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "shopping",
"_id": "pBInzYoBv9F9H8lF0S9_",
"_score": 1,
"_source": {
"title": "小米手机2"
}
},
{
"_index": "shopping",
"_id": "es0G0ooBmBrU9yypB0AI",
"_score": 1,
"_source": {
"title": "小米手机3"
}
},
{
"_index": "shopping",
"_id": "e80G0ooBmBrU9yypJ0BY",
"_score": 1,
"_source": {
"title": "小米手机4"
}
}
]
}
}
查询 - 排序 'sort'
GET http://localhost:9200/shopping/_search
{
"query": {
"match_all": {
}
},
"from":0,
"size":3,
"_source": ["title"],
"sort":{
"price":{
"order" : "asc"
}
}
}
结果:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 9,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "shopping",
"_id": "es0G0ooBmBrU9yypB0AI",
"_score": null,
"_source": {
"title": "小米手机3"
},
"sort": [
1999
]
},
{
"_index": "shopping",
"_id": "e80G0ooBmBrU9yypJ0BY",
"_score": null,
"_source": {
"title": "小米手机4"
},
"sort": [
2199
]
},
{
"_index": "shopping",
"_id": "fM0G0ooBmBrU9yypT0AH",
"_score": null,
"_source": {
"title": "小米手机10"
},
"sort": [
2999
]
}
]
}
}
查询 - 多条件 bool 'must'
must 是 AND
shold 是 OR
GET http://localhost:9200/shopping/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "米手"
}
},
{
"match": {
"price": 2999
}
}
]
}
}
}
返回,分词查询,所以"title": "华为手机1" 也被查出来了
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.8498011,
"hits": [
{
"_index": "shopping",
"_id": "fM0G0ooBmBrU9yypT0AH",
"_score": 1.8498011,
"_source": {
"title": "小米手机10",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 2999
}
},
{
"_index": "shopping",
"_id": "fc0G0ooBmBrU9yyptEC8",
"_score": 1.0512933,
"_source": {
"title": "华为手机1",
"category": "华为",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 2999
}
},
{
"_index": "shopping",
"_id": "fs0G0ooBmBrU9yypwkB9",
"_score": 1.0512933,
"_source": {
"title": "华为手机2",
"category": "华为",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 2999
}
}
]
}
}
查询 - 多条件 bool 'should'
must 是 AND
should 是 OR
GET http://localhost:9200/shopping/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"title": "小米"
}
},
{
"match": {
"title": "华为"
}
}
]
}
}
}
查询 - 多条件 bool 范围 'range'
范围价格超过 4000 的
gt 大于
lt 小于
GET http://localhost:9200/shopping/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"title": "小米"
}
},
{
"match": {
"title": "华为"
}
}
],
"filter":{
"range":{
"price":{
"gt": 4000
}
}
}
}
}
}
查询 - 短语完全匹配 match_phrase
短语完全匹配
GET http://localhost:9200/shopping/_search
{
"query": {
"match_phrase": {
"title": "米手"
}
}
}
查询 - 聚合分组 aggs
GET http://localhost:9200/shopping/_search
{
"aggs": { //聚合操作
"price_group": { //名字随便起
"terms": { //分组
"field": "price"
}
}
},
"size":0 //丢掉原始数据
}
返回结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 11,
"relation": "eq"
},
"max_score": 1,
"hits": []
},
"aggregations": {
"price_group": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 4999,
"doc_count": 4
},
{
"key": 2999,
"doc_count": 3
},
{
"key": 3999,
"doc_count": 2
},
{
"key": 1999,
"doc_count": 1
},
{
"key": 2199,
"doc_count": 1
}
]
}
}
}
查询 - 平均值 'avg'
GET http://localhost:9200/shopping/_search
{
"aggs": { //聚合操作
"price_avg": { //名字随便起
"avg": { //平均值
"field": "price"
}
}
},
"size": 0 //丢掉原始数据
}
结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 11,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"price_avg": {
"value": 3744.4545454545455
}
}
}
模糊查询
假如有2条数据
{
"_index": "shopping",
"_id": "hM1C0ooBmBrU9yypO0Cw",
"_score": 1,
"_source": {
"title": "John Cleese criticizes BBC removal of ‘Fawlty Towers’ episode due to racial slurs 'We were not supporting his views,' said the 'Monty Python' star of 'Fawlty Towers",
"category": "华为",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 4999
}
},
{
"_index": "shopping",
"_id": "hc1E0ooBmBrU9yypyEBf",
"_score": 1,
"_source": {
"title": "We were not supporting his views",
"category": "华为",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 4999
}
}
wildcard 类似 like
和SQL的 LIKE
一样
注意,这里使用了 .keyword
后缀,这是因为我们在映射中定义了 "title" 字段的 "keyword" 子字段。在 wildcard 查询中,我们需要明确指定这个子字段。
另外,你需要注意的是,wildcard 查询是区分大小写
的。如果你希望执行不区分大小写的搜索,你可能需要调整你的映射或使用其他查询类型。
{
"query": {
"wildcard": {
"title.keyword": "*We were*"
}
}
}
结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "shopping",
"_id": "hM1C0ooBmBrU9yypO0Cw",
"_score": 1,
"_source": {
"title": "John Cleese criticizes BBC removal of ‘Fawlty Towers’ episode due to racial slurs 'We were not supporting his views,' said the 'Monty Python' star of 'Fawlty Towers",
"category": "华为",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 4999
}
},
{
"_index": "shopping",
"_id": "hc1E0ooBmBrU9yypyEBf",
"_score": 1,
"_source": {
"title": "We were not supporting his views",
"category": "华为",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 4999
}
}
]
}
}
如果使用
{
"query": {
"wildcard": {
"title.keyword": "*We were not supporting his views,*"
}
}
}
结果
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "shopping",
"_id": "hM1C0ooBmBrU9yypO0Cw",
"_score": 1,
"_source": {
"title": "John Cleese criticizes BBC removal of ‘Fawlty Towers’ episode due to racial slurs 'We were not supporting his views,' said the 'Monty Python' star of 'Fawlty Towers",
"category": "华为",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 4999
}
}
]
}
}
忽略大小写
{
"query": {
"wildcard": {
"title.keyword":{
"value": "*we were not supporting HIS VIEWS,*",
"case_insensitive":true
}
}
}
}
一样可以搜索出来
fuzzy 纠错查询
是一种误拼写时的fuzzy模糊搜索技术,用于搜索的时候可能输入的文本会出现误拼写的情况。
比如:输入”方财兄“,这时候也要匹配到“方才兄”。
prefix 前缀查询
这种只支持前缀查询,属于模糊查询的子集,叫前缀查询(prefix),返回包含指定前缀的所有文档。
精确查找
精确查找 term
避免对
文本
字段使用查询search 文本, use the
match
query instead
搜索数字
POST shopping/_search
{
"query": {
"term": {
"price": 2999
}
}
}
搜索文本
{
"query": {
"match": {
"title.keyword": "小米手机4"
}
}
}
批量精确查找 terms
POST shopping/_search
{
"query": {
"terms": {
"title.keyword": [
"小米手机4",
"小米手机2"
]
}
}
}