国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

python使用ElasticSearch7.17.6筆記

這篇具有很好參考價值的文章主要介紹了python使用ElasticSearch7.17.6筆記。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

數(shù)操作系統(tǒng):windows10

我開始使用最新的版本,8.4.1但是使用過程中kibana啟動不了,就索性使用舊版;

下載地址:

es7.17.6 下載地址?

kibana7.17.6下載地址

解壓到合適的位置,更改elasticsearch.yml

添加配置如下:

cluster.name: robin-es
node.name: node-1
network.host: 0.0.0.0
http.port: 9200
cluster.initial_master_nodes: ["node-1"]

更改kibana.yml配置

i18n.locale: "zh-CN"

到各自的bin目錄下啟動兩個服務(wù)bat文件,

在瀏覽器中執(zhí)行http:://localhost:9200

可以看到j(luò)son就對了

{
  "name" : "node-1",
  "cluster_name" : "robin-es",
  "cluster_uuid" : "pAvuRyRESuCHtbTnfdWrvA",
  "version" : {
    "number" : "7.17.6",
    "build_flavor" : "default",
    "build_type" : "zip",
    "build_hash" : "f65e9d338dc1d07b642e14a27f338990148ee5b6",
    "build_date" : "2022-08-23T11:08:48.893373482Z",
    "build_snapshot" : false,
    "lucene_version" : "8.11.1",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

使用python需要添加一下相關(guān)的庫,我這里使用國內(nèi)的庫,并且使用代理,

注意:建議使用對應(yīng)版本的庫,否則可能不兼容。

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/   elasticsearch==7.17.6 --proxy="http://127.0.0.1:1081"
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/   elasticsearch[async]==7.17.6 --proxy="http://127.0.0.1:1081"

連接數(shù)據(jù)庫:

indexName = "student"
client = Elasticsearch(
    ['127.0.0.1:9200'],
    # 在做任何操作之前,先進行嗅探
    # sniff_on_start=True,
    # # 節(jié)點沒有響應(yīng)時,進行刷新,重新連接
    sniff_on_connection_fail=True,
    # # 每 60 秒刷新一次
    sniffer_timeout=60
)

寫幾個增刪改查的函數(shù):

需要注意:7.15版本以上使用了新的函數(shù),舊的方式已經(jīng)不適用了

# 推薦使用  elasticsearch  需要注意版本問題
from queue import Empty
from elasticsearch import Elasticsearch
from elasticsearch import *
import json
# es 7.17


def checkIndexByName(client, indexName):
    try:
        res = client.indices.get(index=indexName)
        # print(res)
        return True
    except Exception as ex:
        return False

# 創(chuàng)建索引
def createIndex(client, name,  doc):
    ret = False
    try:
        # Elasticsearch.options()
        resp = client.indices.create(index=name, mappings=doc["mappings"])
        # print(resp['result'])
        ret = True
    except Exception as ex:
        print(ex)
        return False

    return ret

# 刪除索引 
def dropIndex(client, name):
    ret = False
    try:
        # Elasticsearch.options()
        result = client.indices.delete(index=name)
        ret = True
    except:
        return False

    return ret


def addDoc(client, index, doc, id):
    # 重復(fù)添加,數(shù)據(jù)覆蓋
    try:
        resp = client.index(index=index, document=doc, id=id)
        print(resp['result'])
        return True
    except Exception as e:
        print("create index error")
        return False


def delDocFromIndex(client, index, id):
    try:
        res = client.delete(index=index, id=id)
        print(res['_shards']['successful'])
        return '1'
    except Exception as e:
        print(e)
        return '0'


def findDocById(client, index, id):
    try:
        res = client.get(index=index, id=id)
        return res['_source']
    except Exception as e:
        print(e)
        return 'nil'

創(chuàng)建索引的過程,可以在外部配置文件中設(shè)置相關(guān)的參數(shù),

比如我們創(chuàng)建一個學(xué)生的相關(guān)索引,我們建立一個配置文件student.json

{
    "settings": {
        "index": {
            "number_of_shards": 1,
            "number_of_replicas": 0
        }
    },
    "mappings": {
        "dynamic": "strict",
        "properties": {
            "name": {
                "type": "key"
            },
            "age": {
                "type": "long"
            },
            "birthday": {
                "type": "date"
            }
        }
    }
}

之后,我們創(chuàng)建索引時候這樣使用:

def load_json(filePath):
    data = open(filePath, 'r').read()
    return json.loads(data)


docMapping = load_json("./student.json")
# print(docMapping)


#dropIndex(client, indexName, result)
ret = checkIndexByName(client, indexName)
if not ret:
    print("\nindex is exsit = %d" % ret)
    createIndex(client, indexName, docMapping)

如果沒有索引,則創(chuàng)建一下;

在kibana的開發(fā)工具中可以看到相關(guān)的結(jié)果:

python使用ElasticSearch7.17.6筆記

之后, 添加2個記錄(文檔)試試

doc = {

    "name": "灰太狼",
    "age": 22,
    "birthday": "2000-02-02",
    "tags": ["男"]

}
res = addDoc(client, indexName, doc, 13810500001)
# print(res)

doc = {

    "name": "美羊羊",
    "age": 10,
    "birthday": "2010-01-01",
    "tags": ["女"]

}
res = addDoc(client, indexName, doc, 13810500002)
# print(res)

可以在kibana中看到:

python使用ElasticSearch7.17.6筆記

python使用ElasticSearch7.17.6筆記?

?目前位置,基本的增刪改查,都實現(xiàn)了,但是還需要復(fù)雜的查詢:

bodyQueryAll = {
    "query": {
        "match_all": {}
    }
}
res = client.search(index=indexName, query=bodyQueryAll["query"])
print("查詢到%d 個" % res['hits']['total']['value'])

items = res["hits"]["hits"]
# print(items)
for item in items:
    print("index=%s, id=%s doc=%s" %
          (item['_index'], item['_id'], item['_source']))
查詢到2 個
index=student, id=13810501001 doc={'name': '灰太狼', 'age': 22, 'birthday': '2000-02-02', 'tags': ['男']}
index=student, id=13810501002 doc={'name': '美羊羊', 'age': 21, 'birthday': '2000-01-01', 'tags': ['女']}

在kibana中,是這樣的:

python使用ElasticSearch7.17.6筆記

?

知道了查詢后返回數(shù)據(jù)的結(jié)構(gòu)了,就可以提取我們想要的數(shù)據(jù)了,

再添加2個查詢函數(shù):

def queryAll(client, indexName):
    bodyQueryAll = {
        "query": {
            "match_all": {}
        }
    }
    res = client.search(index=indexName, query=bodyQueryAll["query"])
    n = res['hits']['total']['value']
    #print("查詢到%d 個" % n)

    items = res["hits"]["hits"]
    # print(items)
    # for item in items:
    #     print("index=%s, id=%s doc=%s" %
    #           (item['_index'], item['_id'], item['_source']))
    return (n, items)


def queryByDoc(client, indexName, query):
    res = client.search(index=indexName, query=query)
    n = res['hits']['total']['value']
    items = res["hits"]["hits"]
    return (n, items)

測試代碼如下:

print("查全量:")
res = queryAll(client, indexName)
n = res[0]
items = res[1]
# print(items)
for item in items:
    print("index=%s, id=%s doc=%s" %
          (item['_index'], item['_id'], item['_source']))


queryNames = {
    "bool":
    {
        "should": [
            {"match":
                {"name": "美羊羊"}
             },
            {
                "match": {"name": "喜羊羊"}
            }
        ]
    }
}

print("查名字:")
res = queryByDoc(client, indexName, queryNames)
n = res[0]
items = res[1]
# print(items)
for item in items:
    print("index=%s, id=%s doc=%s" %
          (item['_index'], item['_id'], item['_source']))

輸出:

查全量:
index=student, id=13810501001 doc={'name': '灰太狼', 'age': 22, 'birthday': '2000-02-02', 'tags': ['男']}
index=student, id=13810501002 doc={'name': '美羊羊', 'age': 21, 'birthday': '2000-01-01', 'tags': ['女']}
查名字:
index=student, id=13810501002 doc={'name': '美羊羊', 'age': 21, 'birthday': '2000-01-01', 'tags': ['女']}

kibana中這樣的:

python使用ElasticSearch7.17.6筆記

?

參考:python操作Elasticsearch7.x - lshan - 博客園

Elasticsearch API Reference — Python Elasticsearch client 8.4.1 documentation

python操作Elasticsearch7.17.0_有勇氣的牛排的博客-CSDN博客

https://github.com/elastic/elasticsearch-py/issues/1698

elasticsearch——入門 - 走看看文章來源地址http://www.zghlxwxcb.cn/news/detail-469469.html

到了這里,關(guān)于python使用ElasticSearch7.17.6筆記的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • elasticsearch7.17.3 實現(xiàn)類似mysql的like查詢

    前言:現(xiàn)在想要實現(xiàn)在elasticsearch中類似于mysql的like查詢方式,有下面幾種方法可以參考 建議 :wildcard方法是純純的like查詢方式平替,但是性能差,上百GB的數(shù)據(jù)量后就會很慢。根據(jù)自己業(yè)務(wù)量需求,前面兩種方式能解決的情況下盡量用前面兩種方式。前兩種方式可以修改索

    2024年02月08日
    瀏覽(26)
  • 【軟件安裝教程】elasticsearch7.17安裝設(shè)置用戶名和密碼

    【軟件安裝教程】elasticsearch7.17安裝設(shè)置用戶名和密碼

    鏈接:https://pan.baidu.com/s/1f3jTc4kaQvu_i9pVbuXdPQ? 提取碼:l3g1? 下載后解壓? ? bin :存入一些二進制腳本,包括啟動 ES、安裝插件命令等都在這里。 config :存放 ES 的配置文件,主要是 elasticsearch.yml 。 jdk :ES 自帶的 Java 環(huán)境,所以我們無需自己安裝 Java 環(huán)境。 lib :存放 ES 相關(guān)

    2024年02月13日
    瀏覽(21)
  • Visual Studio 2022 v17.6 正式發(fā)布

    Visual Studio 2022 v17.6 正式發(fā)布

    Visual Studio 17.6 正式發(fā)布,這個最新版本提供了一系列強大的工具和功能,旨在使你能夠制作出最先進的應(yīng)用程序。 通過 Visual Studio 2022,目標(biāo)是幫助你在更短的時間內(nèi)完成 IDE 內(nèi)的所有開發(fā)任務(wù),在這個版本中,微軟根據(jù)開發(fā)者的反饋改進了幾個核心體驗的性能。 性能改進

    2024年02月06日
    瀏覽(22)
  • Elasticsearch7學(xué)習(xí)筆記(尚硅谷)

    Elasticsearch7學(xué)習(xí)筆記(尚硅谷)

    Elasticsearch是一個 實時的分布式搜索和分析引擎 。它可以幫助你用前所未有的速度去處理大規(guī)模數(shù)據(jù)。它可以用于 全文搜索,結(jié)構(gòu)化搜索以及分析 ,當(dāng)然你也可以將這三者進行組合。 Elasticsearch是一個建 立在全文搜索引擎 Apache Lucene? 基礎(chǔ) 上的搜索引擎,可以說Lucene是當(dāng)今

    2024年01月23日
    瀏覽(28)
  • ElasticSearch7.6.x 學(xué)習(xí)筆記

    ElasticSearch7.6.x 學(xué)習(xí)筆記

    ElasticSearch,簡稱es,es是一個 開源 的 高擴展 的 分布式全文檢索引擎 ,它可以近乎實時的存儲、檢索數(shù)據(jù)。且本身擴展性很好,可以擴展到上百臺服務(wù)器,處理PB級別的數(shù)據(jù)。es也使用java開發(fā)并使用Lucene作為核心來實現(xiàn)所用索引和搜索的功能。但是,它的目的是通過簡單的

    2023年04月16日
    瀏覽(23)
  • Visual Studio 2022 版本 17.6 預(yù)覽版發(fā)行版小范圍更新,值得你一看

    Visual Studio 2022 版本 17.6 預(yù)覽版發(fā)行版小范圍更新,值得你一看

    寫在前面: ? ? ??Visual Studio 2022 v17.6 是vs2022 v17.6小版本的一個更新,此版本拓展了 vs2022 v17.5版本的功能,旨在提高您的工作效率,無論你是 .NET 開發(fā)人員、游戲開發(fā)人員和/或C++開發(fā)人員 此博客總結(jié)了此版本中的主要更新和修改。 目錄 集成開發(fā)環(huán)境生產(chǎn)力 大括號對著色

    2024年02月09日
    瀏覽(44)
  • 本地部署Canal筆記-實現(xiàn)MySQL與ElasticSearch7數(shù)據(jù)同步

    本地部署Canal筆記-實現(xiàn)MySQL與ElasticSearch7數(shù)據(jù)同步

    本地搭建canal實現(xiàn)mysql數(shù)據(jù)到es的簡單的數(shù)據(jù)同步,僅供學(xué)習(xí)參考 建議首先熟悉一下canal同步方式:https://github.com/alibaba/canal/wiki 本地搭建MySQL數(shù)據(jù)庫 本地搭建ElasticSearch 本地搭建canal-server 本地搭建canal-adapter 本地環(huán)境為window11,大部分組件采用docker進行部署,MySQL采用8.0.27, 推薦

    2024年02月02日
    瀏覽(96)
  • elasticsearch7基礎(chǔ)用法及java中使用

    elasticsearch7基礎(chǔ)用法及java中使用

    GET、POST、PUT、DELETE、HEAD。 id content 1001 my name is zhang san 1002 i name is li si 1003 my name is wang wu keyword id name 1001,1002,1003 zhang 1001a my 1001,1003 PUT請求:http://127.0.0.1:9200/索引名稱 返回值: 表頭 含義 health 當(dāng)前服務(wù)器健康狀態(tài): green (集群完整) yellow (單點正常、集群不完整) red(單點不正

    2024年02月03日
    瀏覽(53)
  • 離線安裝Elasticsearch7.15.1集群(使用內(nèi)置jdk)

    背景: 以192.168.50.210、192.168.50.211、192.168.50.212這三臺機器為例,進行相關(guān)的配置 而我本地的jdk是1.8的,已經(jīng)不符合要求了。但項目中沒有那么高版本的jdk,也只想用1.8版本的,只是es用自己內(nèi)置的jdk而已。 ES安裝 1.下載相應(yīng)的包文件 https://www.elastic.co/cn/downloads/elasticsearch 2.解

    2024年02月16日
    瀏覽(20)
  • springboot集成Elasticsearch7.16,使用https方式連接并忽略SSL證書

    千萬萬苦利用科學(xué)上網(wǎng)找到了,記錄一下

    2024年02月09日
    瀏覽(25)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包