1、创建用户,启动elasticsearch
创建组
groupadd elsearch
创建用户指定组
useradd elsearch -g elsearch
修改用户密码
passwd elsearch
改变elasticsearch目录的属主
chown -R elsearch:elsearch elasticsearch-7.6.2
切换用户
su elsearch
修改linux操作系统配置限制
1、max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
vi /etc/security/limits.conf
-
soft nofile 65535
-
hard nofile 65535
#查看可打开文件数量
ulimit -Hn
2、max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
vi /etc/sysctl.conf
vm.max_map_count=262144
sysctl -p
重启linux
shutdown -r now
修改elasticsearch配置文件
vi config/elasticsearch.yml
node.name: node-1
network.host: 0.0.0.0
cluster.initial_master_nodes: [“node-1”]
重启elasticsearch
./elasticsearch -d
使用elsticseach-head查看
git clone git://github.com/mobz/elasticsearch-head.git
npm install
简单测试
http://192.168.163.71:9200/_cluster/health?pretty
curl -H ‘Content-Type: application/json’ -XPUT ‘http://192.168.163.71:9200/store/books/1’ -d ‘{
“title”: “Elasticsearch: The Definitive Guide”,
“name” : {
“first” : “Zachary”,
“last” : “Tong”
},
“publish_date”:“2015-02-06”,
“price”:“49.99”
}’
curl -XGET ‘http://192.168.163.71:9200/store/books/1’
curl -XGET ‘http://192.168.163.71:9200/store/books/1?_source=title,price’
#可以通过覆盖的方式更新
curl -H ‘Content-Type: application/json’ -XPUT ‘http://192.168.163.71:9200/store/books/1’ -d ‘{
“title”: “Elasticsearch: The Definitive Guide”,
“name” : {
“first” : “Zachary”,
“last” : “Tong”
},
“publish_date”:“2016-02-06”,
“price”:“99.99”
}’
或者通过 _update API的方式单独更新你想要更新的
curl -H ‘Content-Type: application/json’ -XPOST ‘http://192.168.163.71:9200/store/books/1/_update’ -d ‘{
“doc”: {
“price” : 88.88
}
}’
#删除一个文档
curl -H ‘Content-Type: application/json’ -XDELETE ‘http://192.168.163.71:9200/store/books/1’
curl -H ‘Content-Type: application/json’ -XPUT ‘http://192.168.163.71:9200/store/books/4’ -d ‘{
“title”: “Elasticsearch: The Definitive Guide”,
“author”: “Guide”,
“publish_date”:“2016-02-06”,
“price”:“35.99”
}’
curl -H ‘Content-Type: application/json’ -XGET ‘http://192.168.163.71:9200/store/books/_search’ -d ‘{
“query”: {
“bool”: {
“must”: {
“match_all”: {}
},
“filter”: {
“term”: {
“price”: 35.99
}
}
}
}
}’
返回的的分是1.0
curl -H ‘Content-Type: application/json’ -XGET ‘http://192.168.163.71:9200/store/books/_search’ -d ‘{
“query”: {
“constant_score”: {
“filter”: {
“term”: {
“price”: 35.99
}
}
}
}
}’
curl -H ‘Content-Type: application/json’ -XGET ‘http://192.168.163.71:9200/store/books/_search’ -d ‘{
“query”: {
“bool”: {
“filter” : {
“term” : {
“price” : 35.99
}
}
}
}
}’
curl -H ‘Content-Type: application/json’ -XGET ‘http://192.168.163.71:9200/store/books/_search’ -d ‘{
“query” : {
“bool” : {
“filter” : {
“terms” : {
“price” : [35.99, 49.99]
}
}
}
}
}’
SELECT * FROM books WHERE publish_date = “2015-02-06”
curl -H ‘Content-Type: application/json’ -XGET ‘http://192.168.163.71:9200/store/books/_search’ -d ‘{
“query” : {
“bool” : {
“filter” : {
“term” : {
“publish_date” : “2015-02-06”
}
}
}
}
}’
bool过滤查询,可以做组合过滤查询
SELECT * FROM books WHERE (price = 35.99 OR price = 99.99) AND publish_date != “2016-02-06”
类似的,Elasticsearch也有 and, or, not这样的组合条件的查询方式
格式如下:
{
“bool” : {
“must” : [],
“should” : [],
“must_not” : [],
}
}
must: 条件必须满足,相当于 and
should: 条件可以满足也可以不满足,相当于 or
must_not: 条件不需要满足,相当于 not
curl -H ‘Content-Type: application/json’ -XGET ‘http://192.168.163.71:9200/store/books/_search’ -d ‘{
“query” : {
“bool” : {
“should” : [
{ “term” : {“price” : 35.99}},
{ “term” : {“price” : 49.99}}
],
“must_not” : {
“term” : {“publish_date” : “2016-02-06”}
}
}
}
}’
嵌套查询
SELECT * FROM books WHERE price = 35.99 OR ( publish_date = “2016-02-06” AND price = 99.99 )
curl -H ‘Content-Type: application/json’ -XGET ‘http://192.168.163.71:9200/store/books/_search’ -d ‘{
“query”: {
“bool”: {
“should”: [
{
“term”: {
“price”: 35.99
}
},
{
“bool”: {
“must”: [
{
“term”: {
“publish_date”: “2016-02-06”
}
},
{
“term”: {
“price”: 99.99
}
}
]
}
}
]
}
}
}’
curl -H ‘Content-Type: application/json’ -XGET ‘http://192.168.163.71:9200/store/books/_search’ -d ‘{
“query”: {
“bool”: {
“should”: [
{
“term”: {
“price”: 35.99
}
},
{
“bool”: {
“must”: [
{
“term”: {
“publish_date”: “2016-02-06”
}
},
{
“term”: {
“price”: 99.99
}
}
]
}
}
]
}
}
}’
range范围过滤
SELECT * FROM books WHERE price >= 10 AND price < 99
gt : > 大于
lt : < 小于
gte : >= 大于等于
lte : <= 小于等于
curl -H ‘Content-Type: application/json’ -XGET ‘http://192.168.163.71:9200/store/books/_search’ -d ‘{
“query”: {
“range” : {
“price” : {
“gte” : 10,
“lt” : 99
}
}
}
}’
#name和author都必须包含Guide,并且价钱等于33.99或者188.99
curl -H ‘Content-Type: application/json’ -XGET ‘http://192.168.163.71:9200/store/books/_search’ -d ‘{
“query”:{
“bool”:{
“must”:{
“multi_match”:{
“operator”:“and”,
“fields”:[
“name”,
“author”
],
“query”:“Guide”
}
},
“filter”:[
{
“terms”:{
“price”:[
35.99,
188.99
]
}
},
{
“terms”:{
“price”:[
35.99,
188.99
]
}
}
]
}
}
}’
curl -H ‘Content-Type: application/json’ -XGET ‘http://192.168.163.71:9200/store/books/_search’ -d ‘{
“query”:{
“bool”:{
“must”:{
“multi_match”:{
“operator”:“or”,
“fields”:[
“title”,
“author”
],
“query”:“Guide”}
},
“filter”:[
{
“terms”:{
“price”:[
35.99,
49.99
]
}
},
{
“terms”:{
“price”:[
35.99,
49.99
]
}
}
]
}
}
}’
查询所有
http://192.168.163.71:9200/store/books/_search
git clone git://github.com/mobz/elasticsearch-head.git
#将源码包下载后剪切到/bigdata目录,并改所属用户和组
sudo chown -R xiaoniu:xiaoniu /bigdata/elasticsearch-head
#进入到elasticsearch-head中
cd elasticsearch-head
#编译安装
npm install
打开elasticsearch-head-master/Gruntfile.js,找到下面connect属性,新增hostname: ‘0.0.0.0’,
connect: {
server: {
options: {
hostname: ‘0.0.0.0’,
port: 9100,
base: ‘.’,
keepalive: true
}
}
}
编辑elasticsearch-5.4.3/config/elasticsearch.yml,加入以下内容:
http.cors.enabled: true
http.cors.allow-origin: “*”
#运行服务
npm run start
安装IK分词器
下载对应版本的插件
https://github.com/medcl/elasticsearch-analysis-ik/releases
首先下载es对应版本的ik分词器的zip包,上传到es服务器上,在es的安装目录下有一个plugins的目录,在这个目录下创建一个叫ik的目录
然后将解压好的内容,拷贝到ik目录
将ik目录拷贝到其他的es节点
重新启动所有的es
#创建索引名字叫news
curl -H ‘Content-Type: application/json’ -XPUT http://192.168.163.71:9200/news
https://github.com/medcl/elasticsearch-analysis-ik
#创建mapping(相当于数据中的schema信息,表名和字段名以及字段的类型)
curl -H ‘Content-Type: application/json’ -XPOST http://192.168.163.71:9200/news/_mapping -d '{
“properties”: {
“content”: {
“type”: “text”,
“analyzer”: “ik_max_word”,
“search_analyzer”: “ik_max_word”
}
}
}’
curl -XPOST http://localhost:9200/news/_create/1 -H ‘Content-Type:application/json’ -d’
{“content”:“美国留给伊拉克的是个烂摊子吗”}
’
curl -H ‘Content-Type: application/json’ -XPOST http://192.168.163.71:9200/news/_create/2 -d’
{“content”:“公安部:各地校车将享最高路权”}’
curl -H ‘Content-Type: application/json’ -XPOST http://192.168.163.71:9200/news/_create/3 -d’
{“content”:“中韩渔警冲突调查:韩警平均每天扣1艘中国渔船”}’
curl -H ‘Content-Type: application/json’ -XPOST http://192.168.163.71:9200/news/_create/4 -d’
{“content”:“中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首”}’
curl -H ‘Content-Type: application/json’ -XPOST http://192.168.163.71:9200/news/_search -d’
{
“query” : { “match” : { “content” : “中国” }},
“highlight” : {
“pre_tags” : ["", “”],
“post_tags” : ["", “”],
“fields” : {
“content” : {}
}
}
}’
curl -H ‘Content-Type: application/json’ -XGET ‘http://192.168.163.71:9200/index/_analyze?pretty&analyzer=ik_max_word’ -d ‘联想是全球最大的笔记本厂商’
curl -XGET “http://localhost:9200/index/_analyze” -H ‘Content-Type: application/json’ -d’
{
“text”:“中华人民共和国MN”,“tokenizer”: “ik_max_word”
}’
curl -XGET “http://localhost:9200/index/_analyze” -H ‘Content-Type: application/json’ -d’
{
“text”:“中华人民共和国MN”,“tokenizer”: “ik_smart”
}’