elasticsearch部署使用 docker

qued经过测试这里我们使用 2.4.4相对稳定些 因为5.几的版本会卡centos等一些原因
但是由于方便dsl的操作
这里 elasticsearch:5.4.0 elasticsearch-dsl==5.0.0 配合使用

1 拉取镜像
docker pull elasticsearch:5.4.0
docker pull mobz/elasticsearch-head:5

2 通过镜像,启动一个容器,并将9200和9300端口映射到本机

docker run -d --name es2 -p 9200:9200 -p 9300:9300 -v /home/wangji/Desktop/es/es.yml:/usr/share/elasticsearch/config/elasticsearch.yml elasticsearch:5.4.0

es.yml 文件内容
http.host: 0.0.0.0

http.cors.enabled: true
http.cors.allow-origin: “*”

验证是否安装成功?访问: http://localhost:9200/

导入json文件到es
curl -XPOST ‘localhost:9200/bank/account/_bulk?pretty’ --data-binary @accounts.json
开启head查看
docker run -d -p 9100:9100 mobz/elasticsearch-head:5

二 elasticsearch-dsl 的 CRUD

#!/usr/bin/env python

-- coding:utf8 --

from datetime import datetime
from elasticsearch_dsl import DocType, Date, Nested, Boolean, analyzer, InnerObjectWrapper, Completion, Keyword, Text, Integer

扫描二维码关注公众号,回复: 6182814 查看本文章

更多字段类型见第三百六十四节elasticsearch(搜索引擎)的mapping映射管理

from elasticsearch_dsl.connections import connections # 导入连接elasticsearch(搜索引擎)服务器方法
connections.create_connection(hosts=[‘127.0.0.1‘])
class lagouType(DocType): # 自定义一个类来继承DocType类
# Text类型需要分词,所以需要知道中文分词器,ik_max_wordwei为中文分词器
title = Text(analyzer=“ik_max_word”) # 设置,字段名称=字段类型,Text为字符串类型并且可以分词建立倒排索引
description = Text(analyzer=“ik_max_word”)
keywords = Text(analyzer=“ik_max_word”)
url = Keyword() # 设置,字段名称=字段类型,Keyword为普通字符串类型,不分词
riqi = Date() # 设置,字段名称=字段类型,Date日期类型

class Meta:                                                             # Meta是固定写法
    index = "lagou"                                                     # 设置索引名称(相当于数据库名称)
    doc_type = ‘biao‘                                                   # 设置表名称

if name == “main”: # 判断在本代码文件执行才执行里面的方法,其他页面调用的则不执行里面的方法
lagouType.init() # 生成elasticsearch(搜索引擎)的索引,表,字段等信息

新增

lagou.save() #将数据写入elasticsearch(搜索引擎)

删除
sousuo_orm = lagouType() # 实例化
sousuo_orm.get(id=1).delete() # 删除id等于1的数据
修改
sousuo_orm = lagouType() # 实例化
sousuo_orm.get(id=1).update(title=‘123456789‘) # 修改id等于1的数据
5 倒排索引概念
倒排索引源于实际应用中需要根据属性的值来查找记录,这种索引表中的每一项都包括一个属性值和具有该属性值的各记录的地址。由于不是由记录来确定属性值,而是由属性值来确定记录的位置,因而称为倒排索引(inverted index) 带有倒排索引的文件我们称为倒排索引文件,简称倒排文件(inverted file)

三 老版本使用
docker run -d --name es2 -p 9200:9200 -p 9300:9300 elasticsearch:2.4.4
安装插件,先进入容器:
docker exec -it es2 /bin/bash
进入
cd bin
ls

plugin install mobz/elasticsearch-head
/(低版本执行命令有所不同)/
plugin -install mobz/elasticsearch-head
4 访问:
http://localhost:9200/_plugin/head/

https://www.cnblogs.com/hts-technology/p/8477258.html

猜你喜欢

转载自blog.csdn.net/WJ844908240/article/details/90039360