溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python-ElasticSearch搜索查詢的講解

發(fā)布時間:2020-10-17 10:20:40 來源:腳本之家 閱讀:143 作者:奔跑的豆子_ 欄目:開發(fā)技術(shù)

Elasticsearch 是一個開源的搜索引擎,建立在一個全文搜索引擎庫 Apache Lucene™ 基礎(chǔ)之上。 Lucene 可能是目前存在的,不論開源還是私有的,擁有最先進,高性能和全功能搜索引擎功能的庫。但是 Lucene 僅僅只是一個庫。為了利用它,你需要編寫 Java 程序,并在你的 java 程序里面直接集成 Lucene 包。 更壞的情況是,你需要對信息檢索有一定程度的理解才能明白 Lucene 是怎么工作的。Lucene 是 很 復(fù)雜的。

在上一篇文章中介紹了ElasticSearch的簡單使用,接下來記錄一下ElasticSearch的查詢:

查詢所有數(shù)據(jù)

# 搜索所有數(shù)據(jù)
es.search(index="my_index",doc_type="test_type")
# 或者
body = {
  "query":{
    "match_all":{}
  }
}
es.search(index="my_index",doc_type="test_type",body=body)

term與terms

# term
body = {
  "query":{
    "term":{
      "name":"python"
    }
  }
}
# 查詢name="python"的所有數(shù)據(jù)
es.search(index="my_index",doc_type="test_type",body=body)
# terms
body = {
  "query":{
    "terms":{
      "name":[
        "python","android"
      ]
    }
  }
}
# 搜索出name="python"或name="android"的所有數(shù)據(jù)
es.search(index="my_index",doc_type="test_type",body=body)

match與multi_match

# match:匹配name包含python關(guān)鍵字的數(shù)據(jù)
body = {
  "query":{
    "match":{
      "name":"python"
    }
  }
}
# 查詢name包含python關(guān)鍵字的數(shù)據(jù)
es.search(index="my_index",doc_type="test_type",body=body)
# multi_match:在name和addr里匹配包含深圳關(guān)鍵字的數(shù)據(jù)
body = {
  "query":{
    "multi_match":{
      "query":"深圳",
      "fields":["name","addr"]
    }
  }
}
# 查詢name和addr包含"深圳"關(guān)鍵字的數(shù)據(jù)
es.search(index="my_index",doc_type="test_type",body=body)

ids

body = {
  "query":{
    "ids":{
      "type":"test_type",
      "values":[
        "1","2"
      ]
    }
  }
}
# 搜索出id為1或2d的所有數(shù)據(jù)
es.search(index="my_index",doc_type="test_type",body=body)

復(fù)合查詢bool

bool有3類查詢關(guān)系,must(都滿足),should(其中一個滿足),must_not(都不滿足)

body = {
  "query":{
    "bool":{
      "must":[
        {
          "term":{
            "name":"python"
          }
        },
        {
          "term":{
            "age":18
          }
        }
      ]
    }
  }
}
# 獲取name="python"并且age=18的所有數(shù)據(jù)
es.search(index="my_index",doc_type="test_type",body=body)

切片式查詢

body = {
  "query":{
    "match_all":{}
  }
  "from":2  # 從第二條數(shù)據(jù)開始
  "size":4  # 獲取4條數(shù)據(jù)
}
# 從第2條數(shù)據(jù)開始,獲取4條數(shù)據(jù)
es.search(index="my_index",doc_type="test_type",body=body)

范圍查詢

body = {
  "query":{
    "range":{
      "age":{
        "gte":18,    # >=18
        "lte":30    # <=30
      }
    }
  }
}
# 查詢18<=age<=30的所有數(shù)據(jù)
es.search(index="my_index",doc_type="test_type",body=body)

前綴查詢

body = {
  "query":{
    "prefix":{
      "name":"p"
    }
  }
}
# 查詢前綴為"趙"的所有數(shù)據(jù)
es.search(index="my_index",doc_type="test_type",body=body)

通配符查詢

body = {
  "query":{
    "wildcard":{
      "name":"*id"
    }
  }
}
# 查詢name以id為后綴的所有數(shù)據(jù)
es.search(index="my_index",doc_type="test_type",body=body)

排序

body = {
  "query":{
    "match_all":{}
  }
  "sort":{
    "age":{         # 根據(jù)age字段升序排序
      "order":"asc"    # asc升序,desc降序
    }
  }
}

filter_path

響應(yīng)過濾

# 只需要獲取_id數(shù)據(jù),多個條件用逗號隔開
es.search(index="my_index",doc_type="test_type",filter_path=["hits.hits._id"])
# 獲取所有數(shù)據(jù)
es.search(index="my_index",doc_type="test_type",filter_path=["hits.hits._*"])

count

執(zhí)行查詢并獲取該查詢的匹配數(shù)

# 獲取數(shù)據(jù)量
es.count(index="my_index",doc_type="test_type")

度量類聚合

  • 獲取最小值
body = {
  "query":{
    "match_all":{}
  },
  "aggs":{            # 聚合查詢
    "min_age":{         # 最小值的key
      "min":{         # 最小
        "field":"age"    # 查詢"age"的最小值
      }
    }
  }
}
# 搜索所有數(shù)據(jù),并獲取age最小的值
es.search(index="my_index",doc_type="test_type",body=body)
  • 獲取最大值
body = {
  "query":{
    "match_all":{}
  },
  "aggs":{            # 聚合查詢
    "max_age":{         # 最大值的key
      "max":{         # 最大
        "field":"age"    # 查詢"age"的最大值
      }
    }
  }
}
# 搜索所有數(shù)據(jù),并獲取age最大的值
es.search(index="my_index",doc_type="test_type",body=body)
  • 獲取和
body = {
  "query":{
    "match_all":{}
  },
  "aggs":{            # 聚合查詢
    "sum_age":{         # 和的key
      "sum":{         # 和
        "field":"age"    # 獲取所有age的和
      }
    }
  }
}
# 搜索所有數(shù)據(jù),并獲取所有age的和
es.search(index="my_index",doc_type="test_type",body=body)
  • 獲取平均值
body = {
  "query":{
    "match_all":{}
  },
  "aggs":{            # 聚合查詢
    "avg_age":{         # 平均值的key
      "sum":{         # 平均值
        "field":"age"    # 獲取所有age的平均值
      }
    }
  }
}
# 搜索所有數(shù)據(jù),獲取所有age的平均值
es.search(index="my_index",doc_type="test_type",body=body)

更多的搜索用法:

https://elasticsearch-py.readthedocs.io/en/master/api.html

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對億速云的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI