Elasticsearch查询DSL

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
}

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

远走与梦游

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值