创建索引语法结构
PUT /${index_name} // 索引名称
{
"settings":{
... 索引相关的配置项目,如何:分配个数 副分片个数等
},
"mappings":{
... 数据的结构
}
}
实现Demo
步骤 1: 创建商品搜索索引
首先,使用 HTTP PUT 请求来创建一个名为 "product_index" 的索引。你可以指定该索引的设置,比如分片数量和副本数量。这里我们假设使用默认设置。
PUT http://localhost:9200/helloword
步骤 2: 向索引中添加商品数据
接下来,使用 HTTP POST 请求向新创建的 "product_index" 索引中添加商品数据。以下是一个简单的商品数据示例:
POST http://localhost:9200/helloword/_doc/1
{
"product_name": "华为 P40 手机",
"description": "一款性能强劲的智能手机,拥有卓越的摄像和处理能力。",
"price": 3999.00,
"in_stock": true,
"category": "电子产品",
"tags": ["智能手机", "华为", "科技"]
}
POST http://localhost:9200/helloword/_doc/2
{
"product_name": "苹果 MacBook Pro",
"description": "一款高性能的笔记本电脑,适用于专业工作和日常使用。",
"price": 9999.00,
"in_stock": true,
"category": "电脑",
"tags": ["笔记本", "苹果", "电脑"]
}
在这个示例中,我们向 "product_index" 索引中添加了两个商品文档。每个文档都包含了商品的名称、描述、价格、库存情况、类别和标签信息。
步骤 3: 执行搜索查询
一旦数据被添加到索引中,你可以使用 HTTP POST 请求执行搜索查询。以下是一个简单的搜索查询示例,用于查找所有包含 "智能手机" 标签的商品:
POST http://localhost:9200/helloword/_search
{
"query": {
"match": {
"tags": "智能手机"
}
}
}
通过执行这个搜索查询,Elasticsearch 将会返回包含 "智能手机" 标签的商品文档。
通过这些步骤,你可以创建一个商品搜索的索引,并向其中添加商品数据。随后,你可以执行搜索查询来检索和分析这些商品数据。
最后修改于 2023-12-15 11:40:32
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付

