[英]How can the getter/setter method be used in python to manipulate a list?
[英]How to transform a python getter method to an elasticsearch query?
我想翻譯python方法,以獲取從已刪除網站到Elasticsearch查詢的特定術語。
我正在做一個實習,在webscraping和elasticsearch(和其他東西......),我是這個領域的新手(和一般的編程)
我被賦予了抓取國家/地區代碼的任務,然后查詢以使用其他國家/地區代碼獲取國家/地區代碼,例如:
澳大利亞的2個字符的國家代碼是:'AU',它的三個字符是國家代碼:'AUS'
因此,通過精確'AU',我想要'AUS'代碼。
為此,我已經抓取了所有國家列表代碼,並制作了一個python代碼來獲得這個結果,一個例子如下:
"took": 84,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 248,
"max_score": 1,
"hits": [
{
"_index": "countries-codes",
"_type": "event",
"_id": "Gx_gEGoBP2qGR-HHGMw3",
"_score": 1,
"_source": {
"name": "Albanie",
"alpha_2": "AL",
"alpha_3": "ALB",
"num": "8"
}
},
def get_alpha2_by_alpha3(self, alpha_3):
for element in self.countries_list.get_countries_list():
if element['alpha_3'] == alpha_3.upper():
return element['alpha_2']
所以基本上我想將上面的代碼翻譯成一個請求,然后在網頁中實現它以供內部使用
請盡可能明確,我是初學者。
假設您在索引文檔時使用了默認動態映射,那么所有strings
都應該被映射為text
類型和keyword
類型。 因此, keyword
映射的簡單term
查詢應該會產生您正在尋找的結果。
例如,使用默認設置創建索引的過程如下:
PUT countries-codes
索引提供的文檔如下所示:
POST countries-codes/event
{
"name": "Albanie",
"alpha_2": "AL",
"alpha_3": "ALB",
"num": "8"
}
現在,我們可以查看索引的映射,以了解Elasticsearch如何在內部映射字段:
GET countries-codes/_mapping
結果:
{
"countries-codes" : {
"mappings" : {
"event" : {
"properties" : {
"alpha_2" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"alpha_3" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"num" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
}
現在我們只是對2個字符的國家代碼的keyword
映射進行一個term
查詢,然后我們將得到一個表示匹配的文檔(或者在某種情況下有多個匹配,所有文檔代表這些匹配):
GET countries-codes/_search
{
"query": {
"bool": {
"filter": {
"term": {
"alpha_2.keyword": "AL"
}
}
}
}
}
請注意,這是一個已過濾的查詢,因為您對評分不感興趣。 簡而言之,過濾器上下文將比查詢上下文更快,因此請盡可能使用它。 有關更多信息,請參閱: https : //www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html
這會產生您之前發布的文檔,位於hits
返回數組中:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.0,
"hits" : [
{
"_index" : "countries-codes",
"_type" : "event",
"_id" : "qGDmEWoBqkB-aMRpdfvt",
"_score" : 0.0,
"_source" : {
"name" : "Albanie",
"alpha_2" : "AL",
"alpha_3" : "ALB",
"num" : "8"
}
}
]
}
}
提交的任何不匹配的條款都會產生一個空的命中數組。 在客戶端,您可以解析出您想要的元素。 如果您有非常大的文檔或返回大量文檔,您可能需要查看source filtering
- https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source- filtering.html
例如:
GET countries-codes/_search
{
"_source": "alpha_3",
"query": {
"bool": {
"filter": {
"term": {
"alpha_2.keyword": "AL"
}
}
}
}
}
在返回的命中對象中,您會注意到從文檔中只返回了您想要的結果:
"hits" : {
"total" : 1,
"max_score" : 0.0,
"hits" : [
{
"_index" : "countries-codes",
"_type" : "event",
"_id" : "qGDmEWoBqkB-aMRpdfvt",
"_score" : 0.0,
"_source" : {
"alpha_3" : "ALB"
}
}
]
}
所有示例都使用Dev Tools / simple API調用顯示。 由於您使用的是Python,請查看官方維護的Elasticsearch庫:
Elasticsearch DSL - 建立在較低級別Elasticsearch-Py之上 - https://elasticsearch-dsl.readthedocs.io/en/latest/
Elasticsearch-Py - https://elasticsearch-py.readthedocs.io/en/master/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.