ElasticSearch 使用

一、索引操作

---------------------------------

创建索引(PUT)

PUT /索引名

curl -X PUT http://10.20.20.214:9200/shopping

设置映射关系(类似mysql的表的字段设置属性) (PUT /索引名/_mapping)

{"properties":{          //特性
    "name":{            //字段
        "type":"text",    //字段属性
        "index":true    //是否能被索引
        }
    }
}
curl -X PUT http://10.20.20.214:9200/user
{"acknowledged":true,"shards_acknowledged":true,"index":"user"}

curl -X PUT http://10.20.20.214:9200/user/_mapping?pretty -d '{"properties":{"name":{"type":"text","index":true}, "sex":{"type":"keyword","index":true},"tel":{"type":"keyword","index":false}}}' -H "Content-Type:application/json"
{
  "acknowledged" : true
}

查询映射关系:GET /索引名/_mapping

复制代码
 curl -X GET http://10.20.20.214:9200/user/_mapping?pretty
{
  "user" : {
    "mappings" : {
      "properties" : {
        "name" : {
          "type" : "text"
        },
        "sex" : {
          "type" : "keyword"
        },
        "tel" : {
          "type" : "keyword",
          "index" : false
        }
      }
    }
  }
}
复制代码
  • 查询字段type:keyword的则 match查询时,全词模糊匹配。
  • 查询字段type:text 的则 match查询时,输入的查询关键词会被拆分后再进行模糊匹配

查询索引(GET /_cat/indices?v)

GET http://10.20.20.214:9200/_cat/indices?v

二、数据操作

--------------------------------

添加数据 (body中添加数据{} json格式)

  • POST /索引名/_doc 
  • PUT /索引名/_create
curl -X POST http://10.20.20.214:9200/shopping/_doc  -d '{"name":"zhangmingda", "age":23}' -H "Content-Type:application/json"

查询数据 

  • 查单条数据:GET /索引名/_doc/索引ID
  • 查所有数据:GET /索引名/_search 
复制代码
[root@vm10-20-9-45 ElasticSearch]# curl -X GET http://10.20.20.214:9200/shopping/_search?pretty=true
{
  "took" : 438,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 6,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "4V9BSnsBL-0_1XxfCTs7",
        "_score" : 1.0,
        "_source" : {
          "name" : "zhangmingda",
          "age" : 23
        }
      },
      .......
    ]
  }
}
复制代码
  • 过滤查询

  • GET /index/_search?pretty body中传递查询参数
{"query":{"match":{"category":"小米"}}}
curl -X GET http://10.20.20.214:9200/shopping/_search?pretty -H "Content-Type:application/json" -d '{"query":{"match":{"category":"小米"}}}'
  • 查询所有(body传参)
{"query":{"match_all":{}}}
    • 分页查询
 {"query":{"match_all":{}},"from":0,"size":2}
复制代码
curl -X GET http://10.20.20.214:9200/shopping/_search?pretty -H "Content-Type:application/json" -d '{"query":{"match_all":{}},"from":0,"size":2}'
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 13,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "4V9BSnsBL-0_1XxfCTs7",
        "_score" : 1.0,
        "_source" : {
          "name" : "zhangmingda",
          "age" : 23
        }
      },
      {
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "519NSnsBL-0_1XxfIztW",
        "_score" : 1.0,
        "_source" : {
          "name" : "zhangmingda",
          "age" : 23
        }
      }
    ]
  }
}
复制代码
  • 只要数据的特定字段"query":{"match_all":{}},"_source":["title"]}
curl -X GET http://10.20.20.214:9200/shopping/_search?pretty -H "Content-Type:application/json" -d '{"query":{"match_all":{}},"_source":["title"]}'
复制代码
[root@vm10-20-9-45 ~]# curl -X GET http://10.20.20.214:9200/shopping/_search?pretty -H "Content-Type:application/json" -d '{"query":{"match_all":{}},"_source":["title"]}'
{
  "took" : 13,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 13,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "4V9BSnsBL-0_1XxfCTs7",
        "_score" : 1.0,
        "_source" : { }
      },
      {
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "519NSnsBL-0_1XxfIztW",
        "_score" : 1.0,
        "_source" : { }
      },
      {
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "6F9NSnsBL-0_1XxfMjvj",
        "_score" : 1.0,
        "_source" : { }
      },
      {
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "10001",
        "_score" : 1.0,
        "_source" : {
          "title" : "华为V8"
        }
      },
      {
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "10002",
        "_score" : 1.0,
        "_source" : {
          "title" : "小米手机4"
        }
      },
      {
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "10003",
        "_score" : 1.0,
        "_source" : {
          "title" : "小米手机4"
        }
      },
      {
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "10004",
        "_score" : 1.0,
        "_source" : {
          "title" : "小米手机4"
        }
      },
      {
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "10005",
        "_score" : 1.0,
        "_source" : {
          "title" : "小米手机6"
        }
      },
      {
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "10008",
        "_score" : 1.0,
        "_source" : {
          "title" : "小米手机8"
        }
      },
      {
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "20008",
        "_score" : 1.0,
        "_source" : {
          "title" : "华为V1"
        }
      }
    ]
  }
}
输出
复制代码
  • 查询&排序 & 分页
    "sort": {"price":{"order":"desc"}},"from":2,"size":3
    {"query":{"match_all":{}},"_source":["title","price"],"sort": {"price":{"order":"desc"}},"from":2,"size":3}

     

curl -X GET http://10.20.20.214:9200/shopping/_search?pretty -H "Content-Type:application/json" -d '{"query":{"match_all":{}},"_source":["title","price"],"sort": {"price":{"order":"desc"}},"from":2,"size":3}'
  • 全文查询,匹配即可,不做字符拆分"match_phrase"
curl -X GET http://10.20.20.214:9200/shopping/_search?pretty -H "Content-Type:application/json" -d '{"query":{"match_phrase":{"category":"小米"}}}'
复制代码
curl -X GET http://10.20.20.214:9200/shopping/_search?pretty -H "Content-Type:application/json" -d '{"query":{"match_phrase":{"category":"米"}}}'
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : 0.50209194,
    "hits" : [
      {
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "10002",
        "_score" : 0.50209194,
        "_source" : {
          "title" : "小米手机4",
          "category" : "小米",
          "images" : "http://www.xiaomi.com",
          "price" : 2993.0
        }
      },
      {
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "10003",
        "_score" : 0.50209194,
        "_source" : {
          "title" : "小米手机4",
          "category" : "小米",
          "images" : "http://www.xiaomi.com",
          "price" : 2993.0
        }
      },
      {
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "10004",
        "_score" : 0.50209194,
        "_source" : {
          "title" : "小米手机4",
          "category" : "小米",
          "images" : "http://www.xiaomi.com",
          "price" : 2993.0
        }
      },
      {
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "10005",
        "_score" : 0.50209194,
        "_source" : {
          "title" : "小米手机6",
          "category" : "小米",
          "images" : "http://www.xiaomi.com",
          "price" : 2999.0
        }
      },
      {
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "10008",
        "_score" : 0.50209194,
        "_source" : {
          "title" : "小米手机8",
          "category" : "小米",
          "images" : "http://www.xiaomi.com",
          "price" : 8999.0
        }
      }
    ]
  }
}
输出
复制代码
  • 高亮显示字段 "highlight":{"fields":{"字段":{}}}
curl -X GET http://10.20.20.214:9200/shopping/_search?pretty -H "Content-Type:application/json" -d '{"query":{"match_phrase":{"category":"米"}},"highlight":{"fields":{"category":{}}}}'
复制代码
curl -X GET http://10.20.20.214:9200/shopping/_search?pretty -H "Content-Type:application/json" -d '{"query":{"match_phrase":{"category":"米"}},"highlight":{"fields":{"category":{}}}}'
{
  "took" : 36,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : 0.50209194,
    "hits" : [
      {
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "10002",
        "_score" : 0.50209194,
        "_source" : {
          "title" : "小米手机4",
          "category" : "小米",
          "images" : "http://www.xiaomi.com",
          "price" : 2993.0
        },
        "highlight" : {
          "category" : [
            "小<em>米</em>"
          ]
        }
      },
      {
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "10003",
        "_score" : 0.50209194,
        "_source" : {
          "title" : "小米手机4",
          "category" : "小米",
          "images" : "http://www.xiaomi.com",
          "price" : 2993.0
        },
        "highlight" : {
          "category" : [
            "小<em>米</em>"
          ]
        }
      },
      {
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "10004",
        "_score" : 0.50209194,
        "_source" : {
          "title" : "小米手机4",
          "category" : "小米",
          "images" : "http://www.xiaomi.com",
          "price" : 2993.0
        },
        "highlight" : {
          "category" : [
            "小<em>米</em>"
          ]
        }
      },
      {
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "10005",
        "_score" : 0.50209194,
        "_source" : {
          "title" : "小米手机6",
          "category" : "小米",
          "images" : "http://www.xiaomi.com",
          "price" : 2999.0
        },
        "highlight" : {
          "category" : [
            "小<em>米</em>"
          ]
        }
      },
      {
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "10008",
        "_score" : 0.50209194,
        "_source" : {
          "title" : "小米手机8",
          "category" : "小米",
          "images" : "http://www.xiaomi.com",
          "price" : 8999.0
        },
        "highlight" : {
          "category" : [
            "小<em>米</em>"
          ]
        }
      }
    ]
  }
}
输出
复制代码
  • 分组查询
复制代码
{
    "aggs":{ //聚合操作
        "price_group":{ //名称随意起名
            "terms":{    //分组
                "field": "price" //分组字段
            }
        }
    }
}
复制代码

测试分组查询:

curl -X GET http://10.20.20.214:9200/shopping/_search?pretty -d '{"aggs":{"price_group":{"terms":{"field":"price"}}}}' -H "Content-Type:application/json"
复制代码
curl -X GET http://10.20.20.214:9200/shopping/_search?pretty -d '{"aggs":{"price_group":{"terms":{"field":"price"}}}}' -H "Content-Type:a
{
  "took" : 23,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 13,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "4V9BSnsBL-0_1XxfCTs7",
        "_score" : 1.0,
        "_source" : {
          "name" : "zhangmingda",
          "age" : 23
        }
      },
      .......
    ]
  },
  "aggregations" : {
    "price_group" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 2993.0,
          "doc_count" : 4
        },
        {
          "key" : 4999.0,
          "doc_count" : 2
        },
        {
          "key" : 8999.0,
          "doc_count" : 2
        },
        {
          "key" : 2999.0,
          "doc_count" : 1
        },
        {
          "key" : 3999.0,
          "doc_count" : 1
        }
      ]
    }
  }
}
输出
复制代码

不显示原始数据:加 "size":0

{"aggs":{"price_group":{"terms":{"field":"price"}}},"size":0}

结果

复制代码
curl -X GET http://10.20.20.214:9200/shopping/_search?pretty -d '{"aggs":{"price_group":{"terms":{"field":"price"}}},"size":0}' -H "Content-Type:application/json"
{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 13,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "price_group" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 2993.0,
          "doc_count" : 4
        },
        {
          "key" : 4999.0,
          "doc_count" : 2
        },
        {
          "key" : 8999.0,
          "doc_count" : 2
        },
        {
          "key" : 2999.0,
          "doc_count" : 1
        },
        {
          "key" : 3999.0,
          "doc_count" : 1
        }
      ]
    }
  }
}
复制代码

 

 

 

更新数据

  • 全量更新 PUT  /index/_doc/id  -d {'数据'}
curl -X PUT http://10.20.20.214:9200/shopping/_doc/1002  -d '{"name":"zhangmingda", "age":22}' -H "Content-Type:application/json"
  • 局部数据更新 POST /index/_update/id -d {"doc":{数据}}
curl -X POST http://10.20.20.214:9200/shopping/_update/1002  -d '{"doc":{"age":33}}' -H "Content-Type:application/json"

删除数据

  • DELETE /index/_doc/数据id
curl -X DELETE http://10.20.20.214:9200/shopping/_doc/1002  
{"_index":"shopping","_type":"_doc","_id":"1002","_version":4,"result":"deleted","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":9,"_primary_term":1}

 

posted on   zhangmingda  阅读(51)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2019-08-15 cuda
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示