笔记---elasticsearch学习
1 安装
使用的系统是window10下Ubuntu18.04子系统
1.1 java环境安装
# 更新软件包列表:
sudo apt-get update
# 安装openjdk-8-jdk
sudo apt-get install openjdk-8-jdk
# 查看java版本,看看是否安装成功
java -version
1.2 Elasticsearch安装
官网安装连接:
https://www.elastic.co/guide/en/elastic-stack/current/installing-elastic-stack.html
https://www.elastic.co/guide/en/elasticsearch/reference/7.13/install-elasticsearch.html
使用的是targz包的安装方式:
https://www.elastic.co/guide/en/elasticsearch/reference/7.13/targz.html
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.13.2-linux-x86_64.tar.gz
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.13.2-linux-x86_64.tar.gz.sha512
shasum -a 512 -c elasticsearch-7.13.2-linux-x86_64.tar.gz.sha512
# output: elasticsearch-{version}-linux-x86_64.tar.gz: OK
tar -xzf elasticsearch-7.13.2-linux-x86_64.tar.gz
# $ES_HOME
cd elasticsearch-7.13.2/
# 启动服务
./bin/elasticsearch
# localhost:9200
# 以守护进程方式运行
./bin/elasticsearch -d -p pid
# 关闭服务
pkill -F pid
2 elasticsearch-head可视化前端
2.1 安装
下载链接:https://github.com/mobz/elasticsearch-head/
# 下载链接:https://github.com/mobz/elasticsearch-head/
# git clone项目到本地
# github地址
git clone git://github.com/mobz/elasticsearch-head.git
# gitee地址(在gitee上搜索很多)
git clone https://gitee.com/cyberton/elasticsearch-head.git
cd elasticsearch-head
npm install
# 国内加速 npm install --registry=https://registry.npm.taobao.org
npm run start
# 访问
http://localhost:9100/
2.2 配置文件修改
"…\elasticsearch\config\ " 下的 elasticsearch.yml 文件,在文件末尾添加如下代码:
# 解决跨域问题
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-methods: OPTIONS, HEAD, GET, POST, PUT, DELETE
http.cors.allow-headers: "X-Requested-With, Content-Type, Content-Length, X-User"
# 解决同一局域网或外网访问
# 添加
http.host: 0.0.0.0
# 修改
network.host: 127.0.0.1
参考链接:
https://blog.csdn.net/ymd8005/article/details/70676798
https://blog.csdn.net/majunzhu/article/details/100105290
3 kibana使用
3.1 安装
官网下载: https://www.elastic.co/cn/kibana
# 官网下载 https://www.elastic.co/cn/kibana
# 切忌:kibana版本要和Es一致
#解压后,找到bin目录,同样在终端中直接运行 <kibana>文件
#访问5601端口,进行使用
http://localhost:5601
3.2 汉化
修改配置文件夹config中的kibana.yml文件
#i18n.locale: "en"
i18n.locale: "zh-CN"
# 使同一局域网内可以被访问到,修改#server.host: "localhost"为
server.host: "0.0.0.0"
4 Elasticsearch在Rails项目的集成
Rails项目的版本信息:ruby:2.5.1,rails:6.0.4.6
4.1 安装gem
gem install elasticsearch-model
gem install elasticsearch-rails
# 或者在Gemfile中添加
gem 'elasticsearch-model', github: 'elastic/elasticsearch-rails', branch: '5.x'
gem 'elasticsearch-rails', github: 'elastic/elasticsearch-rails', branch: '5.x'
4.2 配置文件
在项目根目录的config/initializers/下新建elasticsearch.rb文件
elasticsearch_url对应的是上面的es服务地址
# Configure the Elasticsearch client to connect to your Elasticsearch server
Elasticsearch::Model.client = Elasticsearch::Client.new(url: elasticsearch_url, user: 'username', password: '123456')
# user和password可不配置
在项目根目录的config/下新建elasticsearch.yml文件
production:
index:
analysis:
analyzer:
ik_analyzer:
type: custom
tokenizer: ik_max_word
filter: [ik_smart]
filter:
ik_smart:
type: ik_smart
# development:
# index:
# analysis:
# analyzer:
# ik_analyzer:
# type: custom
# tokenizer: ik_max_word
# filter: [ik_smart]
# filter:
# ik_smart:
# type: ik_smart
4.3 在model对象中集成
require 'elasticsearch/model'
class Article < ActiveRecord::Base
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
# Index name
index_name "articles_#{Rails.env}"
# Define the mapping (schema) for Elasticsearch
mapping dynamic: false do
indexes :id, type: 'integer'
indexes :title, type: 'text' ,analyzer: 'ik_max_word',search_analyzer: "ik_smart"
indexes :title_keyword, type: 'keyword' # 添加一个不分词的字段
indexes :content, type: 'text' ,analyzer: 'ik_max_word',search_analyzer: "ik_smart"
indexes :company, type: 'keyword'
indexes :branch, type: 'keyword'
end
# Define how the model is serialized for Elasticsearch
def as_indexed_json(options = {})
{
id: id,
title: title,
title_keyword: title,
content: content,
company: company,
branch: branch,
}
end
# Function to reindex all users
def self.reindex_all
#User.find_each { |user| user.__elasticsearch__.index_document }
Article.__elasticsearch__.delete_index! # 删除原有的索引
Article.__elasticsearch__.create_index! # 创建新的索引
Article.import # 导入数据到新的索引
end
# Callback to automatically reindex the document when it's saved
after_commit on: [:create] do
if self.persisted?
__elasticsearch__.index_document
end
end
after_commit on: [:update] do
if self.persisted?
__elasticsearch__.update_document
end
end
after_commit on: [:destroy] do
if self.persisted?
__elasticsearch__.delete_document
end
end
# Function to search users by key
def self.search_by_key(key, min_score = 1,score_mode="sum")
__elasticsearch__.search(
{
query: {
function_score: {
query: {
multi_match: {
query: key,
fields: ['title_keyword^11', 'title^7', 'content^5', 'company^2', 'branch^1'],
}
},
functions: [
{
filter: { term: { title_keyword: key } }, # 匹配 name_keyword 的文档
weight: 10, # 调整匹配到 name_keyword 的文档得分更高
},
{
filter: { term: { title: key } }, # 匹配 name 的文档
weight: 5, # 调整匹配到 name 的文档得分较高
},
],
score_mode: score_mode # 可以根据需要选择其他分数模式,如"avg"或"max"
}
},
_source: false, # 禁用文档内容返回,只返回ID,
min_score: min_score # 添加 min_score 参数
}
)
end
# Function to analyze a given text using the specified analyzer
def self.analyze_text(text, analyzer)
client = __elasticsearch__.client
response = client.indices.analyze(
body: {
text: text,
analyzer: analyzer
}
)
tokens = response["tokens"].map { |token| token["token"] }
return tokens
end
end
4.4 调用
articles = Article
articles = articles.search_by_key(params[:search]).records params[:search].present?
articles
5 笔记
学习笔记&问题记录
未完待续…