elasticsearch之python操作


  總結使用python對於elasticsearch的常用操作

 

  1. 安裝
pip  install elasticsearch

 

  2. 連接

from elasticsearch import Elasticsearch

es = Elasticsearch([{'host':'49.232.6.227' , 'port':9200}], timeout=3600)

# 添加驗證
# http_auth=('xiao', '123456')
es = Elasticsearch([{'host':'49.232.6.227' , 'port':9200}], http_auth=http_auth, timeout=3600)

 

  3. 查詢

1)全部查詢

query = {
    'query': {
        'match_all': {}
    }
}

result = es.search(index=account_index, body=query)

for row in result['hits']['hits']:
    print(row)

 

2)term 過濾--term主要用於精確匹配哪些值,比如數字,日期,布爾值或 not_analyzed 的字符串(未經切詞的文本數據類型)

query = {
    "query": {
        "term":{
            'age': 32
        }
    }
}
result = es.search(index="megacorp", body=query)
print(result)
# first_name 可能經過切詞了
query = {
    "query": {
        "term":{
            'first_name': 'Jane'
        }
    }
}
result = es.search(index="megacorp", body=query)
print(result)

 

3)terms 過濾--terms 跟 term 有點類似,但 terms 允許指定多個匹配條件。 如果某個字段指定了多個值,那么文檔需要一起去做匹配

query = {
    'query': {
        'terms': {
            'name': ['111111', '22222']
        }
    }
}

 

4) 查詢文檔中是否某個字段

query = {
    'query': {
        'exists': {
            'field': 'age'
        }
    }
}

 

5) 布爾值

  • bool 過濾--合並多個過濾條件查詢結果的布爾邏輯
    • must :: 多個查詢條件的完全匹配,相當於 and。
    • must_not :: 多個查詢條件的相反匹配,相當於 not。
    • should :: 至少有一個查詢條件匹配, 相當於 or。
query = {
    'query': {
        'bool': {
            'must': {
                'term': {"_score": 1.0},
                'term': {'name': 'lanlang'}
            }
        }
    }
}

# 匹配name為lanlang 並且沒有age字段的記錄
query = {
'query': {
'bool': {
'must': {
'term': {
'name': 'lanlang'
}
},
'must_not': {
'exists': {
'field': 'age'
}
}
}
}
}
 

 

6) 范圍查找

  • gt : 大於
  • gte : 大於等於
  • lt : 小於
  • lte : 小於等於
query = {
    'query': {
        'range': {
            'age': {
                'lt': 10
            }
        }
    }
}

 

 7)match標准查詢

# 做精確匹配搜索時,你最好用過濾語句,因為過濾語句可以緩存數據。
# match查詢只能就指定某個確切字段某個確切的值進行搜索,而你要做的就是為它指定正確的字段名以避免語法錯誤。
query = {
    "query": {
        "match": {
            "about": "rock"
        }
    }
}

 

8)multi_match 查詢--match查詢的基礎上同時搜索多個字段,在多個字段中同時查一個

 
query = {
    'query': {
        'multi_match': {
            'query': 'lanlang',
            'fields': ['name','wife']
        }
    }
}

 

9 )wildcards 查詢--使用標准的shell通配符查詢

 
query = {
    'query': {
        'wildcard': {
            'name': 'lan*'
        }
    }
}

 

10 )regexp查詢

query = {
    "query": {
        "regexp": {
            "about": ".a.*"
        }
    }
}

 

11)prefix  以什么開頭

query = {
    'query': {
        'prefix': {
            'name': 'lan'
        }
    }
}

 

 12)短語匹配(Phrase Matching) -- 尋找鄰近的幾個單詞

query = {
    "query": {
        "match_phrase": {
            "about": "I love"
        }
    }
}

 

13)統計查詢

query = {
    "query": {
        "match_phrase": {
            "about": "I love"
        }
    }
}
result = es.count(index="megacorp", body=query)

{'count': 4, '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0}}

 

 

  4. 插入數據

1)不指定ID

# body = {
#     'name': 'xing',
#     'age': 9,
#     'sex': 0,
#     'wife': 'maomao'
# }

# result = es.index(index=account_index, body=body)

2)指定ID

es.index(index="megacorp",id=4,body={"first_name":"xiao","last_name":"wu", 'age': 66, 'about': 'I love to go rock climbing', 'interests': ['sleep', 'eat']})

 

 

  5. 刪除數據

1)指定ID刪除

id = '5DhJUHEBChSA6Z-1wbVW'

ret = es.delete(index=account_index, id=id)

 

2)根據查詢條件刪除

query = {
    "query": {
        "match": {
            "first_name": "xiao"
        }
    }
}
result = es.delete_by_query(index="megacorp", body=query)

 

  6. 更新

1)指定ID更新

id = '5ThEVXEBChSA6Z-1OrVA'

# 刪除字段 doc_body = { 'script': 'ctx._source.remove("wife")' } ret = es.update(index=account_index, id=id, body=doc_body) print(ret)


# 增加字段 doc_body = { 'script': "ctx._source.address = '合肥'" } 

# 修改部分字段
doc_body = {
'doc': {'name': 'xing111'}
}
 

 

2)滿足條件進行更新

query = {
    "query": {
        "match": {
            "last_name": "xiao"
        }
    },
    "script":{
        "source": "ctx._source.last_name = params.name;ctx._source.age = params.age",
        "lang": "painless",
        "params" : {
            "name" : "wang",
            "age": 100,
        },  
    }

}
result = es.update_by_query(index="megacorp", body=query)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM