ElasticSearch的基本操作(简单易懂)

一、索引库操作

1.1 新增索引库

PUT /kwgmall

1.2 查看索引库

GET /kwgmall

1.3 删除索引库

DELETE /kwgmall

二、配置映射信息

2.1

PUT /kwgmall/goods/_mapping
{
  "properties": {
    "title":{
      "type": "text",
      "analyzer": "ik_max_word",
      "index": true,
      "store": false
    },
    "subtitle":{
      "type": "text",
      "analyzer": "ik_max_word",
      "index": true,
      "store": false
    },
    "images":{
      "type": "keyword",
        "index": false,
      "store": false
    },
    "price":{
      "type": "double",
      "index": true,
      "store": false
    }
  }
}

2.2 查看映射信息

GET /kwgmall/goods/_mapping

2.3 一次性创建索引库并配置映射

PUT /kwgmall001
{
  "mappings":{
    "goods":{
       "properties": {
        "title":{
          "type": "text",
          "analyzer": "ik_max_word",
          "index": true,
          "store": false
        },
        "subtitle":{
          "type": "text",
          "analyzer": "ik_max_word",
          "index": true,
          "store": false
        },
        "images":{
          "type": "keyword",
            "index": false,
          "store": false
        },
        "price":{
          "type": "double",
          "index": true,
          "store": false
        }
      }
    }
  }
}

三、文档操作

3.1 文档新增POST /{索引库名称}/{类型名称}/{主键ID}

如果不写主键,会随机生成一个字符串
PUT 在restful风格中确实是表示新增

POST /kwgmall/goods
{
  "title":"华为手机",
  "images":"http://baidu.com/img/time.jpg",
  "price":2699
}

3.2 查看

GET /kwgmall/goods/onDqaHQBEIzRTBWGMB6h

3.3 自定义ID新增

POST /kwgmall/goods/1001010086
{
  "title":"华为手机",
  "images":"http://baidu.com/img/time.jpg",
  "price":2699
}

3.4 修改

POST /kwgmall/goods/1001010086
{
  "title":"华为手机",
  "images":"http://baidu.com/img/time.jpg",
  "price":3999
}

3.5 删除

DELETE /kwgmall/goods/1001010086

3.6 批量新增

POST /kwgmall/goods/_bulk
{"index":{"_index" : "kwgmall","_type" : "goods"}}
{"title":"大米手机","images":"http://image.leyou.com/12479122.jpg","price":3288}
{"index":{"_index" : "kwgmall","_type" : "goods"}}
{"title":"小米手机","images":"http://image.leyou.com/12479122.jpg","price":2699}
{"index":{"_index" : "kwgmall","_type" : "goods"}}
{"title":"小米电视4A","images":"http://image.leyou.com/12479122.jpg","price":4288}
{"index":{"_index" : "kwgmall","_type" : "goods"}}
{"title": "华为手机","images": "http://image.leyou.com/12479122.jpg","price": 5288,"subtitle": "小米"}
{"index":{"_index" : "kwgmall","_type" : "goods"}}
{"title":"apple手机","images":"http://image.leyou.com/12479122.jpg","price":5899.00}

3.7批量删除

POST /kwgmall/_delete_by_query
{
  "query":{
    "match_all":{}
  }
}

四、请求体查询

4.1 基本查询

4.1.1 查询所有 match_all 匹配所有

POST /kwgmall/goods/_search
{
  "query": {
    "match_all":{}
  }
}

4.1.2 match查询 带分词器的查询,在指定的字段中进行搜索

将查询的内容进行分词,然后去指定的字段中进行匹配,关键字之间的关系是or
operator是查询关键字之前的关系,默认是or

POST /kwgmall/goods/_search
{
  "query": {
    "match": {
      "title": "小米手机"
    }
  }
}
POST /kwgmall/goods/_search
{
  "query": {
    "match": {
      "title": {
        "operator": "and",
        "query": "小米手机"
      }
    }
  }
}

查看分词结果

POST /_analyze
{
  "analyzer": "ik_max_word",
  "text":"小米手机"
}

4.1.3 多字段的match查询 ,可以指定多个字段

POST /kwgmall/goods/_search
{
  "query": {
    "multi_match": {
      "query": "小米",
      "fields": ["title","subtitle"]
    }
  }
}

4.1.4 term查询,关键字匹配查询,不会对查询条件进行分词,把查询条作为关键词去指定的字段匹配

POST /kwgmall/goods/_search
{
  "query": {
    "term": {
      "title": {
        "value": "小米"
      }
    }
  }
}

4.1.5 多关键词的terms查询

POST /kwgmall/goods/_search
{
  "query": {
    "terms": {
      "price": [
        "2699",
        "3288"
      ]
    }
  }
}

4.2 查询结果的过滤

POST /kwgmall/goods/_search
{
  "_source": ["title","price"],
  "query": {
    "term": {
      "title": {
        "value": "小米"
      }
    }
  }
}

excludes 代表不包含,排除的字段
includes 代表包含,排除的字段

POST /kwgmall/goods/_search
{
  "_source": {
    "excludes": "price"
  },
  "query": {
    "term": {
      "title": {
        "value": "小米"
      }
    }
  }
}

4.3 高级查询

4.3.1 布尔组合查询

must 必须满足条件
must not 必须不满足条件
should 应该满足条件

POST /kwgmall/goods/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "小米"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "title": "电视"
          }
        }
      ],
      "should": [
        {
          "match": {
            "title": "手机"
          }
        }
      ]
    }
  }
}

4.3.2 范围查询

gt(greater than) 大于
lt(less than) 小于
gte(greater than equals) 大于等于
lte(less than equals) 小于等于

POST /kwgmall/goods/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 3000,
        "lte": 5000
      }
    }
  }
}

4.3.3 模糊组合查询

POST /kwgmall/goods/_search
{
  "query": {
    "fuzzy": {
      "title": "appla"
    }
  }
}

默认偏差值是2,自动纠正两个字的错误

POST /kwgmall/goods/_search
{
  "query": {
    "fuzzy": {
      "title": {
        "value": "appla",
        "fuzziness": 1
      }
    }
  }
}

4.4 分页查询及排序查询

currentPage 当前页
pageSize 每页显示的条数
from 当前页第一个文档的起始序号
size 每页显示的条数
int from = (currentPage - 1) * pageSize
正序 ASC ,倒叙 DESC

POST /kwgmall/goods/_search
{
  "query": {
    "match_all": {}
  },
  "size": 2,
  "from": 4, 
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ]
}

4.5 高亮查询

关键词被字条标签包裹,
搜索的内容必须包含关键词,指定字段

pre_tags 设置标签头
post_tags 设置标签尾部
fields 设置高亮字段,必须是个对象,而不是一个字符串

POST /kwgmall/goods/_search
{
  "query": {
    "match": {
      "title": "小米"
    }
  },
  "highlight": {
    "pre_tags": "<font color='red'>"
    "post_tags": "</font>",
    "fields": {
      "title": {}
    }
  }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值