栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

ES的使用

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ES的使用

ES数据模型:

Index:索引,由多个document组成;索引名必须为小写

Type:索引类型

Document:文档,由多个field组成

Field:字段

1、拉取es镜像
docker pull bitnami/elasticsearch
2、创建es容器
docker run -d -p 9200:9200 -p 9300:9300 --name elasticsearch bitnami/elasticsearch
3、创建es实例
from elasticsearch import Elasticsearch
es = Elasticsearch("http://xxx.xx.xx.xx:9200/")

class ES(object):
  
    创建es 对象

    def __init__(self, index_name: str):
        self.es = es
        self.index_name = index_name

    def get_doc(self, uid):
        return self.es.get(index=self.index_name, id=uid)

    def insert_one(self, doc: dict):
        self.es.index(index=self.index_name, body=doc)

    def insert_array(self, docs: list):
        for doc in docs:
            self.es.index(index=self.index_name, body=doc)

    def search(self, query, count: int = 30, fields=None):
        fields = fields if fields else ["title", 'pub_date']
        dsl = {
            "query": {
                "multi_match": {
                    "query": query,
                    "fields": fields
                },
                
            },
            "highlight": {
                "fields": {
                    "title": {}
                }
            }
        }
        match_data = self.es.search(index=self.index_name, body=dsl, size=count)
        return match_data

    def _search(self, query: dict, count: int = 20, fields=None):  

# count: 返回的数据大小
        results = []
      
        match_data = self.search(query, count, fields)
        for hit in match_data['hits']['hits']:
            results.append(hit['_source'])
        return results

    def create_index(self):
        if self.es.indices.exists(index=self.index_name) is True:
            self.es.indices.delete(index=self.index_name)
        self.es.indices.create(index=self.index_name, ignore=400)

    def delete_index(self):
        try:
            self.es.indices.delete(index=self.index_name)
        except:

            pass

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/882079.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号