记一下 Centos7 安装 Elasticsearch 笔记及碰到的坑
安装
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
sudo nano /etc/yum.repos.d/elasticsearch.repo
插入下面代码,保存
[elasticsearch]
name=Elasticsearch repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=0
autorefresh=1
type=rpm-md
yum 安装
sudo yum install --enablerepo=elasticsearch elasticsearch
启动
设置开机自启
sudo systemctl enable elasticsearch.service
启动
sudo service elasticsearch start
踩坑
启动报错
启动的时候,报错超时,查了一下可能要配置一下内存
sudo nano /etc/elasticsearch/jvm.options
配置内存,保存文件
-Xms512m
-Xmx512m
数据重复导入
6 万条数据批量导入的时候,数据里面明明没有重复项,查询的时候发现有重复项。
经过排查,发现可能是数据量过大,超时之后重新又写入了一遍,解决方法很简单,数据切片再导入,或者提高机子的性能
var i,
j,
temporary,
chunk = 10;
for (i = 0, j = array.length; i < j; i += chunk) {
temporary = array.slice(i, i + chunk);
// do whatever
}
常见命令
启停
sudo systemctl start elasticsearch.service
sudo systemctl stop elasticsearch.service
删除整个索引
curl -X DELETE 'http://localhost:9200/bible'
查询字段
curl -X GET "localhost:9200/bible/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"match": {
"verseID": "GN1_1"
}
}
}
'
查看映射
curl -X GET "localhost:9200/bible/_mapping?pretty"
Nodejs Api
Elastic Search Index and Mapping in node JS
查找
client
.search({
index: 'users',
type: 'staff',
body: {
query: {
match: {
body: 'firstname',
},
},
},
})
.then((res) => console.log(res))
.catch((err) => console.log(err));
添加
client.indices.exists({ index: 'users' }, (err, res, status) => {
if (res) {
console.log('index already exists');
} else {
client.indices.create({ index: 'users' }, (err, res, status) => {
console.log(err, res, status);
});
}
});
带映射的添加
async function putMapping () {
console.log("Creating Mapping index");
client.indices.putMapping({
index: 'users',
type: 'staff',
body: {
properties: {
firstname: { type: 'text' },
lastname: { type: 'text' },
email: { type: 'text' },
phone_number: { type: 'text' },
created_on: { type: 'date' },
updated_at: { type: 'date' } }
}
}, (err,resp, status) => {
if (err) {
console.error(err, status);
}
else {
console.log('Successfully Created Index', status, resp);
}
});
}
优化
中文分词
安装和使用方法上面都有
后记
通过查资料解决的问题,链接如下