ES基本语法

1.1. 概念

索引(Index)文档

PUT /索引名称/_doc/文档ID
{
  "field1": "value1",
  "field2": "value2",
  ...
}

获取(Get)文档

GET /索引名称/_doc/文档ID

更新(Update)文档

POST /索引名称/_update/文档ID
{
  "doc": {
    "field1": "new_value1",
    "field2": "new_value2",
    ...
  }
}

删除(Delete)文档

DELETE /索引名称/_doc/文档ID

搜索(Search)文档

GET /索引名称/_search
{
  "query": {
    "match": {
      "field": "value"
    }
  }
}

这些语句中的关键点解释如下:

  • /索引名称:指定要操作的索引名称。
  • /_doc:指定文档类型,从 Elasticsearch 7.x 版本开始,默认类型为 _doc
  • /文档ID:指定要操作的文档的唯一标识符。

在实际使用中,你需要将这些语句中的占位符替换为你实际的索引名称、文档ID和字段值。

请注意,Elasticsearch 还提供了更多高级的查询和操作功能,例如聚合、过滤器等。以上只是一些基本的示例,你可以根据具体需求进行更复杂的查询和操作。

1.2. 基本语法

// 获取所有
GET /_search
{
  "took": 410,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 0,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  }
}

1 took:耗费了几毫秒
2 timed_out:是否超时,false是没有,默认无timeout
3 _shards:shards fail的条件(primary和replica全部挂掉),不影响其他shard。默认情况下来说,一个搜索请求,会打到一个index的所有primary shard上去,当然了,每个primary shard都可能会有一个或多个replic shard,所以请求也可以到primary shard的其中一个replica shard上去。
4 hits.total:本次搜索,返回了几条结果
5 hits.max_score:score的含义,就是document对于一个search的相关度的匹配分数,越相关,就越匹配,分数也高
6 hits.hits:包含了匹配搜索的document的详细数据,默认查询前10条数据,按_score降序排序

创建索引


// 创建索引
PUT /test_es
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      },
      "age": {
        "type": "integer"
      },
      "email": {
        "type": "keyword"
      },
      "date_of_birth": {
        "type": "date"
      },
      "address": {
        "type": "nested",
        "properties": {
          "street": {
            "type": "text"
          },
          "city": {
            "type": "text"
          },
          "country": {
            "type": "keyword"
          }
        }
      }
    }
  }
}
{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "test_es"
}

创建值

// 为索引添加值
POST /test_es/_doc/1
{
  "name": "John",
  "age": 30,
  "email": "john@example.com",
  "date_of_birth": "1990-01-01",
  "address": {
    "street": "123 Main St",
    "city": "New York",
    "country": "USA"
  }
}

POST /test_es/_doc/2
{
  "name": "Jane",
  "age": 25,
  "email": "jane@example.com",
  "date_of_birth": "1995-05-10",
  "address": {
    "street": "456 Elm St",
    "city": "London",
    "country": "UK"
  }
}
{
  "_index": "test_es",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}

条件查询

// 查询
GET /test_es/_search
{
  "query": {
    "match": {
      "name": "John"
    }
  }
}
{
  "took": 475,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 0.6931471,
    "hits": [
      {
        "_index": "test_es",
        "_id": "1",
        "_score": 0.6931471,
        "_source": {
          "name": "John",
          "age": 30,
          "email": "john@example.com",
          "date_of_birth": "1990-01-01",
          "address": {
            "street": "123 Main St",
            "city": "New York",
            "country": "USA"
          }
        }
      }
    ]
  }
}

修改

// 修改
POST /test_es/_update/1
{
  "doc": {
    "age": 35
  }
}
{
  "_index": "test_es",
  "_id": "1",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 2,
  "_primary_term": 1
}

删除

// 删除
DELETE /test_es/_doc/1
{
  "_index": "test_es",
  "_id": "1",
  "_version": 3,
  "result": "deleted",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 3,
  "_primary_term": 1
}

results matching ""

    No results matching ""