ES与数据库对应关系
Elasticsearch | 索引(Index) | 类型(Type) | 文档(Docments) | 字段(Fields) |
---|---|---|---|---|
关系数据库(MySQL) | 数据库(DataBase) | 表(Table) | 行(Rows) | 列(Columns) |
基本命令
#创建索引库
PUT /shopping
#查看所有索引详细信息
GET /_cat/indices?v
#查询某个索引
GET /shopping
#删除索引库
DELETE /shopping
创建类型和映射
PUT 索引名称/类型名称/_mapping
{}
# 请求方法:PUT
PUT /shopping/product/_mapping
{
"properties": {
"title":{
"type": "text",
"analyzer": "ik_max_word"
},
"subtitle":{
"type": "text",
"analyzer": "ik_max_word"
},
"images":{
"type": "keyword",
"index": false
},
"price":{
"type": "float",
"index": true
}
}
}
查看类型
GET /shopping/product/_mapping
创建索引库同时创建类型和映射
# 请求方法:PUT
PUT /shopping2
{
"settings": {},
"mappings": {
"product":{
"properties": {
"title":{
"type": "text",
"analyzer": "ik_max_word"
},
"subtitle":{
"type": "text",
"analyzer": "ik_max_word"
},
"images":{
"type": "keyword",
"index": false
},
"price":{
"type": "float",
"index": true
}
}
}
}
}
添加文档
POST /shopping/product
{
"title":"小米手机",
"images":"http://www.gulixueyuan.com/xm.jpg",
"price":3999.00
}
查询文档
GET /shopping/product/刚才生成的id
指定id,不让系统自动生成
POST /shopping/product/1{} #此时id为1
修改:就是重新添加,会有覆盖
根据id修改
POST /shopping/product/1/_update
{
"doc": {
"price":3000.00
}
}
删除
DELETE /shopping/product/1
根据条件删除
POST /shopping/_delete_by_query
{
"query":{
"match":{
"title":"手机"
}
}
}
查询DSL
数据准备
POST /shopping/product/1
{
"title":"小米手机",
"images":"http://www.gulixueyuan.com/xm.jpg",
"price":3999.00
}
POST /shopping/product/2
{
"title":"华为手机",
"images":"http://www.gulixueyuan.com/hw.jpg",
"price":4999.00
}
POST /shopping/product/3
{
"title":"小米电视",
"images":"http://www.gulixueyuan.com/xmds.jpg",
"price":5999.00
}
查询所有
GET /shopping/_search
{
"query": {
"match_all": {}
}
}
匹配查询
# 请求方法:GET
GET /shopping/_search
{
"query": {
"match": {
"title": "小米手机"
}
}
}
此时会全查出来,因为做了分词处理
多字段匹配查询
GET /shopping/_search
{
"query": {
"multi_match": {
"query": "小米",
"fields": ["title","subtitle"]
}
}
}
先匹配title在匹配subtitle
关键词精确查询
GET /shopping/_search
{
"query": {
"term": {
"title": {
"value": "小米"
}
}
}
}
多关键词精确查询
GET /shopping/_search
{
"query": {
"terms": {
"price": [3999,5999]
}
}
}
指定查询字段
默认情况下,ElasticSearch在搜索的结果中,会把文档中保存在_source的所有字段都返回。
如果我们只想获取其中的部分字段,我们可以添加_source的过滤
GET /shopping/_search
{
"_source": ["title","price"],
"query": {
"terms": {
"price": [3999]
}
}
}
过滤指定字段:includes和excludes
includes:来指定想要显示的字段
excludes:来指定不想要显示的字段
GET /shopping/_search
{
"_source": {
"excludes": ["images"]
},
"query": {
"terms": {
"price": [3999]
}
}
}
布尔组合
bool
把各种其它查询通过must
(必须 )、must_not
(必须不)、should
(应该)的方式进行组合
GET /shopping/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "小米"
}
}
],
"must_not": [
{
"match": {
"title": "电视"
}
}
],
"should": [
{
"match": {
"title": "手机"
}
}
]
}
}
}
范围查询
range 查询找出那些落在指定区间内的数字或者时间。range查询允许以下字符:
操作符 | 说明 |
---|---|
gt == (greater than) | 大于> |
gte == (greater than equal) | 大于等于>= |
lt == (less than) | 小于< |
lte == (less than equal) | 小于等于<= 1 |
例子
GET /shopping/_search
{
"query": {
"range": {
"price": {
"gte": 2500,
"lte": 4000
}
}
}
}
模糊查询
当搜索单词写错时,可以自动改正搜索,2个以内必须完全匹配,3-5允许错一个....
GET /shopping/_search
{
"query": {
"fuzzy": {
"title": {
"value": "ccple",
"fuzziness": 2 //可以错2个
}
}
}
}
单字段排序
GET /shopping/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"price": {
"order": "desc"
}
}
]
}
多字段
GET /shopping/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"price": {
"order": "desc"
}
},
{
"_score":{
"order": "desc"
}
}
]
}
高亮查询
GET /shopping/_search
{
"query": {
"match": {
"title": "华为"
}
},
"highlight": {
"pre_tags": "<font color='red'>",
"post_tags": "</font>",
"fields": {
"title": {}
}
}
}
分页查询
GET /shopping/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"price": {
"order": "desc"
}
},
{
"_score":{
"order": "desc"
}
}
],
"from": 0,
"size": 2
}