本文操作基于 elasticsearch7.6.1 测试通过
指定mapping, 创建一个index
PUT /test_user2
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"name":{
"type":"keyword"
}
}
}
}
我们系统运行一段时间后, 想增加一个地址的字段, 如何手动指定类型呢?
在已有index mapping中添加新类型
PUT /test_user2/_mapping
{
"properties": {
"address":{
"type":"text",
"index": false //禁止被检索
}
}
}
//设置模版-类型推断为int
PUT /_template/template_1
{
"index_patterns": ["user*"],
"order":1,
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
},
"mappings": {
"numeric_detection": true
}
}
//获取模版信息
GET /_template/template_1
在mapping中我们还可以设置 dynamic 属性
dynamic 可以分为动态映射(dynamic mapping)和静态(显式)映射(explicit mapping)和精确(严格)映射(strict mappings),具体由dynamic属性控制。
检测分词情况
GET /_analyze
{
"analyzer": "standard",
"text":"Waiting to Exhale (1995)"
}
//查看test_users索引的name字段怎样分词, 默认为standard
GET /test_users/_analyze
{
"field":"name",
"text":"hello world 中国"
}
//创建一个document, 如果存在则更新(不推荐)
POST /test_users/create/1
{
"name":"xiaoyu",
"age": 22,
"address":"河北保定"
}
//只能新增, 否则报错
POST /test_users/_doc/3?op_type=create
{
"name":"wansan",
"age": 33,
"address":"河北保定"
}
//创建或更新(推荐)
PUT /test_users/_doc/2
{
"name":"linlin",
"age": 22,
"address":"河北保定"
}
//更新-覆盖掉旧数据
PUT /test_users/_doc/2
{
"sex":"女"
}
//将sex加入到原文档中(只更新指定的字段)
POST /test_users/_update/2
{
"doc":{
"sex":"女"
}
}
//查询字段中包含关键词的数据
GET /test_users/_search?q=河北
//查询_id=1的数据
GET test_users/_doc/1
模糊查询Waiting 和 Exhale
GET xiaoyu_movie/_search
{
"query": {
"match": {
"column2": "Waiting Exhale",
}
}
}
但是这不是我们想要的结果, 我们想同时包含这两个单词
当然你会想到拆分多条件查询
GET xiaoyu_movie/_search
{
"query": {
"bool": {
"must": [
{
"match":{
"column2": "Waiting"
}
},
{
"match":{
"column2": "Exhale"
}
}
]
}
}
}
这里我们使用一种更简便的方式
GET xiaoyu_movie/_search
{
"profile": "true",
"explain": true, //开启查询分析
"query": {
"match": {
"column2":{
"query": "Waiting Exhale",
"operator": "AND"
}
}
}
}
或者使用
GET xiaoyu_movie/_search
{
"profile": "true",
"explain": true,
"query": {
"match": {
"column2":{
"query": "Waiting Exhale",
"minimum_should_match": 2
}
}
}
}
优化 (使用 constant_score filter 屏蔽评分):
GET xiaoyu_movie/_search
{
"query": {
"constant_score": {
"filter": {
"match": {
"column2": {
"query": "Waiting Exhale",
"minimum_should_match": 2
}
}
}
}
}
}
如果想要提升某个子句的排序权重, 可以设置 boost
GET /xiaoyu_movie/_search
{
"query": {
"match": {
"column2":{
"query": "a",
"boost": 2
}
}
}
}
如果想对某个条件提升或降低权重, 可以使用boost, 默认为1.
GET /xiaoyu_movie/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"column1": {
"value": "2376"
}
}
},
{
"term": {
"column1": {
"value": "1"
}
}
}
],
"minimum_should_match": 1 //默认为1,表示最少满足一个条件
}
}
}
多字段相同查询, 匹配度20%
GET /xiaoyu_movie/_search
{
"query": {
"multi_match": {
"query": "a b c",
"fields": ["column1","column2"],
"minimum_should_match": "20%"
}
}
}
我们可以对 index 起别名, 这样我们以后 reindex, 可以无缝切换了, alials还可以加过滤条件.
POST /_aliases
{
"actions": [
{
"add": {
"index": "xiaoyu_movie",
"alias": "alias_yu"
}
}
]
}
//使用别名查询
GET /alias_yu/_search
查找地址中含有河北的数据, 按照age从大到小排序,如果有多段查询可以使用 factor 参数增强某查询语句的影响力
GET /test_score/_search
{
"query": {
"function_score": {
"query": {
"match": {
"address": "河北"
}
},
"field_value_factor": {
"field": "age"
},
"boost_mode":"sum", //将算分累加起来, 此处相当于 age+其他条件分数
"max_boost": 10 //相当于最大age分数为0
}
}
}
GET /test_score/_search
{
"query": {
"function_score": {
"random_score": {
"field":"uid",
"seed": 2213132
}
}
}
}
故意输错单词, 会给出推测提示, 但是中文目前无效果
GET /xiaoyu_movie/_search
{
"suggest": {
"address_text": {
"text": "Storyi",
"term": {
"field": "column2"
}
}
}
}
首先创建mapping
PUT /test_user
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"name":{
"type":"keyword"
},
"age":{
"type": "integer"
},
"address":{
"type":"completion"
}
}
}
}
设置type为 completion
检索
GET /test_user/_search
{
"suggest": {
"complate_test": {
"prefix": "河北",
"completion":{
"field":"address"
}
}
}
}
from size对于深分页来说是有局限性的, 这里我们使用其他方式
首先排序, 使用 search_after 指定每次顺序
GET /xiaoyu_movie/_search
{
"query": {
"match_all": {}
},
"search_after":[
100194
],
"sort": [
{
"column1": {
"order": "asc"
}
}
]
}
GET /test_users/_search
{
"size": 0,
"aggs": {
"age_agg":{
"terms": {
"field": "age",
"size": 10
},
"aggs": {
"constom_a":{
"sum": {
"field": "age"
}
}
}
}
}
}
上面首先根据age进行分组(分桶), 在组内对age进行求和.
GET /test_users/_search
{
"size": 0,
"aggs": {
"max_age":{
"max": {
"field": "age"
}
},
"min_age":{
"min": {
"field": "age"
}
},
"avg_age":{
"avg": {
"field": "age"
}
}
}
}
统计年龄的最大值 最小值和平均值
GET /test_users/_search
{
"size": 0,
"aggs": {
"age_agg": {
"terms": {
"field": "age"
},
"aggs": {
"constom_a": {
"top_hits": {
"size": 1,
"sort": [
{
"grade": {
"order": "desc"
}
}
]
}
}
}
}
}
}
对数据进行聚合, 然后每个分类按年级倒序取一条
区间统计查询
GET /test_users/_search
{
"size": 0,
"aggs": {
"age_range":{
"range": {
"field": "age",
"ranges": [
{
"key": "<=22",
"to": 22
},
{
"key":"range 2",
"from": 23,
"to": 60
},
{
"key":">60",
"from":61
}
]
}
}
}
特定条件下聚合(对age<=15的数据进行聚合)
GET /test_users/_search
{
"size": 0,
"query": {
"range": {
"age": {
"lte": 15
}
}
},
"aggs": {
"age":{
"terms": {
"field": "age"
}
}
}
}
当数据分散在多个分片时, 有可能聚合结果是不准确的, 我们可以使用 shard_size 指定分片数返回, 但是可能对性能又有较大压力.
依赖数据库版本机制, 只能比version版本大才能继续更新
PUT /xiaoyu_movie/_doc/XwNiNXEBn2UI19J8-2xH
?version=4001&version_type=external
{
"_doc":{
"column1":"text_a"
}
}
创建 index
PUT /test_user
{
"mappings": {
"properties": {
"name":{
"type":"keyword"
},
"address":{
"properties": {
"city":{
"type":"text"
}
}
}
}
}
}
创建数据
POST /test_user/_doc/1
{
"name":"xiaoyu",
"address": {
"city":"北京"
}
}
GET /test_user/_search
{
"query": {
"term": {
"address.city": {
"value": "北京"
}
}
}
}
通过上面term精确检索失败,mapping一经创建无法更改, 这里需要重建 index
PUT /test_user2
{
"mappings": {
"properties": {
"name":{
"type":"keyword"
},
"address":{
"properties": {
"city":{
"type":"keyword"
}
}
}
}
}
}
更改mapping 执行数据同步
POST _reindex
{
"source": {
"index": "test_user"
},
"dest": {
"index": "test_user2"
}
}