ES 与sql对比学习 实例操作和进阶操作,不定时更新...

版权声明:诸葛老刘所有 https://blog.csdn.net/weixin_39791387/article/details/86480492

第一章: mapping(类似于sql的数据结构)

  • string类型: test, keyword类型 string类型在es5开始废弃使用
  • 数字类型: long integer short byte double float
  • 日期类型: date 可以解析python中的date_time
  • bool类型: boolean True false yes no 都可以进行解析
  • binary类型: binary 二进制类型 不会进行检索
  • 复杂类型: object, nested (1.1 将详细说明)
  • geo类型: geo-point, geo-shape 地理位置经纬度类型
  • 专业类型: ip, competion(做搜索建议)

es 会对设置字符串的字段text 进行分析, 分词处理. 而keyword 不会进行分词,必须进行全部匹配

1.1 数据类型说明

  • object 类型介绍
{
  "a":"b",
  "c":"d",
  "e":{
    "f":"g"
    "h":"j"
  }
}

对象 或者类似python的字典类型 称为object类型

  • nested类型
{
  "a":"b",
  "c":"d",
  "e":[{
    "f":"g",
    "h":"j"
  },
  {
    "f":"g",
    "h":"j"
  }]
}

数组或者称为列表这样的为nested类型

1.2 内置类型设置对应的属性以及类型

属性 概述 适合类型
store 值为yes标识存储,为no表示不存储,默认为no all
index yes表示分析,no表示不分析,默认值为true string
null_value 如果字段为空,可以设置一个默认值,NA all
analyzer 可以设置索引和搜索时用的分析器,默认使用的是 standard分析器, 还可以使用whitespace、simple, english all
include_in_all 默认es为每个文档定义一个特殊域_all,它的作用是让每个字段被搜索到,如果不想某个字段被搜索到,可以设置为false all
format 时间格式字符串的模式 date

第二章 ES实例化基本操作

2.1 准备测试数据

2.1.1 删除 zhuge_test (类似sql中的清空数据表)

DELETE zhuge_test

2.1.2 创建新的mapping (类似sql的建表)

PUT zhuge_test
{
    "settings":{
    "index":{
      "number_of_shards":"1",
      "number_of_replicas":"1"
    }
  },
  "mappings":{
    "job":{
      "properties":{
        "title":{
          "store":true,  # 本文2.2.8 里有说明, 默认为false
          "type":"text"
        },
        "company_name":{
          "store":true,
          "type":"keyword"
        },
        "desc":{
          "type":"text"
        },
        "comments":{
          "type":"integer"
        },
        "add_time":{
          "type":"date",
          "format":"yyyy-MM-dd"
        }
      }
    }
  }
}
  • 查看数据, 类似于sql中的select *
    GET zhuge_test/_mapping

2.1.3 准备查询所需要的数据(类似sql中的insert)

POST zhuge_test/job  # 向ES提交数据, 可以理解为接口的post请求表单提交
{
  "title": "python django 开发工程师",
  "company_name":  "美团科技有限公司",
  "desc":"对django的概念熟悉, 熟悉python基操",
  "comments":20,
  "add_time":"2019-1-19"
}
POST zhuge_test/job
{
  "title": "python scrapy redis 分布式爬虫基本",
  "company_name":  "百度科技有限公司",
  "desc":"对django的概念熟悉, 熟悉python基操",
  "comments":5,
  "add_time":"2019-1-18"
}
POST zhuge_test/job
{
  "title": "elasticsearch打造搜索引擎",
  "company_name":  "阿里巴巴科技有限公司",
  "desc":"熟悉推介引擎的原理以及算法, 掌握C语言",
  "comments":15,
  "add_time":"2016-1-18"
}
POST zhuge_test/job
{
  "title": "Python打造推介引擎引擎",
  "company_name":  "阿里巴巴科技有限公司",
  "desc":"熟悉推介引擎的原理以及算法, 掌握C语言",
  "comments":60,
  "add_time":"2016-10-18"
}
GET zhuge_test/_search  # 获取数据, 可以理解为接口的get请求数据

2.2 基本操作练起来

2.2.1 match 查询 对输入的进行分词

select * from table where title regexp 'python'

近似操作

GET zhuge_test/job/_search
{
  "query": {
    "match": {
      "title": "python"
    }
  }
}

2.2.2 term 查询 对传入的值不会做任何处理 精准匹配型

select * from table where company_name= '阿里巴巴科技有限公司'

近似操作

{
  "query": {
    "term": {
      "company_name": "阿里巴巴科技有限公司"
    }
  }
}

2.2.3 terms查询 查询多个 其中一个满足 就会返回数据

select * from table where title regexp '工程师|django|系统'

近似操作

GET zhuge_test/job/_search
{
  "query": {
    "terms":{
      "title":["工程师","django","系统"]
    }
  }
}

2.2.4 控制查询的返回数量 – 用于分页

select * from table where title regexp 'python' limit 0, 2

近似操作

GET zhuge_test/job/_search
{
  "query": {
    "match": {
      "title": "python"
    }
  },
  "from":0,
  "size":2
}

2.2.4 match_all 查询

select * from table

近似操作

GET zhuge_test/job/_search
{
  "query": {
    "match_all": {}
  }
}

2.2.6 match_phrase查询-- 短语查询

  • 传入词会进行分析并解析为数组,且必须同时满足数组所有的词才会返回结果。slop 则是传入词与词直接的距离
  • 暂无近义sql
GET zhuge_test/job/_search
{
  "query":{
    "match_phrase":{
      "title": {
        "query": "python开发",
        "slop": 1
      }
    }
  }
}

2.2.7 multi_match查询–可以指定多个字段

  • 比如查询title 和desc 这两个字段里包含python 的关键词文档 – 可以检索多个字段出现的查询词

title^3 是我们认为在title里出现的关键词会比desc 里多 现设置了titledesc 大3倍的权重值

GET zhuge_test/job/_search
{
  "query": {
    "multi_match": {
      "query": "python",
      "fields": ["title^3","desc"]
    }
  }
}

2.2.8 返回指定的字段

  • 使用关键字:stored_fields 在数组中设置要返回的字段即可,但必须在建立mapping 映射的时候该字段是 storetrue 的字段,如没有在该字段添加该属性 则没有返回值, 本文2.1.2 有演示
GET zhuge_test/job/_search
{
  "stored_fields":["title","company_name"],
  "query": {
    "match":{
      "title":"python"
    }
  }
}

2.2.9 查询并对结果排序操作

GET zhuge_test/job/_search
{
  "query":{
    "match_all":{}
  },
  "sort":[{
  "comments":{
    "order":"desc"
  }
}]
}

2.2.10 通用的范围查询–range

  • gte: 大于等于
  • gt 大于
  • lte 小于等于
  • lt 小于
  • 可设置该字段的权重 boost: 2
GET zhuge_test/job/_search
{
  "query": {
    "range":{
      "comments": {
        "gte": 10,
        "lte": 20,
        "boost": 1
      }
    }
  }
}

2.2.11 rang范围时间查询中的now 为当前时间

GET zhuge_test/job/_search
{
  "query": {
    "range": {
      "add_time": {
        "gte": "2016-04-01",
        "lte": "now"
      }
    }
  }
}

2.2.12 wildcard 查询 --模糊查询

  • 支持查询字符串里有通配符
  • boost 可不设置表名该字段的权重值
GET zhuge_test/job/_search
{
  "query": {
    "wildcard": {
      "title": {
        "value": "pyth*n",
        "boost": 2
      }
    }
  }
}

第三章 进阶操作

3.1 部分练习

POST zhuge/testdb/_bulk
{"index":{"_id":1}}
{"salary":10, "title":"python"}
{"index":{"_id":2}}
{"salary":20, "title":"scrap"}
{"index":{"_id":3}}
{"salary":30, "title":"Django"}
{"index":{"_id":4}}
{"salary":40, "title":"Elasticsearch"}
DELETE zhuge
POST zhuge/testdb
{
  "settings":{
    "index":{
      "number_of_shards":"1",
      "number_of_replicas":"1"
    }
  }
}
GET zhuge/_settings
GET zhuge/testdb/_search

3.2 多条件组合操作

  • 用bool查询包括 must, should, must_not, filter 来完成格式如下
 bool:{
 "filter":[],  对字段进行过滤同时不参与打分
 "must":[],   数组里的所有查询都必须满足
 "should":[],数组里的查询满足一个和多个都可以
 "must_not":{}, 数组里的查询一个都不能满足
}

3.2 简单的过滤查询实例

3.2.1 简单的filter查询

GET zhuge/testdb/_search
{
  "query":{
    "bool":{
      "must":{
        "match_all":{}
      },
      "filter": {
        "term": {
          "salary": "20"
        }
      }
    }
  }
}

int 类型不进行分析和分词 也可以用match查询

3.2.2 查询多个值

GET zhuge/testdb/_search
{
  "query": {
    "bool":{
      "must":{
        "match_all":{}
      },
      "filter":{
        "terms":{
          "salary":[10,20]
        }
      }
    }
  }
}

3.3 比较复杂的过滤组合查询方式

  • 例如要查询薪资等于20K或者工作为python的工作, 并且排除薪资为30k的
    should = sql or
GET zhuge/testdb/_search
{
  "query": {
    "bool": {
      "should": [
        {"term": {"salary": 20}},
        {"term":{"title": "python"}}
      ],
      "must_not":{
        "term":{"salary":30}
      }
    }
  }
}

3.3.1 must_not 不等于多个的情况 格式写法

GET zhuge/testdb/_search
{
  "query": {
    "bool": {
      "should": [
        {"term": {"salary": 20}},
        {"term":{"title": "python"}}
      ],
      "must_not":[{
        "term":{"salary": 30}},
        {"term":{"salary":10}}]
    }
  }
}

3.3.2 嵌套查询–dsl语句中 bool套bool 为嵌套查询

  • sql语句表达式
select * from testdb where title="python" or (title = "django" and salary=30)
  • DSL语句表达式
GET zhuge/testdb/_search
{
  "query": {
    "bool": {
      "should": [
        {"term": {"title": "python"}},
        {"bool": {
          "must":[
            {"term": {"title":"django"}},
            {"term":{"salary":30}}
            ]
        }}
      ]
    }
  }
}

3.3.3 过滤空和非空的测试数据

POST zhuge2/testdb/_bulk
{"index":{"_id":"1"}}
{"tags":["search"]}
{"index":{"_id":"2"}}
{"tags":["search","python"]}
{"index":{"_id":3}}
{"tags":["some data"]}
{"index":{"_id":4}}
{"tags":null}
{"index":{"_id":5}}
{"tags":["search", null]}

GET zhuge2/_settings
GET zhuge2/testdb/_search
POST zhuge2/testdb
{
“settings”:{
“index”:{
“number_of_shards”:“1”,
“number_of_replicas”:“1”
}
}
}

DELETE zhuge2

3.3. 4 处理null 空值的方法

  • sql语句表达式
select tags from testdb2 where tags is not null
  • DSL语句表达式
GET zhuge2/testdb/_search
{
  "query": {
    "bool": {
      "filter": {
        "exists": {
          "field": "tags"
        }
      }
    }
  }
}

3.3.5 查询为null的数据

GET zhuge2/testdb/_search
{
  "query": {
    "bool":{
      "must_not":{
        "exists":{
          "field": "tags"
        }
      }
    }
  }
}

猜你喜欢

转载自blog.csdn.net/weixin_39791387/article/details/86480492
今日推荐