ElasticSearch14:索引

ElasticSearch14:索引

倒排索引

倒排索引,也叫反向索引
{ "products" : { "mappings" : { "properties" : { "description" : { "type" : "text" }, "price" : { "type" : "long" }, "title" : { "type" : "keyword" } } } } }
录入数据如下字段,有三个字段title、price、description等
_id
title
price
description
1
蓝月亮洗衣液
19.9
蓝月亮洗衣液高效
2
iphone13
19.9
不错的手机
3
小浣熊干脆面
1.5
小浣熊好吃
索引区和元数据图
notion image
在ES除了text类型分词,其他类型不分词,因此根据不同字段创建索引如下
  • title字段
term
_id(文档id)
蓝月亮洗衣液
1
ihpone13
2
小浣熊干脆面
3
  • price字段
term
_id(文档id)
19.9
[1,2]
1.5
3
  • description字段
term
_id
term
_id
term
_id
1
2
3
1
2
3
1
2
3
1
2
3
1
2
3
1
[1:1:9,2:1:6,3:1:6]
1
1
注意📢📢📢:ES分别为每个字段床都创建一个倒排索引。因此查询时查询字段的term,就知道文档的ID,快速找到文档。

案例演示

# 索引案例 DELETE /products PUT /products { "mappings" : { "properties" : { "description" : { "type" : "text" }, "price" : { "type" : "long" }, "title" : { "type" : "keyword" } } } } PUT /products/_bulk {"index":{"_id":1}} {"title":"蓝月亮洗衣液","price":19.9,"description":"蓝月亮洗衣液很高效"} {"index":{"_id":2}} {"title":"iphone13","price":19.9,"description":"很不错的手机"} {"index":{"_id":3}} {"title":"小浣熊干脆面","price":1.5,"description":"小浣熊很好吃"} GET /products/_search { "query": { "match_all": {} } } GET /products/_search { "query": { "term": { "description": { "value": "很" } } } }
notion image