ES基础-基于Kibana增删改查

ElasticSearch增删改查

#创建索引

PUT shopping

#查看索引

GET shopping/_mapping

#产看所有信息

GET _cat/indices?v

#删除索引

DELETE employee

#创建文档添加数据

POST shopping/_doc   
{
  "title":"小米手机",
  "category":"小米",
  "images":"http://wwww.xiaomi.com",
  "price":119999
}

#创建文档添加数据 带id

POST shopping/_doc/1002
{
  "title":"小米手机",
  "category":"小米",
  "images":"http://wwww.xiaomi.com",
  "price":"1999"
}

#创建文档添加数据 带id

POST shopping/_create/1003  
{
  "title":"小米手机",
  "category":"小米",
  "images":"http://wwww.xiaomi.com",
  "price":"19999"
}

#创建文档添加数据 带id

PUT shopping/_create/1005   
{
  "title":"小米手机",
  "category":"华为",
  "images":"http://wwww.xiaomi.com",
  "price":"19999"
}

#全量数据更新

PUT shopping/_doc/1001   
{
  "title":"小米手机",
  "category":"小米",
  "images":"http://wwww.xiaomi.com",
  "price":"4999"
}

#局部数据更新

POST shopping/_update/1002
{
  "doc":{
    "title":"华为手机"
  }
}

#产看指定id信息

GET shopping/_doc/1002

#产看所有信息

GET shopping/_search

#条件查询

GET shopping/_search?q=category:小米

GET shopping/_search
{
  "query": {
    "match": {
      "category": "小米"
    }
  }
}

#不添加条件查询所有数据

GET shopping/_search
{
  "query": {
    "match_all": {}
  }
}

#分页查询 (页码-1 * 每页数据条数)

GET shopping/_search
{
  "query": {
    "match_all": {
    }
  },
  "from": 0,
  "size": 2
}

#分页查询查询指定字段

GET shopping/_search
{
  "query": {
    "match_all": {
      

    }

  },
  "from": 0,
  "size": 2,
  "_source": "title"
}

#对查询结果排序

GET shopping/_search
{
  "query": {
    "match_all": {
      

    }

  },
  "from": 0,
  "size": 2,
  "_source": "title",
  "sort": [
    {
      "category": {
        "order": "desc"
      }
    }
  ]
}

#针对报错:Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are #disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [category] in order to load field data by uninverting the #inverted index. Note that this can use significant memory #经过查证是字段category类型为Text格式,然后涉及到了聚合排序等功能。没有进行优化,也类似没有加索引。没有优化的字段es默认是禁止聚合/排序操作的。所以需要将要聚合的字段添加优化。如下操作

PUT shopping/_mapping
{
  "properties": {
    "category": { 
      "type":     "text",
      "fielddata": true
    }
  }
}

PUT shopping/_mapping
{
  "properties": {
    "price": { 
      "type":     "text",
      "fielddata": true
    }
  }
}

#多条件查询 must 是必须相当and

#1
GET shopping/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "category": "小米"
          }
        }
      ]
    }
  }
}
#2 
GET shopping/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "category": "小米"
          }
        },
         {
          "match": {
            "price": "1999"
          }
        }
      ]
    }
  }
}

#多条件查询 should 可以是---也可以是---必须相当or

GET shopping/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "category": "小米"
          }
        },
         {
          "match": {
            "category": "华为"
          }
        }
      ]
    }
  }
}

#多条件查询 范围查询 gt 大于 lt 小于

GET shopping/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "category": "小米"
          }
        },
         {
          "match": {
            "category": "华为"
          }
        }
      ],
      "filter": {
        "range": {
          "price": {
            "gt": 1999,
            "lt": 20000
          }
        }
      }
    }
  }
}

#全文检索 (match会将es查询内容进行分词检索查询)

GET shopping/_search
{
  "query": {
    "match": {
      "category": "小华"
    }
  }
}

#全文检索 (通过highlight 对查询的结果对category进行 高亮显示)

GET shopping/_search
{
  "query": {
    "match_phrase": {
      "category": "小米"
    }
  },
  "highlight": {
    "fields": {
      "category": {}
    }
  }
}

#聚合操作 (aggs:聚合操作;price_group :查询结果名称,随意起名;terms:分组;field:分组字段 )

GET shopping/_search
{
  "aggs": {  
    "price_group": {
      "terms": {
        "field": "price"
      }
    }
  }
}

#聚合操作 (size : 0 不显示原始数据只显示聚合结果)

GET shopping/_search
{
  "aggs": {  
    "price_group": {
      "terms": {
        "field": "price"
      }
    }
  },
  "size": 0
}

## 

#聚合操作 (平均值)

GET shopping/_search
{
  "aggs": {  
    "price_avg": {
      "avg": {
        "field": "price"
      }
    }
  },
  "size": 0
}

#--------------------------------------------------------------------------- #报错 :Field [price] of type [text] is not supported for aggregation [avg] #解决办法如下 GET shopping/_mapping

PUT shopping1 PUT shopping1/_mapping {

  "properties" : {
    "category" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }
    },
    "images" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }
    },
    "price" : {
      "type" : "long"
    },
    "title" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }
    }
  }
}

POST _reindex
{
  "source": {
    "index": "shopping"
  },
  "dest": {
    "index": "shopping1"
  }
}

DELETE shopping

POST _reindex
{
  "source": {
    "index": "shopping1"
  },
  "dest": {
    "index": "shopping"
  }
}

DELETE shopping1

#--------------------------------------------------------------

#映射关系

#先创建一个user 索引

PUT user
#创建映射关系
PUT user/_mapping
{
  "properties":{
    "name":{
      "type":"text",
      "index":true
    },
    "sex":{
      "type":"keyword",
      "index":true
    },
    "tel":{
      "type":"keyword",
      "index":false
    }
  }
}

#查看user的映射关系

GET user/_mapping

#插入一条数据  

PUT user/_create/1001
{
  "name":"嚣张的小张",
  "sex":"男的",
  "tel":123123121231111
}

#全局查询   (name类型为text 可以分词查询)
GET user/_search
{
  "query": {
    "match": {
      "name": "小"
    }
  }
}
#全局查询   (sex类型为keyword 为关键字,不能够 被分开)
GET user/_search
{
  "query": {
    "match": {
      "sex": "男"
    }
  }
}

#全局查询   (tel 为关键字,不能被索引,所以不能被查询)
GET user/_search
{
  "query": {
    "match": {
      "tel": "123"
    }
  }
}

你可能感兴趣的:(elasticsearch,大数据,搜索引擎,es)