阅读es文档

This commit is contained in:
asahi
2024-10-29 20:03:05 +08:00
parent c06915ddfe
commit d844663f86

View File

@@ -484,6 +484,129 @@ DELETE /my-explicit-mappings-books
删除索引将会永久删除其document、shards、元数据。 删除索引将会永久删除其document、shards、元数据。
### 全文本搜索和过滤
如下示例展示了如何实现cook blog的搜索功能。
#### 创建索引
创建`cooking_blog`索引
```
PUT /cooking_blog
```
为索引定义mapping
```
PUT /cooking_blog/_mapping
{
"properties": {
"title": {
"type": "text",
"analyzer": "standard",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"description": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"author": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"date": {
"type": "date",
"format": "yyyy-MM-dd"
},
"category": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"tags": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"rating": {
"type": "float"
}
}
}
```
上述mapping定义含义如下
- 对于`text`类型的field如果analyzer没有指定那么会默认使用`standard` analyzer
- 在上述示例中,使用了`multi fields`,将字段既作为`text`来进行全文搜索,又作为`keyword`来进行聚合和排序。在该字段上既支持全文搜索又支持青雀匹配和过滤。如果使用dynamic mapping那么multi-fields将会自动被创建。
- `ignore_above`不会索引`keyword`field中超过256个字符长度的值。默认情况下keyword field其ignore_above的值为256
> #### multi-field
> 对同一个字段按不同的方式进行索引有时候很必要对于multi-fields一个字符串类型字段可以被映射到`text`类型用于全文索引,也可以被映射到`keyword`类型用作排序和聚合。
>
> 示例如下:
> ```
> PUT my-index-000001
> {
> "mappings": {
> "properties": {
> "city": {
> "type": "text",
> "fields": {
> "raw": {
> "type": "keyword"
> }
> }
> }
> }
> }
> }
>
> PUT my-index-000001/_doc/1
> {
> "city": "New York"
> }
>
> PUT my-index-000001/_doc/2
> {
> "city": "York"
> }
>
> GET my-index-000001/_search
> {
> "query": {
> "match": {
> "city": "york"
> }
> },
> "sort": {
> "city.raw": "asc"
> },
> "aggs": {
> "Cities": {
> "terms": {
> "field": "city.raw"
> }
> }
> }
> }
> ```
#### 将blog示例添加到索引中