ElasticSearch 创建索引、插入数据、修改数据、删除数据

创建索引

PUT demoindex

返回

{
  "demoindex" : {
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "demoindex",
        "creation_date" : "1612333489325",
        "number_of_replicas" : "1",
        "uuid" : "0L-lMc6CQR2ft-jDlO670A",
        "version" : {
          "created" : "7100299"
        }
      }
    }
  }
}

number_of_shards:分片数,创建后不能修改

number_of_replicas:副本数/备份数

创建索引,并指定分片参数

PUT /demoindex/
{
  "settings":{
    "index":{
      "number_of_shards" : "5",
      "number_of_replicas" : "1"
    }
  }
}

返回

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "demoindex2"
}

查看索引配置

GET demoindex2/_settings

返回

{
  "demoindex2" : {
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "5",
        "provided_name" : "demoindex2",
        "creation_date" : "1612334114521",
        "number_of_replicas" : "1",
        "uuid" : "75BT_fx5Rhy3e0uf_zkayw",
        "version" : {
          "created" : "7100299"
        }
      }
    }
  }
}

删除索引

DELETE demoindex2

插入数据

PUT demoindex/_create/1
{
  "userName": "",
  "age": 22
}

返回

{
  "_index" : "demoindex",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

更新数据

PUT demoindex/_doc/1
{
  "userName": "laowang",
  "age": 22
}

删除

DELETE demoindex/_doc/1

查询

GET demoindex/_doc/_search

GET demoindex/_doc/1

返回高亮代码的查询

PUT demoindex/_doc/3
{
  "userName": "laowang",
  "age": 22,
  "content": "中国人民解放军,美国美利坚合众国武装部队,远哥"
}

PUT demoindex/_doc/4
{
  "userName": "laowang",
  "age": 22,
  "content": "中国人民解放军,美国美利坚合众国武装部队,远哥,中国人民站起来了"
}

GET demoindex/_doc/_search
{
    "query" : { "match" : { "content" : "中国" }},
    "highlight" : {
        "pre_tags" : ["<font color='red'>"],
        "post_tags" : ["</font>"],
        "fields" : {
            "content" : {}
        }
    }
}

返回结果

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.23802689,
    "hits" : [
      {
        "_index" : "demoindex",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 0.23802689,
        "_source" : {
          "userName" : "laowang",
          "age" : 22,
          "content" : "中国人民解放军,美国美利坚合众国武装部队,远哥,中国人民站起来了"
        },
        "highlight" : {
          "content" : [
            "<font color='red'>中国</font>人民解放军,美国美利坚合众国武装部队,远哥,<font color='red'>中国</font>人民站起来了"
          ]
        }
      },
      {
        "_index" : "demoindex",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.1976162,
        "_source" : {
          "userName" : "laowang",
          "age" : 22,
          "content" : "中国人民解放军,美国美利坚合众国武装部队,远哥"
        },
        "highlight" : {
          "content" : [
            "<font color='red'>中国</font>人民解放军,美国美利坚合众国武装部队,远哥"
          ]
        }
      }
    ]
  }
}

分页查询、过滤返回的字段属性

GET demoindex/_doc/_search
{
    "query" : { "match" : { "content" : "中国" }},
    "sort": [{
        "updateTime": "asc"
    }],
    "highlight" : {
        "pre_tags" : ["<font color='red'>"],
        "post_tags" : ["</font>"],
        "fields" : {
            "content" : {}
        }
    },
    "_source": {
        "includes": [ "title","author","docType","fileDir","filePath","fileSHA1" ],
        "excludes": [ "content" ]
    },
    "size": 10,
    "from": 0
}

安装ik分词插件 elasticsearch-analysis-ik

1、解压 elasticsearch-analysis-ik-7.10.2.zip

2、拷贝解压出来的所有文件放到 elasticsearch-7.10.2\plugins\ik 目录下(首次需要创建一个 ik 目录)

3、重启启动 elasticsearch

创建Ik分词的mapping

POST demoindex/_mapping
{
    "properties":{
        "content":{
            "type":"text",
            "analyzer":"ik_max_word",
            "search_analyzer":"ik_smart"
        }
    }
}

PUT demoindex
{
    "mappings":{
        "properties":{
            "userName":{
                "type":"keyword"
            },
            "age":{
                "type":"long"
            },
            "content": {
                "type": "text",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_smart"
            }
        }
    }
}

分词粒度分为:

ik_smart 和 ik_max_word

数据类型说明:

字符串类型

text 、 keyword

数值类型

long, integer, short, byte, double, float, half_float, scaled_float

日期类型

date

te布尔值类型

boolean

二进制类型

binary

范围类型

integer_range , float_range, long_range, double_range, date_range

评论

暂无评论