当前位置: 首页 > news >正文

elasticsearch的docker安装与使用

安装

docker network create elasticdocker pull docker.elastic.co/elasticsearch/elasticsearch:8.10.4# 增加虚拟内存, 此处适用于linux
vim /etc/sysctl.conf # 添加 vm.max_map_count=262144
# 重新启动
sysctl vm.max_map_countdocker run --name es01 --net elastic -p 9200:9200 -it -m 1GB docker.elastic.co/elasticsearch/elasticsearch:8.10.4

成功后会出现下图
在这里插入图片描述
将密码保存,可以先重置:

docker exec -it es01 /usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic
docker exec -it es01 /usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s kibana

复制证书以供ssl访问

docker cp es01:/usr/share/elasticsearch/config/certs/http_ca.crt .

使用

connect

from elasticsearch import ElasticsearchNODES = ["https://localhost:9200","https://localhost:9201","https://localhost:9202",
]# Password for the 'elastic' user generated by Elasticsearch
ELASTIC_PASSWORD = "<password>"# Create the client instance
client = Elasticsearch(NODES,ca_certs="/path/to/http_ca.crt",basic_auth=("elastic", ELASTIC_PASSWORD)# 支持多种登陆方式# api_key=("api_key.id", "api_key.api_key")# bearer_auth="token-value"# ssl_assert_fingerprint=CERT_FINGERPRINT,
)# Successful response!
client.info()

index

mapping = {"settings": {"analysis": {"analyzer": {"latex_analyzer": {"type": "standard",  # 使用标准分析器作为基础"stopwords": "_none_"  # 不使用停用词}}}},"mappings": {"properties": {"question": {"type": "text","analyzer": "latex_analyzer"  # 使用上面定义的分析器}}}
}# 创建索引
es.indices.create(index="questions_index", body=mapping)

insert

from elasticsearch import Elasticsearch, helpersinsert_df = df["question"].to_frame()insert_df.head()def doc_generator(df, index_name):df_iter = df.iterrows()for index, document in df_iter:yield {"_index": index_name,"_id": index,      "_source": document.to_dict(),}helpers.bulk(es, doc_generator(insert_df, 'questions_index'))

query

search_query = {"query": {"match": {"question": "数轴上A、B两点所表示的有理数的和是"}}
}response = es.search(index="questions_index", body=search_query)for hit in response['hits']['hits']:print(hit['_source']['question'])
import ujson# 原始查询
query_string = '''{"query": {"bool": {"must": [{"bool": {"must": [{"bool": {"should": [{"multi_match": {"query": "数轴上A、B两点所表示的有理数的和是","fields": ["question^3", "question.raw^3", "question.search^1", "question.autosuggest^1", "question.english^1"],"type": "cross_fields","operator": "and"}},{"multi_match": {"query": "数轴上A、B两点所表示的有理数的和是","fields": ["question^3", "question.raw^3", "question.search^1", "question.autosuggest^1", "question.english^1"],"type": "phrase","operator": "and"}},{"multi_match": {"query": "数轴上A、B两点所表示的有理数的和是","fields": ["question^3", "question.raw^3", "question.english^1"],"type": "phrase_prefix","operator": "and"}}],"minimum_should_match": "1"}}]}}]}}
}'''query_dict = ujson.loads(query_string)
response = es.search(index='questions_index', body=query_dict)for hit in response['hits']['hits']:print(hit['_source'])
http://www.lryc.cn/news/199058.html

相关文章:

  • ELK 单机安装
  • 优雅而高效的JavaScript——?? 运算符、?. 运算符和 ?. 运算符
  • Nginx配置负载均衡
  • Ubuntu 20.04 上安装 neo4j
  • 大规模爬虫系统面临的主要挑战及解决思路
  • 统计学习方法 感知机
  • Linux命令(103)之wc
  • 京东店铺公司名爬虫
  • 如何解决不同浏览器的样式兼容性问题?
  • C++ 中迭代器的使用
  • 如何使用BERT生成单词嵌入?
  • 第三章 内存管理 十一、虚拟内存的基本概念
  • web前端面试-- http的各个版本的区别(HTTP/0.9、HTTP/1.0、HTTP/1.1、HTTP/2.0、HTTP/3.0)
  • 统计学习方法 隐马尔可夫模型
  • Cypress 与 Selenium WebDriver
  • Leetcode 第 365 场周赛题解
  • 什么是软件测试? 软件测试都有什么岗位 ?软件测试和调试的区别? 软件测试和开发的区别?软件测试等相关概念入门篇
  • VI/VIM的使用
  • 【虹科干货】Redis Enterprise vs ElastiCache——如何选择缓存解决方案?
  • 2.2.2 交换机间相同vlan的通信
  • C的魅力在于指针
  • 【Linux常用命令14】Linux系统监控常用命令
  • Python Watchdog:高效的文件系统监控
  • C++中多态的原理【精华】
  • 亿赛通电子文档安全管理系统 Update.jsp SQL注入
  • 神经网络中的反向传播:综合指南
  • 协同创新、奔赴未来——“华为云杯”2023人工智能创新应用大赛华丽谢幕
  • 介绍Node.js中fs模块 代码和注释。
  • 【QT 读取JSON】 深入浅出 使用QT内置的QJson模块解析Json文件 匠心之作
  • 初识javaweb2 tomcat