一些kinbna原生操作

es之settings和mappings设置

1、基本操作

查看索引的settings

GET /index_name/_settings

查看索引的mappings

GET /index_name/_mapping

新建索引mappings

PUT /index_name/_mapping
{
  "properties": {
      "content": {
      "type": "text",
      "term_vector": "with_positions_offsets",
      "similarity": "BM25",
      "analyzer": "english",
      "search_analyzer": "standard",
      "fielddata": true
    },
    "country": {
      "type": "keyword"
    },
    "emotion": {
      "type": "byte"
    },  
    "time": {
      "type": "date",
      "format": "yyyy-MM-dd HH:mm:ss"
    }
  }
  }

新索引settings和mappings

PUT /index_name
{
  "settings":{
    "number_of_shards":3,
    "number_of_replicas":2
  },
  "mappings":{
    "properties":{
      "id":{"type":"long"},
      "name":{"type":"text","analyzer":"ik_smart"},
      "text":{"type":"text","analyzer":"ik_max_word"}
    }
  }
}
GET /_cluster/settings

PUT /_cluster/settings
{"persistent": {"search.max_buckets": 1000000}}

2、设置动态模板

设置动态模板

PUT index_name
{
 "mappings": {
    "dynamic_templates": [
      {
        "content": {
          "match": "content",
          "mapping": {
          "type": "text",
          "analyzer": "standard",
          "search_analyzer": "english"
        }
      }
      },
      {
        "title": {
           "match": "tit*",
          "mapping": {
          "type": "text",
          "analyzer": "english"
        }
        }
      }
    ],
  "properties": {
    "content": {
      "type": "text",
      "term_vector": "with_positions_offsets",
      "similarity": "BM25",
      "analyzer": "standard",
      "fielddata": true
    },
    "summary": {
      "type": "text",
      "term_vector": "with_positions_offsets",
      "similarity": "BM25",
      "analyzer": "standard",
      "fielddata": true
    },
    "title": {
      "type": "text",
      "term_vector": "with_positions_offsets",
      "similarity": "BM25",
      "analyzer": "standard"
    }
  }
  }
}

3、scroll滚动查询

I、滚动scroll查询

# 查询media为“新闻报”的内容,查询200条,时间为1000ms
**GET index_name/_search?scroll=1000ms
{
  "query": {
    "match": {
      "media": "新闻报"
    }
  },
  "size": 200
}**

II、continue继续scroll查询(scorll查询后会返回一个scroll_id,可以通过scroll_id继续查询)

GET _search/scroll
{
  "scroll": "1000ms",
  "scroll_id":"FGluY2x1ZGVfY29udGV4dF91dWlkDnF1ZXJ5VGhlbkZldGNoBRY0bWVKNHJoelJLU3h4bE5FTFFEU3BnAAAAAAAOXp8WUHdKQnB6WGpSRzZndWZVaEppVFNnQRY0bWVKNHJoelJLU3h4bE5FTFFEU3BnAAAAAAAOXqEWUHdKQnB6WGpSRzZndWZVaEppVFNnQRY0bWVKNHJoelJLU3h4bE5FTFFEU3BnAAAAAAAOXqMWUHdKQnB6WGpSRzZndWZVaEppVFNnQRY0bWVKNHJoelJLU3h4bE5FTFFEU3BnAAAAAAAOXqAWUHdKQnB6WGpSRzZndWZVaEppVFNnQRY0bWVKNHJoelJLU3h4bE5FTFFEU3BnAAAAAAAOXqIWUHdKQnB6WGpSRzZndWZVaEppVFNnQQ="
}

4、分词查询

查看分词器解析效果

GET /index_name/_analyze
{
  "field": "title",
  "text": "The quick Brown Foxes."
}

5、聚合查询

分组查询

# index_name是查询的索引,emotionGroup是分组名称(可自行设置),field后是分组的字段
GET index_name/_search
{
  "aggs":{
    "emotionGroup":{
      "terms":{
        "field":"emotion"
      }
    }
 },
 "size": 0  #查询显示的文档条目数
}

分组后求和

GET index_name/_search
{
  "aggs":{
    "aggGroup":{ # 自己设置名称
      "terms":{
        "field":"emotion"
    },
    "aggs":{
      "emotionSum":{ # 自己设置名称
        "sum": {
          "field": "emotion"
          }
        }
      }
    }
 },
 "size": 0
}

求平均值

GET index_name/_search
{
  "aggs":{
    "emotionAvg":{ # 自己设置名称
      "avg":{
        "field":"emotion"
      }
    }
 },
 "size": 0  
}

获取前3名操作

GET index_name/_search
{
  "aggs":{
    "top3":{ # 自己设置名称
      "top_hit":{
        "size":3
      }
    }
 },
 "size": 0  
}

先排序,再获取前3名操作

GET index_name/_search
{
  "aggs":{
    "top3":{
      "top_hits":{
        "sort": [{
          "emotion":{
            "order": "desc"
          }
        }], 
        "size":3
      }
    }
 },
 "size": 0
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值