elasticsearch 常见语法操做

本文操做基于 elasticsearch7.6.1 测试经过数据库

mapping 操做

指定mapping, 建立一个indexbash

PUT /test_user2
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  }, 
  "mappings": {
    "properties": {
      "name":{
        "type":"keyword"
      }
    }
  }
}
复制代码

咱们系统运行一段时间后, 想增长一个地址的字段, 如何手动指定类型呢?markdown

在已有index mapping中添加新类型并发

PUT /test_user2/_mapping
{
  "properties": {
      "address":{
        "type":"text",
        "index": false  //禁止被检索
      }
    }
}
复制代码

index 模版建立

//设置模版-类型推断为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 属性

在mapping中咱们还能够设置 dynamic 属性app

dynamic 能够分为动态映射(dynamic mapping)和静态(显式)映射(explicit mapping)和精确(严格)映射(strict mappings),具体由dynamic属性控制。dom

  • 动态映射(dynamic:true) 自动建立自动索引elasticsearch

  • 静态映射(dynamic:false)能够存进去 但不能检索性能

  • 严格模式(dynamic:false)若是遇到新的字段,就抛出异常测试

分词检测

检测分词状况
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":"女"
  }
}
复制代码

查询

url 模糊查询

//查询字段中包含关键词的数据
GET /test_users/_search?q=河北

//查询_id=1的数据
GET test_users/_doc/1
复制代码

dsl方式查询

模糊查询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.

OR 查询

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
    }
  }
}
复制代码

随机查找(seed变化结果变化)

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"
       }
     }
   }
}
复制代码

设置es自动补全功能

首先建立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"
          }
        }
      }
    }
  }
}
复制代码

执行同步

POST _reindex
{
  "source": {
    "index": "test_user"
  },
  "dest": {
    "index": "test_user2"
  }
}
复制代码
相关文章
相关标签/搜索