ElasticSearch restful语法

ElasticSearch基础学习

1. Elasticsearch 的基本概念

  • Index(索引):相当于数据库中的表,存储一类文档。
  • Document(文档):索引中的一条记录,使用 JSON 格式表示。
  • Type(类型):文档的分类,Elasticsearch 7.x 之后已不再推荐使用。
  • Field(字段):文档中的键值对。

2. 常用的 CRUD 操作

2.1 创建索引

PUT /my_index
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  },
  "mappings": {
    "properties": {
      "title": { "type": "text" },
      "date": { "type": "date" },
      "content": { "type": "text" }
    }
  }
}

注意:一旦创建索引库,对应的mapping结构不能修改,但是可以添加新的字段,以下是添加新字段操作:

PUT /索引库名/_mapping
{
  "properties": {
    "新字段名":{
      "type": ""
    }
  }
}

每个字段的子属性有以下几种:

type:字段数据类型,常见的简单类型有:
字符串:text(可分词的文本)、keyword(精确值,例如:品牌、国家、ip地址)
数值:long、integer、short、byte、double、float
布尔:boolean
日期:date
对象:object
index:是否创建索引(即倒排索引),默认为true(可参与搜索)
analyzer:使用哪种分词器(跟text类型结合使用)
properties:该字段的子字段

2.2 添加文档

POST /my_index/_doc/1
{
  "title": "My First Document",
  "date": "2023-10-01",
  "content": "This is the content of the document."
}

2.3 获取文档

GET /索引名/_doc/文档id

2.4 删除文档

DELETE /索引名/_doc/文档id

2.5 更新文档

更新文档有两种,全量修改和增量修改

  1. 全量修改,是将原来的文档删除,然后增加一个相同id的文档,写法与添加文档相同
  2. 增量修改。如下,这里的更新字段只会匹配原来文档中已存在的字段。
POST /索引名/_doc/索引id/_update
{
  "doc": {
  	"更新字段": "更新内容"
  }
}

3. 查询文档

term&terms查询

termterms 查询用于查找与指定值完全匹配的文档,精确匹配,通常用于非分析字段(如 keyword 类型或数值类型)。

POST /索引库/_search
{
  "query": {
    "term": {
      "field_name": "value"
    }
  }
}

terms 查询允许你指定多个值,匹配字段中的任何一个值。它用于查找包含任意指定值之一的文档。

POST /索引库/_search
{
  "query": {
    "terms": {
      "field_name": ["value1", "value2", "value3"]
    }
  }
}

match查询

match本质是多个term查询的上层封装。match会识别字段类型,如果是可以分词的("text")就采用分词方式查询,如果是不分词("keyword"或数值类型)就采用精确匹配。

查询所有

POST /test/_search
{
  "query": {
    "match_all": {}
  }
}

对于普通的match,match中字段常用的属性。

"query": 需要匹配的内容,
"operator": 是OR匹配还是AND匹配
"fuzziness": "AUTO" 模糊查询,允许一定的拼写错误
POST /test/_search
{
  "query": {
    "match": {
      "field": "Second content"
    }
  }
}

上面的是默认写法,如果不做其他要

multi_match

多个字段匹配同一内容

POST /test/_search
{
  "query": {
    "multi_match": {
      "query": "content",
      "fields": [field1,field2]
    }
  }
}

其他查询

prefix查询

fuzzy模糊查询,

wildcard通配查询(相当于Mysql的like查询,可以加通配符),

range范围查询,只针对数值类型

regexp正则表达式查询

深分页

ES对用from+size这种方法的查询是有限制的,两者之和不能超过10000,所以要使用Scroll。

  • from + size: 适合小数据集的简单分页。
  • Scroll: 适合大数据集的批量处理,且性能更优。

复合查询

将多个条件组合起来

must : 在里面的条件都要满足,相当于and

must_not: 在里面的条件都不要满足,相当于not

should:在里面的条件满足其中一个,相当于or

# 班级为1,2,3中总分为100分的且家乡在北京的人
POST /test/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "class":1
        },
        {
          "class":2
        },
        {
          "class":3
        }
      ],
      "must": [
        {
          "total_score" : 100
        },
        {
          "city": "beijing"
        }
      ]
    }
  }
}

高亮查询

POST /test/_search
{
  "query": {
    "match": {
      "content": "content"
    }
  },
  "highlight": {
    "fields": {
      "content": {}
    },
    "pre_tags": "<font color='red'>", 
    "post_tags": "</font>"
  }
}
posted @ 2024-11-16 09:11  Liang2003  阅读(43)  评论(0)    收藏  举报