elasticsearch常用命令总结

本文介绍如何使用Elasticsearch进行各种查询操作,包括简单的数据检索、条件筛选、排序及限制结果数量等。通过实例演示了如何针对特定字段进行精确匹配、模糊匹配、范围查询以及组合查询等高级功能。

检查健康状态
curl 'localhost:9200/_cat/health?v'

获得集群中的节点列表
curl 'localhost:9200/_cat/nodes?v'

列出所有的索引
curl -XGET http://localhost:9200/_cat/indices?v

列出索引user的所有表结构
curl -XGET http://localhost:9200/user/_mapping?pretty

列出索引user中log表的结构
curl -XGET http://localhost:9200/user/log/_mapping?pretty

简单查询
curl -XGET 'http://localhost:9200/user/log/_search?pretty'  返回前10条数据
curl -XGET 'http://localhost:9200/user/log/1?pretty'  按id查询
curl -XGET 'http://localhost:9200/user/log/_search?pretty'  -d '{
    "query" : {
        "term" : { "name" : "test" }
    }
}'

查找:
查找索引user中log表的url字段的值为http://www.test.com的文档(记录)

curl -XGET 'http://localhost:9200/user/log/_search?pretty' -d '{
    "query" : {
        "term" : { "url" : "http://www.test.com" }
    }
}'

查找索引user中log表的url字段的值为http://www.test.com的文档(记录),并按create_time降序输出

curl -XPOST 'http://localhost:9200/user/log/_search?pretty' -d '{
    "query" : {
        "term" : { "url" : "http://www.test.com" }
    },"sort" : [{ "create_time" : {"order" : "desc"}} ]
}'

查找索引user中log表的url字段的值为http://www.test.com的文档(记录),并输出前20个

curl -XPOST 'http://localhost:9200/user/log/_search?pretty' -d '{
    "query" : {
        "term" : { "url" : "http://www.test.com" }
    },
    "from":0,
    "size":20
}'

查找索引user中log表的url字段的值是以http://www.test.com开头的文档(记录)

curl -XPOST 'http://localhost:9200/user/log/_search?pretty' -d '{
  "query": {
    "prefix": {
      "url": {
        "value": "http://www.test.com"
      }
    }
  }
}'

查找索引user中log表的url字段的值是以http://www.test.com开头的文档(记录),输出到1.txt文件中

curl -XPOST 'http://localhost:9200/user/log/_search?pretty' -d '{
  "query": {
    "prefix": {
      "url": {
        "value": "http://www.test.com"
      }
    }
  }
}' > 1.txt


使用通配符做查找条件

curl -XPOST 'http://localhost:9200/user/log/_search?pretty' -d '{
    "query":{
        "bool":{
            "must":[
                {
                    "wildcard":{
                        "url":"http://www.test.com?id=*"
                    }
                }
            ]
        }
    },
    "from":0,
    "size":70
    , "sort" : [{ "create_time" : {"order" : "asc"}} ]
}' > 2.txt


组合搜索

{
    "query": {
        "bool": {
            "minimum_should_match": 1,
            "must": [{
                    "match": {
                        "XB": "2 "
                    }
                },
                {
                    "wildcard": {
                        "XM": "王*"
                    }
                }
            ],
            "should": [{
                "range": {
                    "CSRQ": {
                        "gte": "2010-09-01",
                        "lte": "2014-09-01"
                    }
                }
            }]
        }
    },
    "from": 0,
    "size": 10
}

查找ip为空的文档(记录)

curl -XPOST 'http://localhost:9200/user/log/_search?pretty' -d '{
  "query": {
    "bool": {
      "must_not": {
        "exists": {
          "field": "ip"
        }
      }
    }
  },
    "from":0,
    "size":20
}'

查找ip不为空的文档(记录)

curl -XPOST 'http://localhost:9200/user/log/_search?pretty' -d '{
  "query": {
    "bool": {
      "must": {
        "exists": {
          "field": "字段名"
        }
      }
    }
  }
}'

 

### Elasticsearch 常用 API 请求示例 以下是几种常见的 Elasticsearch API 请求及其用途: #### 1. 创建索引 通过 `PUT` 方法可以创建一个新的索引,并定义其映射结构。以下是一个创建名为 `blog` 的索引并指定 `article` 类型的示例[^4]。 ```json PUT /blog { "mappings": { "properties": { "id": { "type": "long", "store": true, "index": true }, "title": { "type": "text", "store": true, "index": true, "analyzer": "standard" }, "content": { "type": "text", "store": true, "index": true, "analyzer": "standard" } } } } ``` --- #### 2. 插入文档 可以通过 `POST` 或 `PUT` 方法向已存在的索引中插入文档。以下是在 `blog` 索引下插入一条记录的示例。 ```json POST /blog/_doc/1 { "id": 1, "title": "Introduction to Elasticsearch", "content": "This is an introduction to using Elasticsearch." } ``` --- #### 3. 查询文档 使用 `_search` 接口执行查询操作,支持多种查询方式。下面展示了基于布尔查询返回状态码为 `200` 的前 10 条记录的例子[^2]。 ```json GET /_search { "size": 10, "query": { "bool": { "must": [ { "term": { "status": 200 } } ] } } } ``` --- #### 4. 更新文档 更新现有文档的部分字段可使用 `POST _update/{id}` 接口。例如,更新 ID 为 `1` 的文档标题。 ```json POST /blog/_update/1 { "doc": { "title": "Updated Title of the Document" } } ``` --- #### 5. 删除文档 删除特定文档可通过 `DELETE` 方法实现。例如,删除 ID 为 `1` 的文档。 ```json DELETE /blog/_doc/1 ``` --- #### 6. 使用 DSL 进行复杂查询 对于更复杂的场景,可以利用 Elasticsearch 提供的 Query DSL 执行高级查询。以下例子演示了如何查找 `content` 字段包含关键词 `elastic` 同时满足条件 `(title 包含 lucene 或 solr)` 的文档[^3]。 ```json GET /_search { "query": { "query_string": { "default_field": "content", "query": "elastic AND (title:lucene OR title:solr)" } } } ``` --- #### 7. 获取集群健康状况 监控 Elasticsearch 集群的状态可用如下命令获取整体健康信息。 ```json GET /_cluster/health ``` --- #### 8. 设置集群配置 动态调整集群级别的参数也可以通过 RESTful API 实现。比如修改分片分配策略。 ```json PUT /_cluster/settings { "persistent": { "cluster.routing.allocation.enable": "all" } } ``` --- ### 总结 上述列举了几种常用的 Elasticsearch API 请求形式,涵盖了从基础的数据增删改查到较为深入的集群管理功能。每一种接口都提供了灵活多样的选项来适应不同的业务需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值