Elasticsearch(六)Elasticsearch索引操作

78 阅读6分钟

这里我们继续来学习Elasticsearch索引操作,我这里基本上就是按照官方文档来做测试。

 

官方文档地址:

www.elastic.co/guide/en/el…

 

1:创建索引以及配置分词器

PUT /test123
{
  "settings": {
    "analysis": {
      "analyzer": {
        "ik_analyzer": {
          "type": "custom",
          "tokenizer""ik_max_word",
          "filter": ["synonym_filter""stop_filter"]
        }
      },
      "filter": {
        "synonym_filter": {
          "type": "synonym",
          "synonyms_path""analysis/synonyms.txt"
        },
        "stop_filter": {
          "type": "stop",
          "stopwords_path""analysis/stop.txt"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "content": {
        "type": "text",
        "analyzer""ik_analyzer"
      },
      "arttitle": {
        "type": "text",
        "analyzer""ik_analyzer"
      },
      "artdesc": {
        "type": "text",
        "analyzer""ik_analyzer"
      }
    }
  }
}

我在前文中是安装了IK分词器的插件的,因此我上边创建索引的时候,直接将IK分词器配置进去了,如果你没有安装IK分词器,建议你安装一下,具体请移步《Elasticsearch(三)安装分词器

 

2:字段数据类型(这个玩意儿后期要废弃)

1)   字符串类型

a)   text、keyword

2)   text:支持分词,全文检索,支持模糊、精确查询,不支持聚合,排序操作;text类型的最大支持的字符长度无限制,适合大字段存储;

3)   keyword:不进行分词,直接索引、支持模糊、支持精确匹配,支持聚合、排序操作。keyword类型的最大支持的长度为——32766个UTF-8类型的字符,可以通过设置ignore_above指定自持字符长度,超过给定长度后的数据将不被索引,无法通过term精确匹配检索返回结果。

4)   数值型

a)   long、Integer、short、byte、double、float、half float、scaled float

5)   日期类型

a)   date

6)   布尔类型

a)   boolean

7)   二进制类型

a)   binary

 

3:指定字段的类型(使用PUT)

类似于建库(建立索引和字段对应类型),也可看做规则的建立

这个我在创建索引部分已经做过了。

"mappings": {
    "properties": {
      "content": {
        "type": "text",
        "analyzer""ik_analyzer"
      },
      "arttitle": {
        "type": "text",
        "analyzer""ik_analyzer"
      },
      "artdesc": {
        "type": "text",
        "analyzer""ik_analyzer"
      }
    }
  }

**
**

4:获取我们创建的规则

GET /test123

如下图所示:

1.jpg

 

5:添加数据

_doc 默认类型(default type),type 在未来的版本中会逐渐弃用,因此产生一个默认类型进行代替

PUT /test123/_doc/1
{
  "content""流柚",
  "arttitle": 18,
  "artdesc""1999-10-10"
}

2.jpg

如果自己的文档字段没有被指定,那么ElasticSearch就会给我们默认配置字段类型

 

6:获取上边添加的数据

GET /test123/_doc/1

3.jpg

 

7:修改数据

修改数据有两种方式,一种是直接添加(同一个id),另一种是直接修改。

(1):直接添加修改

版本+1(_version)

但是如果漏掉某个字段没有写,那么更新是没有写的字段 ,会消失

PUT /test123/_doc/1
{
  "name" : "流柚"
}

获取刚修改的数据

GET /test123/_doc/1

4.jpg

 

(2):使用post的update

version不会改变

需要注意doc

不会丢失字段

POST /test123/_doc/1/_update
{
  "doc":{
    "name" : "post修改,version不会加一",
    "age" : 2
  }
}

获取刚修改的数据

GET /test123/_doc/1

5.jpg

 

8:删除

这个就很简单了。

DELETE /test1

 

9:简单查询

命令格式

GET /test123/_doc/_search?q=name:post

查询结果:

6.jpg

 

10:复杂查询

(1):查询匹配

²  match:匹配(会使用分词器解析(先分析文档,然后进行查询))

²  _source:过滤字段

²  sort:排序

²  form、size 分页

// 查询匹配
GET /test123/_doc/_search
{
  // 匹配
  "query":{
    "match":{
      "name":"post"
    }
  }
  // 过滤字段
  ,
  "_source": ["name","age"]
  ,
  // 排序
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ]
  ,
  // 从第几条开始取
  "from": 0
  ,
  // 取几条
  "size": 1
}

结果如下图所示:

7.jpg

 

(2):多条件查询(bool)

must 相当于 and

should 相当于 or

must_not 相当于 not (... and ...)

filter 过滤

/// bool 多条件查询
//// must <==> and
//// should <==> or
//// must_not <==> not (... and ...)
//// filter数据过滤
//// boost
//// minimum_should_match
GET /test123/_doc/_search
{
  "query":{
    "bool": {
      // 相当于and条件
      "must": [
        {
          "match":{
            "age":2
          }
        },
        {
          "match": {
            "name""post"
          }
        }
      ],
      // 过滤条件,range按范围
      "filter": {
        "range": {
          "age": {
            "gte"1,
            "lte"3
          }
        }
      }
    }
  }
}

查询结果如下图所示:

8.jpg

 

(3):匹配数组

貌似不能与其它字段一起使用

可以多关键字查(空格隔开)— 匹配字段也是符合的

match 会使用分词器解析(先分析文档,然后进行查询)

// 匹配数组 貌似不能与其它字段一起使用
// 可以多关键字查(空格隔开)
// match 会使用分词器解析(先分析文档,然后进行查询)
GET /test123/_doc/_search
{
  "query":{
    // match会使用分词器将 “年龄 牛 大” 分拆为 “年” “龄” “牛” “大”
    "match":{
      "name":"年龄 牛 大"
    }
  }
}

结果如下图所示:

9.jpg

 

(4):精确查询

term 直接通过 倒排索引 指定词条查询

适合查询 number、date、keyword ,不适合text

// 精确查询(必须全部都有,而且不可分,即按一个完整的词查询)
// term 直接通过 倒排索引 指定的词条 进行精确查找的
GET /test123/_doc/_search
{
  "query":{
    // term使用倒H排索引查的,所以他会把"流"看成一个整体,从表中查文档然而没有符号的,如果换成"流”就满足,可以查询出数据
    "term":{
      "name":""
    }
  }
}

搜索结果如下图所示:

10.jpg

 

(5):text和keyword

**1)  ** text

a)    支持分词,全文检索、支持模糊、精确查询,不支持聚合,排序操作;

b)    text类型的最大支持的字符长度无限制,适合大字段存储;

**2)  ** keyword

²  不进行分词,直接索引、支持模糊、支持精确匹配,支持聚合、排序操作。

²  keyword类型的最大支持的长度为——32766个UTF-8类型的字符,可以通过设置ignore_above指定自持字符长度,超过给定长度后的数据将不被索引,无法通过term精确匹配检索返回结果。

// 测试keyword和text是否支持分词
// 设置索引类型
PUT /test
{
  "mappings": {
    "properties": {
      "text":{
        "type":"text"
      },
      "keyword":{
        "type":"keyword"
      }
    }
  }
}
// 设置字段数据
PUT /test/_doc/1
{
  "text":"测试keyword和text是否支持分词",
  "keyword":"测试keyword和text是否支持分词"
}
// text 支持分词
// keyword 不支持分词
GET /test/_doc/_search
{
  "query":{
   "match":{
      "text":"测试"
   }
  }
}// 查的到
GET /test/_doc/_search
{
  "query":{
   "match":{
      "keyword":"测试"
   }
  }
}// 查不到,必须是 "测试keyword和text是否支持分词" 才能查到
GET _analyze
{
  "analyzer": "keyword",
  "text": ["测试liu"]
}// 不会分词,即 测试liu
GET _analyze
{
  "analyzer": "standard",
  "text": ["测试liu"]
}// 分为 测 试 liu
GET _analyze
{
  "analyzer":"ik_max_word",
  "text": ["测试liu"]
}// 分为 测试 liu

 

(6):高亮查询

// 高亮且自定义前缀和后缀
GET test123/_doc/_search
{
  "query": {
    "match": {
      "name":"流"
    }
  }
  ,
  "highlight": {
    "pre_tags": "<p style='color:red'>",
    "post_tags": "</p>",
    "fields": {
      "name": {}
    }
  }
}

查询结果如下所示:

11.jpg

 

以上大概就是elasticsearch对索引以及文档的基本操作。

 

有好的建议,请在下方输入你的评论。