1. 程式人生 > >elasticsearch下的各種查詢

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