5、elasticsearch查询

1、elasticsearch安装
2、elasticsearch概念
3 、elasticsearch的crud、批量操作
4、elasticsearch映射mapping
5、elasticsearch查询

5、elasticsearch查询_第1张图片
图片.png

准备

PUT lagou
{
  "mappings": {
    "job":{
      "properties":{
        "title":{
          "type": "text",
          "store":true,
          "analyzer": "ik_max_word"
        },
        "company_name": {
          "type": "keyword",
          "store":true
        },
        "desc":{
          "type":"text"
        }, 
        "add_time":{
          "type":"date",
          "format":"yyyy-MM-dd"
        },
        "comments":{
          "type": "integer"
        }
      }
    }
  }
}


POST lagou/job
{
  "title":"python django 开发工程师" ,
  "company_name":"美国科技有限公司",
  "desc":"对django的概念熟悉,熟悉python基础知识", 
  "comments":20,
  "add_time":"2017-04-01"  
}

POST lagou/job
{
  "title":"python scrapy redis 分布式爬虫基本" ,
  "company_name":"百度科技有限公司",
  "desc":"对scrapy的概念熟悉,熟悉redis的基本操作",
  "comments":5,
  "add_time":"2017-04-15"  
} 

POST lagou/job
{
  "title":"Elasticsearch打造搜索引擎" ,
  "company_name":"阿里巴巴科技有限公司",
  "desc":"熟悉数据结构算法,熟悉python的基本开发",
  "comments":15,
  "add_time":"2017-06-20"  
} 

POST lagou/job
{
  "title":"python打造推荐引擎系统" ,
  "company_name":"阿里巴巴科技有限公司",
  "desc":"熟悉推荐引擎的原理以及算法、掌握C语言",
  "comments":60,
  "add_time":"2016-10-20"  
} 

简单查询

#查看分析器解析的结果
GET _analyze
{
  "analyzer": "ik_smart",
  "text":"Python网络开发师"
}
GET _analyze
{
  "analyzer": "ik_max_word",
  "text":"Python网络开发师"
}

#match查询 (分词查询) python 和分布式
#查询第0-2条的title和company_name字段(desc字段的stored属性不是true),并按comments排序
GET lagou/_search
{
 "stored_fields":["title","company_name","desc"], 
  "query":{
    "match":{
      "title":"python分布式"  
    }
  },
 "from": 0,
  "size": 2,
  "sort": [
    {
      "comments": {
        "order": "desc"
      }
    }
  ]
} 
#查询comments在大于等于10、小于等于20、权重2.0的数据
GET lagou/_search
{
  "query":{  
    "range": {
      "comments": {
        "gte": 10,
        "lte": 20,
        "boost":2.0
      }
    }
  }
}
GET lagou/_search
{
  "query":{  
    "range": {
      "add_time": {
        "gte": "2017-04-01",
        "lte": "now", 
      }
    }
  }
}

#term查询(不会做处理、直接查,类似于keyword属性)
GET lagou/_search
{
  "query":{
    "term":{
      "title":"python"  
    }
  }
}
#terms 和用match查django分布工程  效果一样
GET lagou/_search
{
  "query":{
    "terms":{
      "title":["django"  ,"分布"  ,"工程"  ]
    }
  }
}

#match_all
GET lagou/_search
{
  "query":{
    "match_all":{}
  }
}
 
#match_phrase 满足所有词 既有python也有系统,俩个词最小间距6位
GET lagou/_search
{
  "query":{
    "match_phrase": {
      "title": {
        "query": "python系统",
        "slop":6
      }
    }
  }
}

#multi_match 多字段匹配,title的权重高于desc的3倍
GET lagou/_search
{
  "query":{
    "multi_match": { 
      "query": "python系统",
      "fields":["title^3","desc"]
    }
  }
}

#wildcard 通配符查询
GET lagou/_search
{
  "query":{  
    "wildcard": {
      "title": {
        "value": "pyth*n"
      }
    }
  }
}

组合查询

#bool 查询
#用 bool 包括 must should must_not filter来完成
#格式如下
#bool:{
#  "filter":[], #不参与打分
#  "must":[],  #相当于        (salary=20 and title=Python)
#  "should":[], #相当于       (salary=20 or title=Python)
#  "must_not":[], #相当于not
#}

#建立测试数据
POST lagou/testjob/_bulk
{"index":{"_id":1}}
{"salary":10,"title":"Python"}
{"index":{"_id":2}}
{"salary":20,"title":"Scrapy"}
{"index":{"_id":3}}
{"salary":30,"title":"Django"}
{"index":{"_id":4}}
{"salary":30,"title":"Elasticsearch"}

DELETE lagou/testjob

#简单的过滤查询
#最简单的fileter查询
#select * from testjob where salary=20
GET lagou/testjob/_search
{
  "query":{
    "bool": { 
      "must":{
          "match":{
             "salary":10
          }
        },
      "filter":{ 
        "terms":{
          "title":["Python"]
        }
      }
    }
  }
}
#select * from testjob
#where (salary=20 or title=Python) and salary!=30 and salary!=10
GET lagou/testjob/_search
{
  "query":{
    "bool": { 
      "should":[
          {"term":{"salary":20}},
          {"term":{"title":"python"}}
        ],
      "must_not": [
        {"term": {"salary": "30"}},
        {"term": {"salary": "10"}}
      ] 
    }
  }
}

#where (salary=30 and title="django") or title="python"
GET lagou/testjob/_search
{
  "query":{
    "bool": { 
      "should":[
          {"term":{"title":"python"}},
          {"bool": { 
            "must":[
                {"term":{"salary":30}},
                {"term":{"title":"django"}}
              ] 
          }}
        ] 
    }
  }
}

#测试数据
POST lagou/testjob2/_bulk
{"index":{"_id":1}}
{"tags":["search"]}
{"index":{"_id":2}}
{"tags":["search","python"]}
{"index":{"_id":3}}
{"other_filed":["some data"]}
{"index":{"_id":4}}
{"tags":null}
{"index":{"_id":5}}
{"tags":["search",null]}

#处理null空值的方法
#select tags from testjob2 where tags is not null
GET lagou/testjob2/_search
{
  "query": {
    "bool": {
      "filter": {
        "exists": {
          "field": "tags"
        }
      }
    }
  }
}
#select tags from testjob2 where tags is null
GET lagou/testjob2/_search
{
  "query": {
    "bool": {
      "must_not": {
        "exists": {
          "field": "tags"
        }
      }
    }
  }
}

你可能感兴趣的:(5、elasticsearch查询)