命令全部在kibana下创建
非结构化创建
PUT创建索引
PUT /user
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
}
}
PUT /user/_doc/1
{
"name":"唐伯虎",
"age": 30
}
create创建
PUT /user/_create/2
{
"name":"秋香",
"age": 30
}
更新
全量更新
PUT /user/_doc/2
{
"name":"小龙女"
}
局部更新
POST /user/_update/2
{
"doc": {
"name":"黄容"
}
}
条件查询更新
POST /search_hy_userread/_update_by_query
{
"query": {
"term": {
"chnlid": 6483
}
},
"script": {
"inline": "ctx._source['status'] = '10'"
}
}
POST请求/索引/文档名/_update_by_query
主要看一下下面的script
ctx._source[字段名] = “值”;ctx._source[字段名] = “值”;
删除
删除索引
DELETE /user
删除ID
DELETE /user/文档ID
过滤删除
POST smileyan/_delete_by_query
{
"query": {
"match": {
"num": "39"
}
}
}
查询
查询指定ID数据
GET /user/_doc/1
查询所有数据
GET /user/_search
分页查询
GET /user/_search
{
"query": {
"match_all": {}
}
, "from": 0
, "size": 20
}
条件查询
GET /user/_search
{
"query": {
"match": {
"name": "龙"
}
}
排序查询
GET /user/_search
{
"query": {
"match": {
"name": "唐"
}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
filter过滤查询
GET /user/_search
{
"query": {
"bool": {
"filter": [
{"term":{"age":30}}
]
}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
match 会匹配包含该字符串的所有记录
term 只匹配等于该字符串的所有记录
分组查询
GET /user/_search
{
"aggs": {
"group_by_age": {
"terms": {
"field": "age"
}
}
}
}
模糊查询
GET /user/_search
{
"query": {
"wildcard": {
"name": { #要查询的字段
"value": "*龙*" #通配符
}
}
}
}
不匹配查询
GET search_hy_gk/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"siteid": "4"
}
}
]
}
}
}
and查询
GET search_hy_hd/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"siteid": 4
}
},
{
"term": {
"chnldesc": "市长信箱"
}
}
]
}
}
}
结构化创建索引
创建结构
PUT /user
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
}
, "mappings": {
"properties": {
"name":{
"type": "text"
},
"age":{
"type": "integer"
}
}
}
}
添加数据
PUT /user/_doc/1
{
"name":"唐伯虎",
"age": 30
}
分词查看
PUT /movie/_doc/1
{
"name":"Eating an apple a day & keep the doctor away"
}
GET /movie/_analyze
{
"field": "name",
"text":"Eating an apple a day & keep the doctor away"
}
自定义打分
GET /movie/_search
{
"explain": true,
"query": {
"function_score": {
"query": {
"multi_match": {
"query": "steve job",
"fields": ["title","overview"],
"operator": "or",
"type": "most_fields"
}
},
"functions":[
{
"field_value_factor":{
"field":"popularity",
"modifier":"log2p",
"factor":10
}
},
{
"field_value_factor":{
"field":"popularity",
"modifier":"log2p",
"factor":10
}
}
],
"score_mode": "sum",
"boost_mode": "sum"
}
}
}
聚合查询
外层size为源数据条数
内层size为聚合的数据条数
belongCity为自己的字段名
GET search_dev_item/_search
{
"size":0,
"aggs": {
"group_by_tags": {
"terms": {
"field": "belongCity"
, "size": 20
}
}
}
}