其他分享
首页 > 其他分享> > Elasticsearch基本操作

Elasticsearch基本操作

作者:互联网

索引操作

创建索引

PUT /user

查询指定索引

GET /user

查询所有索引信息

GET _cat/indices?v

删除索引

DELETE /user

类型映射

自动映射

elasticsearch默认情况下会自动推断数据类型

PUT /user/_doc/1
{
	"userId": 1,
	"name": "zhang san",
	"age": 20,
	"birthday": "2022-08-20"
}

以上语句的作用,如果索引user不存在,会先创建索引user,然后新增一条id=1的数据。

查询下索引信息,GET /user

{
  "user" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "long"
        },
        "birthday" : {
          "type" : "date"
        },
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "userId" : {
          "type" : "long"
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "user",
        "creation_date" : "1660984341229",
        "number_of_replicas" : "1",
        "uuid" : "yiq_xYzqQPGT7kfcz_TSFw",
        "version" : {
          "created" : "7170599"
        }
      }
    }
  }
}

会看到elasticsearch自动推断出数据字段类型

字段 类型
id long
name text/keyword
age long
birthday date

指定映射

使用如下语句便可以在创建索引时指定字段以及类型

PUT /user
{
  "mappings": {
    "properties": {
      "userId": {
        "type": "integer",
        "index": true
      },
      "name": {
        "type": "text",
        "index": true
      },
      "age": {
        "type": "integer",
        "index": true
      },
      "birthday": {
        "type": "date",
        "index": true
      }
    }
  }
}

其中: "index": true表示该列可以作为查询条件,可以省略,默认为true

查询映射

GET /user/_mapping

查询指定字段映射

GET /user/_mapping/field/userId

增加字段映射

PUT /user/_mapping
{
  "properties": {
    "createTime": {
      "type": "date",
      "index": false,
      "format": "yyyy-MM-dd HH:mm:ss"
    }
  }
}

增加了一个创建时间

改变字段映射类型

比如想把createTime改成long类型,目前不支持此种操作,因为可能会使得已有数据无效

文档基本操作

新增文档

指定id方式(PUT)

PUT /user/_doc/1
{
	"userId": 1,
	"name": "zhang san",
	"age": 20,
	"birthday": "2022-08-20",
	"createTime": "2022-08-20 12:10:30"
}

随机ID(POST),当然了POST方式也可以指定ID

POST /user/_doc
{
	"userId": 1,
	"name": "zhang san",
	"age": 20,
	"birthday": "2022-08-20",
	"createTime": "2022-08-20 12:10:30"
}

修改文档

原样修改

PUT /user/_doc/1
{
	"userId": 1,
	"name": "zhang san",
	"age": 25,
	"birthday": "1997-08-20"
}

或者

POST /user/_doc/1
{
	"userId": 1,
	"name": "zhang san",
	"age": 25,
	"birthday": "1997-08-20"
}

查询结果(createTime会丢失)

{
	"userId": 1,
	"name": "zhang san",
	"age": 25,
	"birthday": "1997-08-20"
}

修改指定字段

POST /user/_update/1
{
  "doc": {
    "age": 25
  }
}

或者

POST /user/_doc/1/_update
{
  "doc": {
    "age": 25
  }
}

查看文档

GET /user/_doc/1

删除文档

DELETE /user/_doc/1

批量操作

使用_bulk API

包含四种操作,可以同时存在

下面对user索引批量新增文档,注意,除了delete不需要紧跟数据,其它操作都需要接一条数据,无论是操作还是数据,都是一行完成。

POST /user/_bulk
{"index": {"_id": 1}}
{"userId": 1, "name" : "zhang san", "age" : 20, "birthday" : "2021-08-20", "createTime" : "2022-08-20 12:10:30"}
{"index": {"_id": 2}}
{"userId": 2, "name" : "li si", "age" : 20, "birthday" : "2022-08-20", "createTime" : "2022-08-20 12:10:30"}
{"index": {"_id": 3}}
{"userId": 3, "name" : "zhang san shuo", "age" : 25, "birthday" : "2022-08-20", "createTime" : "2022-08-20 12:10:30"}
{"index": {"_id": 4}}
{"userId": 4, "name" : "wang wu", "age" : 20, "birthday" : "2021-08-20", "createTime" : "2022-08-20 12:10:30"}

文档搜索

查找所有

GET /user/_search

或者

GET /user/_search
{
    "query": {
        "match_all": {}
     }
}

简单查询

注意,对于类型为text的列,会进行分词

假设现在有以下数据

[
    {
          "userId" : 1,
          "name" : "zhang san",
          "age" : 20,
          "birthday" : "2022-08-20",
          "createTime" : "2022-08-20 12:10:30"
     },
    {
          "userId" : 2,
          "name" : "li si",
          "age" : 20,
          "birthday" : "2022-08-20",
          "createTime" : "2022-08-20 12:10:30"
     }
]

现在根据名称查询

GET /user/_search
{
    "query": {
        "match": {
          "name": "zhang"
        }
     }
}

会找到zhang san那条记录

GET /user/_search
{
    "query": {
        "match": {
          "name": "zhang li"
        }
     }
}

会找到zhang sanli si两条记录,因为搜索条件会被分词为zhangli两个词

如果想要搜索条件作为整体出现,可以使用match_phrase

如以下例子

GET /user/_search
{
    "query": {
        "match_phrase": {
          "name": "zhang li"
        }
     }
}

该命令将不会查到任何结果,因为没有任何记录的名字包含zhang li

继续增加一条数据,假设目前的结果如下

[
    {
          "userId" : 1,
          "name" : "zhang san",
          "age" : 20,
          "birthday" : "2022-08-20",
          "createTime" : "2022-08-20 12:10:30"
     },
    {
          "userId" : 2,
          "name" : "li si",
          "age" : 20,
          "birthday" : "2022-08-20",
          "createTime" : "2022-08-20 12:10:30"
     },
    {
          "userId" : 3,
          "name" : "zhang san shuo",
          "age" : 25,
          "birthday" : "2022-08-20",
          "createTime" : "2022-08-20 12:10:30"
     }
]

执行以下查询

GET /user/_search
{
    "query": {
        "match_phrase": {
          "name": "zhang san"
        }
     }
}

将会得到zhang san以及zhang san shuo两条记录,因为这两条记录的名字都包含zhang san

match_phrase匹配逻辑:首先也会将搜索条件进行分词,并且每个词都要命中记录,且分词的相对位置也要一样。比如搜索zhang shuo是不会匹配到zhang san shuo这条记录的,因为顺序不一致,中间还有一个san,虽然分词zhangshuo都能命中。

范围查询

GET /user/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 10,
        "lte": 20
      }
    }
  }
}

复杂查询

must = and,should = or

查找名字包含zhang san,年龄为25的记录

GET /user/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "name": "zhang san"
          }
        },
        {
           "match": {
            "age": 25
          }
        }
      ]
    }
  }
}

得到结果为name=zhang san shuo,age=25的记录

查找名字包含zhang san或者年龄为25的记录

GET /user/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase": {
            "name": "zhang san"
          }
        },
        {
           "match": {
            "age": 25
          }
        }
      ]
    }
  }
}

得到结果为name=zhang san shuo,age=25的记录以及name=zhang san,age=20的记录

查找(name like '%zhang san%' or age = 25) and birthday = '2022-08-20'

GET /user/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "birthday": "2022-08-20"
          }
        },
        {
          "bool": {
            "should": [
              {
                "match_phrase": {
                  "name": "zhang san"
                }
              },
              {
                "match": {
                  "age": 25
                }
              }
            ]
          }
        }
      ]
    }
  }
}

查找指定列

GET /user/_search
{
    "query": {
        "match_all": {}
     },
     "_source": ["name", "age"]
}

排序

GET /user/_search
{
    "query": {
        "match_all": {}
     },
     "sort": [
       {
         "age": {
           "order": "desc"
         },
         "userId": {
           "order": "asc"
         }
       }
     ]
}

分页

GET /user/_search
{
    "query": {
        "match_all": {}
     },
     "sort": [
       {
         "age": {
           "order": "desc"
         },
         "userId": {
           "order": "asc"
         }
       }
     ],
     "from": 0,
     "size": 2
}

注:

from: 默认为0
size: 默认为10,最大不能超过10000

标签:20,name,zhang,age,Elasticsearch,user,基本操作,08
来源: https://www.cnblogs.com/wt20/p/16610977.html