Rails和ElasticSearch调查备忘录
弹性搜索
需要Java 8或以上版本(下载)
# ESのインストール
$ brew install elasticsearch
# ESの起動
$ /usr/local/Cellar/elasticsearch/7.8.1/bin/elasticsearch
or
$ elasticsearch
# 動作確認
$ curl http://localhost:9200
增加插件
-
- kuromoji (分かち書き)
- ICU (文字の正規化フィルター)
$ elasticsearch-plugin install https://artifacts.elastic.co/downloads/elasticsearch-plugins/analysis-kuromoji/analysis-kuromoji-7.8.1.zip
$ elasticsearch-plugin install https://artifacts.elastic.co/downloads/elasticsearch-plugins/analysis-icu/analysis-icu-7.8.1.zip
数据结构
ES的结构与RDB相比如下(参考)。
Cluster > Node > Index( = DB) > Type(=Table) >Document( =Row)
基本用法
# index一覧
curl -X GET http://localhost:9200/_cat/indices
# index作成
curl -XPUT http://localhost:9200/インデックス名
# indexの削除
curl -XDELETE localhost:9200/インデックス名
# docの追加
curl -XPOST http://localhost:9200/インデックス名/タイプ名/ -d '{
"field_01": "hoge1",
}'
# docの取得
curl -XGET http://localhost:9200/new_index/_search
curl -H "Content-Type: application/json" -XGET localhost:9200/インデックス名/_search\?pretty -d '{"size":100}'
※?prettyで結果を整形して出力
※ -d '{"size":100}'で件数を指定しないとマックス10件しかでない。
※ jsonを送るときは「 -H "Content-Type: application/json"」が必要
# docの検索
curl -H "Content-Type: application/json" -XGET localhost:9200/インデックス名/_search\?pretty -d'
{
"query": {
"match": {
項目名: 検索値
}
}
}'
# docの追加
curl -H "Content-Type: application/json" -XPOST http://localhost:9200/インデックス名/new/ -d'{
項目名: 値
}'
嘉宝娜
如果需要的话,可以添加类似于请求构建器的功能进行开发。
# install
$ brew install kibana
$ brew info kibana
# 起動
$ kibana
# ダッシュボードにアクセス
http://localhost:5601/app/kibana#/dev_tools/console
请在中文中提供下面参考链接的同义词:
参考链接的同义表达可以是:参考资料链接、相关链接、引用链接等。
-
- searchAPI
-
- queryDSL
-
- クエリの例 SQLと比較
-
- Elasticsearch実運用時の注意点とアンチパターンまとめ
-
- Elasticsearch を検索エンジンとして利用する際のポイント
-
- メモリはデフォルトは2Gでサーバーのメモリの50%以下が目安。
- なのでサーバーとしては4Gは欲しい。参考
使用Rails连接ElasticSearch。
elasticsearch-rails gem を利用。
include Elasticsearch::Model してModel経由で操作する。
index名は環境で変わるように、index_name “articles-#{Rails.env}” と設定している。
# index作成
<Model名>.create_index
# DBのデータを全部インポートし直し
<Model名>__elasticsearch__.import
# データ検索例
<Model名>.search query:{ match_all: {}}
<Model名>.search query:{ match: {title:”hello”}}
在进行模型更改时,通过调用 Sidekiq 异步执行(使用 ArticleIndexer.perform_async)→ 任务将被排队到 Redis 中,并在后台以异步方式创建 ElasticSearch 索引(位于 models/concerns/article_searchable)。
Sidekiq – 使用Redis作为后台任务队列的处理器
為了非同步更新索引所需。
Redis將進行排隊,以非同步方式將索引傳送到ElasticSearch。
$ brew install redis
$ gem 'sidekiq'
$ bundle exec sidekiq -C config/sidekiq.yml
只需要一种选择,请将以下内容用中文进行释义:参考
关于ElasticSearch的日语插件问题
只要指定了分析器,就可以确认索引的设置。
curl -H "Content-Type: application/json" http://localhost:9200/articles-test/_mappings\?pretty
curl -H "Content-Type: application/json" http://localhost:9200/articles-test/\?pretty
确认在ES中查询如何被分割(分词 == 分词)
curl -H "Content-Type: application/json" -XGET localhost:9200/articles-test/_search\?pretty -d'
{
"query": {
"match": {
"title": "東京都"
}
}
}'
关于全文检索,首先需要明确一些基本概念。
基本上不是完全匹配。
全文搜索对于英文等语言来说,会通过空格将单词分隔并创建索引。
由于是通过分割后的单词来匹配,所以不能保证完全匹配。
「kuromoji」と「ngram」是用于这种分词的设置。相比之下,“ngram”的匹配率更高。
在ICU中,它能够将半角片假名等字符巧妙地索引为普通字符。它就像一个能够优雅地转换日文UTF-8字符的过滤器。
关于全文搜索的参考链接
请为我重新翻译以下链接的文章,只需要一份翻译:
https://dev.classmethod.jp/articles/es-02/
https://medium.com/hello-elasticsearch/elasticsearch-833a0704e44b
https://qiita.com/shin_hayata/items/41c07923dbf58f13eec4
术语基准查询(精确匹配)
filter和query的区别
当使用查询(全文搜索)时,不考虑得分来获取搜索结果。
当涉及到搜索结果的相关性很重要时,使用查询(query)会更合适。
得分是
在搜索结果中,如何确定排名?
如果一个文档中的搜索词与查询非常匹配,并且这是其他文档中很少见到的特定文档的独特搜索词,并且该文档长度较短,则匹配度很高。
ICU过滤器所提供的功能
在搜索引擎中,可以使用过滤器来进行文字的规范化处理。
安装日语插件
安装Kuromoji
/usr/local/Cellar/elasticsearch/7.8.1/bin/elasticsearch-plugin install https://artifacts.elastic.co/downloads/elasticsearch-plugins/analysis-kuromoji/analysis-kuromoji-7.8.1.zip
安装ICU
/usr/local/Cellar/elasticsearch/7.8.1/bin/elasticsearch-plugin install https://artifacts.elastic.co/downloads/elasticsearch-plugins/analysis-icu/analysis-icu-7.8.1.zip
在使用Rails中的配置时,请参考以下内容。
https://qiita.com/chase0213/items/381b1eeacb849d93ecfd
https://qiita.com/yamashun/items/e1f2157e1b3cf3a716e3
以下是两个链接:
1. https://qiita.com/chase0213/items/381b1eeacb849d93ecfd
2. https://qiita.com/yamashun/items/e1f2157e1b3cf3a716e3