安装Elasticsearch与基本配置
从Elastic官网下载对应系统的安装包(如Linux的.tar.gz或Windows的.zip)。解压后进入目录,修改config/elasticsearch.yml文件:
cluster.name: my-application
node.name: node-1
network.host: 0.0.0.0
http.port: 9200
启动服务:
./bin/elasticsearch # Linux
bin\elasticsearch.bat # Windows
验证安装:
curl -X GET "localhost:9200/"
索引创建与数据插入
创建名为products的索引并定义映射:
PUT /products
{
"mappings": {
"properties": {
"name": { "type": "text" },
"price": { "type": "double" },
"stock": { "type": "integer" }
}
}
}
插入文档数据:
POST /products/_doc/1
{
"name": "Laptop",
"price": 999.99,
"stock": 50
}
搜索查询实现
基本匹配查询:
GET /products/_search
{
"query": {
"match": {
"name": "Laptop"
}
}
}
范围与布尔组合查询:
GET /products/_search
{
"query": {
"bool": {
"must": [
{ "match": { "name": "Laptop" } }
],
"filter": [
{ "range": { "price": { "gte": 500 } } }
]
}
}
}
聚合分析与统计
价格分段统计:
GET /products/_search
{
"aggs": {
"price_ranges": {
"range": {
"field": "price",
"ranges": [
{ "to": 500 },
{ "from
1562

被折叠的 条评论
为什么被折叠?



