下载并启动
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.0.0-darwin-x86_64.tar.gz
tar -xzv elasticsearch-7.0.0-darwin-x86_64.tar.gz
cd elasticsearch-7.0.0/bin
./elasticsearch
验证启动成功
1、通过jps,可以看到Elasticserach进程id
2、Elasticserach默认的web端口是9200,通过访问url地址,可以看到返回Elasticserach的集群、版本等信息。说明已经启动成功。
curl -get http://localhost:9200
{
"name" : "xx-Pro.local",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "dItzL8pMQZSuJD4Z05GIQA",
"version" : {
"number" : "7.0.0",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "b7e28a7",
"build_date" : "2019-04-05T22:55:32.697037Z",
"build_snapshot" : false,
"lucene_version" : "8.0.0",
"minimum_wire_compatibility_version" : "6.7.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
安装中文分词
./elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.0.0/elasticsearch-analysis-ik-7.0.0.zip
安装完成后,重启Elasticsearch,就会加载插件。
Example
- create index
curl -XPUT http://localhost:9200/index
- create mapping
curl -XPOST http://localhost:9200/index/_mapping -H 'Content-Type:application/json' -d'
{
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
}
}}'
- index some doc
curl -XPOST http://localhost:9200/index/_create/1 -H 'Content-Type:application/json' -d' {"content":"美国留给伊拉克的是个烂摊子吗"} '
curl -XPOST http://localhost:9200/index/_create/2 -H 'Content-Type:application/json' -d' {"content":"公安部:各地校车将享最高路权"} '
4.query
-- 根据单个条件查询,匹配“校车”
curl -XPOST http://localhost:9200/index/_search -H 'Content-Type:application/json' -d'
{
"query" : { "match" : { "content" : "校车" }}
}
'返回
{"took":93,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":0.6931472,"hits":[{"_index":"index","_type":"_doc","_id":"2","_score":0.6931472,"_source":
{"content":"公安部:各地校车将享最高路权"}-- 根据多个条件查询 ,必需同时匹配content和title的值
curl -XPOST http://localhost:9200/index/_search -H 'Content-Type:application/json' -d'
{
"query": {
"bool": {
"must": [
{
"match": {
"content": "1111"
}
},
{
"match": {
"title": "yuefeng"
}
}
]
}
}
}'
-- 根据多个值查询,必需匹配content,可选匹配title
curl -XPOST http://localhost:9200/index/_search -H 'Content-Type:application/json' -d'
{
"query": {
"bool": {
"must":
{
"match": {
"content": "1111"
}
},
"must_not":
{
"match": {
"title": "yuef11eng"
}
}
}
}
}'
参考 : https://github.com/medcl/elasticsearch-analysis-ik