elasticsearch下的各种查询

elasticsearch是功能非常强大的搜索引擎,使用它的目的就是为了快速的查询到需要的数据。

查询分类:

           基本查询:使用elasticsearch内置查询条件进行查询

          组合查询:把多个查询组合在一起进行复合查询

          过滤:查询同时,通过filter条件在不影响打分的情况下筛选数据

基本查询:一个一个写    下面的 组合查询用_bulk

创建一个索引shitou

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

添加数据:

POST shitou/job/
{
  "title":"python django 开发工程师",
  "company_name":"美团科技有限公司",
  "desc":"对django的概念熟悉  熟悉python基础知识",
  "comments":20,
  "add_time":"2017-4-1"
}
POST shitou/job/
{
  "title":"python scrapy redis 分布式爬虫基本",
  "company_name":"百度科技有限公司",
  "desc":"对scrapy的概念熟悉  熟悉redis的基本操作",
  "comments":5,
  "add_time":"2017-4-15"
}
POST shitou/job/
{
  "title":"elasticsearch打造搜索引擎",
  "company_name":"阿里巴巴科技有限公司",
  "desc":"熟悉数据结构算法 熟悉python的基本开发",
  "comments":15,
  "add_time":"2017-6-20"
}
POST shitou/job/
{
  "title":"python打造推荐引擎系统",
  "company_name":"阿里巴巴科技有限公司",
  "desc":"阿里巴巴科技有限公司",
  "comments":60,
  "add_time":"2016-10-20"
}

match查询:对输入的字段进行分词,无视大小写(ik)                 keyword对象:不会分析    text对象:进行分析

term查询:对输入的字段不会进行任何处理  

terms查询:传递一个数组,只要有任一满足都会返回

#match
GET shitou/_search
{
  "query": {
    "match": {
      "title": "python"
    }
  }
}



#term查询
GET shitou/_search
{
  "query": {
    "term": {
        "company_name":"阿里巴巴科技有限公司"
    }
  }
}

#terms查询
GET shitou/_search
{
  "query": {
    "terms": {
      "title": [
        "django",
        "工程师",
        "系统"
      ]              #这三个词有一个匹配就会返回
    }
  }
}

#控制查询的返回数量
GET shitou/_search
{
  "query": {
    "match": {
      "title": "python"
    }
  },
  "from": 1,           #分页操作,from:1从第一个开始   size:2   只取两个
  "size": 2
}

#match_all查询
GET shitou/_search
{
  "query": {
    "match_all": {}        
  }
}

#match_phrase查询
#短语查询
GET /shitou/_search
{
  "query": {
    "match_phrase": {
      "title": {
        "query": "python系统",             #必须满足query里面所有的值才会返回   
        "slop":6                           #slop是短语之间的距离
      }                    
    }
  }
}

#multi_match查询
#比如可以指定多个字段
#比如查询title和desc这两个字段里面包含python的关键词文档
GET shitou/_search
{
  "query": {
    "multi_match": {
      "query": "python",
      "fields": ["title^3","desc"]   #3代表权重,查询结果就会因为title而位置发生变化,也可以不用
    }
  }
}

#指定返回字段
GET shitou/_search
{
  "stored_fields": ["title","company_name"],   #返回到 store为True的字段
  "query": {
    "match": {
      "title": "python"
    }
  }
}


#通过sort把结果排序
GET shitou/_search
{
  "query": {
    "match_all": {}
  },
  "sort":[{
    "comments":{
      "order": "desc"   #asc:升序,desc:降序
    }
  }]
}

#查询范围
#range查询
GET shitou/_search
{
  "query": {
    "range": {
      "comments": {
        "gte": 10,
        "lte": 20,     #gte:大于等于,lte:小于等于。boost:权重
        "boost": 2
      }
    }
  }
}


GET shitou/_search
{
  "query": {
    "range": {
      "add_time": {
        "gte": "2017-04-01",
        "lte": now            #now就是指现在的时间
      }
    }
  }
}

#wildcard查询    #模糊匹配(注意里面有个*号)系统
GET shitou/_search
{
  "query": {
    "wildcard": {
      "title": {
        "value": "pyth*n"
      }
    }
  }
}

组合查询:_bulk

建立测试数据

POST shitou/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"}

查询

#最简单的filter查询
GET shitou/testjob/_search
{
  "query":{
    "bool":{
      "must":{
        "match_all":{}
      },
      "filter": {
        "term": {
          "salary": "20"
        }
      }
    }
  }
}

#也可以指定多个值
GET shitou/testjob/_search
{
  "query": {
    "bool":{
      "must": 
        {"match_all":{}},
        "filter": {
          "terms": {
            "salary": [10,20]
          }
        }
    }
  }
}



GET shitou/testjob/_search
{
  "query": {
    "bool":{
      "must":
        {"match_all":{}},
        "filter": {
          "match":{
            "title":"Python"
          }
        }
    }
  }
}

#查看分析器解析的结果

GET /_analyze
{
  "analyzer": "ik_max_word",
  "text":"python网络工程师"
}

#bool过滤查询,可以做组合过滤查询
#查询薪资等于20k或者工作为python的工作,排除价格为30k的
GET shitou/testjob/_search
{
  "query": {
    "bool":{
      "should": [
        {"term": {"salary":20}},
        {"term": {"title":"python"}}
      ],
      "must_not": {
        "term":{"price":30}
      }
    }
  }
}


#嵌套查询
GET shitou/testjob/_search
{
  "query": {
    "bool": {
      "should":[
        {"term":{"title": "python"}},
        {"bool":{
          "must": [
            {"term": {"title": "elasticsearch"}},
            {"term": {"salary": 30}}
              ]
        }
        }
        ]
}}}}

处理null空值的方法

exists

猜你喜欢

转载自blog.csdn.net/weixin_42166745/article/details/83340326