mapping 操作
指定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 //禁止被检索
}
}
}
复制代码
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 属性
dynamic 可以分为动态映射(dynamic mapping)和静态(显式)映射(explicit mapping)和精确(严格)映射(strict mappings),具体由dynamic属性控制。
-
动态映射(dynamic:true) 自动创建自动索引
-
静态映射(dynamic:false)可以存进去 但不能检索
-
严格模式(dynamic:false)如果遇到新的字段,就抛出异常
聚合统计分析
GET /test_users/_search
{
"size": 0,
"aggs": {
"age_agg":{
"terms": {
"field": "age",
"size": 10
},
"aggs": {
"constom_a":{
"sum": {
"field": "age"
}
}
}
}
}
}
复制代码
上面首先根据age进行分组, 在组内对age进行求和.
分词检测
检测分词情况
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%"
}
}
}
复制代码