- 分别安装elasticsearch elasticsearch-head
- linxu安装elasticsearch
- 访问elasticSearch官网地址 https://www.elastic.co/
- 选择7.12.1下载,上传到服务器
- tar -zxvf elasticsearch-*******
- 进入到config文件夹中,修改elasticsearch.yml
<pre data-language="xml" id="vaB3w" class="ne-codeblock language-xml" style="border: 1px solid #e8e8e8; border-radius: 2px; background: #f9f9f9; padding: 16px; font-size: 13px; color: #595959">node.name: node-1
开启跨域访问支持,默认为false
http.cors.enabled: true
# 跨域访问允许的域名地址
http.cors.allow-origin: "*"
# 通过为 cluster.initial_master_nodes 参数设置符合主节点条件的节点的 IP 地址来引导启动集群
cluster.initial_master_nodes: ["node-1"]
network.host: 0.0.0.0</pre>
- 此操作都是root用户,现在需要创建一个普通用户例如mix_usr
- useradd mix_usr
- passwd 123
- 再输入两次密码(自定义)
- chown -R testuser:testuser /usr/local/elasticsearch 更改文件分组, /usr/local/自己的elasticsearch 解压路径
- vi /etc/security/limits.conf 添加如下内容
<pre data-language="xml" id="rQIiM" class="ne-codeblock language-xml" style="border: 1px solid #e8e8e8; border-radius: 2px; background: #f9f9f9; padding: 16px; font-size: 13px; color: #595959"># 在root用户下追加配置
vim /etc/security/limits.conf
配置内容 *表示所有用户生效
- soft nofile 65536
- hard nofile 65536
重新登录即可生效
可使用命令查看是否生效
ulimit -H -n</pre>
- 进入elasticsearch的bin目录 运行启动命令 ./elasticsearch 或者 后台启动命令 ./elasticsearch -d
- 浏览器访问ip:9200
- 出现json内容证明安装完毕
<pre data-language="json" id="X4Ky4" class="ne-codeblock language-json" style="border: 1px solid #e8e8e8; border-radius: 2px; background: #f9f9f9; padding: 16px; font-size: 13px; color: #595959">{
name: "node-1",
cluster_name: "elasticsearch",
cluster_uuid: "RvqWGNf2TEe5RqnxPKOQwA",
version: {
number: "7.12.1",
build_flavor: "default",
build_type: "tar",
build_hash: "3186837139b9c6b6d23c3200870651f10d3343b7",
build_date: "2021-04-20T20:56:39.040728659Z",
build_snapshot: false,
lucene_version: "8.8.0",
minimum_wire_compatibility_version: "6.8.0",
minimum_index_compatibility_version: "6.0.0-beta1"
},
tagline: "You Know, for Search"
}</pre>
- 安装elasticsearch-head插件,需要nodejs等 用到的npm
<pre data-language="json" id="ibjfl" class="ne-codeblock language-json" style="border: 1px solid #e8e8e8; border-radius: 2px; background: #f9f9f9; padding: 16px; font-size: 13px; color: #595959">yum install –y git
git clone git://github.com/mobz/elasticsearch-head.git cd elasticsearch-head
npm install -g grunt-cli
npm install -g grunt
npm install grunt-contrib-copy
npm install grunt-contrib-concat
npm install grunt-contrib-uglify
npm install grunt-css
npm install
npm run start启动或者后台启动 nohup npm run start &</pre>
- 安装完成后需要重新启动elasticsearch
- 浏览器访问ip:9100,可以看到插件界面.红色处注意改成自己的ip地址,默认为localhost
-
- image
- 分词器安装 进入到/bin文件夹下执行命令
<pre data-language="json" id="hJ45h" class="ne-codeblock language-json" style="border: 1px solid #e8e8e8; border-radius: 2px; background: #f9f9f9; padding: 16px; font-size: 13px; color: #595959">/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.12.1/elasticsearch-analysis-ik-7.12.1.zip</pre>
安装分词器的作用,在于搜索的时候,例如搜索: "中国人",那么有分词器就会去全文检索中找"中国"或者"人"的两个词,如果没有分词器,那么只会找"中国人" 这个词
- 安装完成后需要重启elasticsearch
- 查看进程 ps -ef|grep elasticsearch
- kill -9 端口号 杀死进程
- 执行启动命令 ./elasticsearch
- 具体代码demo在 项目test中
- 测试类 ElasticSearchTest
- 具体实际使用需要elasticsearch的初始化配置,具体配置在 ElasticSearchConfig 示例代码在 ElasticsearchImpl
- 官网dsl语句参考地址: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html#query-dsl
- 倒排索引,正向索引就是,在每个文档中,文档id与 关键词出现的次数以及位置列表的对应关系
- 倒排索引则是 转换了另一种格式,变为 关键词与文档id的对应关系
- image
- 这就是一个正向索引,搜索词汇1和词汇4
- 结果为 文档1 : 2 次 集合为: 0,3;文档4 : 1 次 集合为: 0 ;文档5 : 1 次 集合为: 3
- 倒排索引后变为, 词汇1: 文档1的id,文档4的id; 词汇4: 文档1的id,文档5的id
- elasticsearch查询,都要进行倒排索引
- 全文检索: 利用分词器进行分词,然后查询 match(单字段查询)/multi_match(多字段查询)
- 精确查询: 例如keyword,数值,日期,boolean类型,无需分词,进行查询; ids range区间 trem数值
- 地理查询: geo,经纬度查询; geo_distance/ geo_bounding_box
- 复合查询: 将上述条件组合起来查询 bool 与或非
- 搜索示例
- https://docs.apipost.cn/preview/7511b95fdadb1c1b/ff229a63eae3b80a -- 按照title分词检索
- https://docs.apipost.cn/preview/0970d532cc8e1208/23708e60e7b12539 --无条件的搜索
- 搜索的时候,越是匹配度高的,越靠前