多应用+插件架构,代码干净,支持一键云编译,码云点赞13K star,4.8-4.12 预售价格198元 广告
jdk安装:https://blog.csdn.net/nxstack/article/details/54380837 ~~~ java -version rpm -qa|grep java rpm -e --nodeps tzdata-java-2012c-1.el6.noarch rpm -e --nodeps java-1.7.0-openjdk-1.7.0.45-1.45.1.11.1.el6.x86_64 mkdir /usr/local/java vi /etc/profile export JAVA_HOME=/usr/java/jdk1.7.0_71 export CLASSPATH=.:%JAVA_HOME%/lib/dt.jar:%JAVA_HOME%/lib/tools.jar export PATH=$PATH:$JAVA_HOME/bin ~~~ 1、下载解压 https://www.elastic.co/downloads/elasticsearch elasticsearch-6.2.2 中文分词: 访问http://IP:9200/,如果公网IP访问不了,则 修改配置文件 config/elasticsearch.yml network.host: 0.0.0.0 非root启动: ~~~ groupadd esg useradd -g esg esu chown -R esu:esg elasticsearch-6.2.2 ~~~ ./bin/elasticsearch启动 启动脚本: ~~~ [root@centos6 ~]# cat /etc/init.d/elastic #!/bin/sh #chkconfig: 2345 80 05 #description: elasticsearch export JAVA_HOME=/usr/local/java/jdk1.8.0_161 export JAVA_BIN=/usr/local/java/jdk1.8.0_161/bin export PATH=$PATH:$JAVA_HOME/bin export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export JAVA_HOME JAVA_BIN PATH CLASSPATH case "$1" in start) su esu<<! cd /usr/local/src/elasticsearch-6.2.2 ./bin/elasticsearch -d ! echo "elasticsearch startup" ;; stop) es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'` kill -9 $es_pid echo "elasticsearch stopped" ;; restart) es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'` kill -9 $es_pid echo "elasticsearch stopped" su esu<<! cd /usr/local/src/elasticsearch-6.2.2 ./bin/elasticsearch -d ! echo "elasticsearch startup" ;; *) echo "start|stop|restart" ;; esac exit $? [root@centos6 ~]# ~~~ 单用户启动 如果一切正常,Elastic 就会在默认的9200端口运行。这时,打开另一个命令行窗口,请求该端口,会得到说明信息。 ~~~ $ curl localhost:9200 { "name" : "atntrTf", "cluster_name" : "elasticsearch", "cluster_uuid" : "tf9250XhQ6ee4h7YI11anA", "version" : { "number" : "5.5.1", "build_hash" : "19c13d0", "build_date" : "2017-07-18T20:44:24.823Z", "build_snapshot" : false, "lucene_version" : "6.6.0" }, "tagline" : "You Know, for Search" } ~~~ 二、基本概念 2.1 Node 与 Cluster Elastic 本质上是一个分布式数据库,允许多台服务器协同工作,每台服务器可以运行多个 Elastic 实例。 单个 Elastic 实例称为一个节点(node)。一组节点构成一个集群(cluster)。 2.2 Index Elastic 会索引所有字段,经过处理后写入一个反向索引(Inverted Index)。查找数据的时候,直接查找该索引。 所以,Elastic 数据管理的顶层单位就叫做 Index(索引)。它是单个数据库的同义词。每个 Index (即数据库)的名字必须是小写。 下面的命令可以查看当前节点的所有 Index。 $ curl -X GET 'http://localhost:9200/_cat/indices?v' 2.3 Document Index 里面单条的记录称为 Document(文档)。许多条 Document 构成了一个 Index。 Document 使用 JSON 格式表示,下面是一个例子。 { "user": "张三", "title": "工程师", "desc": "数据库管理" } 同一个 Index 里面的 Document,不要求有相同的结构(scheme),但是最好保持相同,这样有利于提高搜索效率。 2.4 Type Document 可以分组,比如weather这个 Index 里面,可以按城市分组(北京和上海),也可以按气候分组(晴天和雨天)。这种分组就叫做 Type,它是虚拟的逻辑分组,用来过滤 Document。 不同的 Type 应该有相似的结构(schema),举例来说,id字段不能在这个组是字符串,在另一个组是数值。这是与关系型数据库的表的一个区别。性质完全不同的数据(比如products和logs)应该存成两个 Index,而不是一个 Index 里面的两个 Type(虽然可以做到)。 下面的命令可以列出每个 Index 所包含的 Type。 $ curl 'localhost:9200/_mapping?pretty=true' 根据规划,Elastic 6.x 版只允许每个 Index 包含一个 Type,7.x 版将会彻底移除 Type。 三、新建和删除 Index 新建 Index,可以直接向 Elastic 服务器发出 PUT 请求。下面的例子是新建一个名叫weather的 Index。 $ curl -X PUT 'localhost:9200/weather' 服务器返回一个 JSON 对象,里面的acknowledged字段表示操作成功。 { "acknowledged":true, "shards_acknowledged":true } 然后,我们发出 DELETE 请求,删除这个 Index。 $ curl -X DELETE 'localhost:9200/weather' 四、中文分词设置 首先,安装中文分词插件。这里使用的是 ik,也可以考虑其他插件(比如 smartcn)。 $ ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.5.1/elasticsearch-analysis-ik-5.5.1.zip 上面代码安装的是5.5.1版的插件,与 Elastic 5.5.1 配合使用。 接着,重新启动 Elastic,就会自动加载这个新安装的插件。 然后,新建一个 Index,指定需要分词的字段。这一步根据数据结构而异,下面的命令只针对本文。基本上,凡是需要搜索的中文字段,都要单独设置一下。 ~~~ curl -H "Content-Type: application/json" -X PUT 'localhost:9200/accounts' -d ' { "mappings": { "person": { "properties": { "user": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" }, "title": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" }, "desc": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" } } } } }' curl -H "Content-Type: application/json" -X PUT 'localhost:9200/accounts/person/1' -d ' { "user": "张三", "title": "工程师", "desc": "数据库管理" }' curl -H "Content-Type: application/json" -X POST 'localhost:9200/accounts/person' -d ' { "user": "李四", "title": "工程师", "desc": "系统管理" }' curl -H "Content-Type: application/json" -X PUT 'localhost:9200/accounts/person/1' -d ' { "user" : "张三", "title" : "工程师", "desc" : "数据库管理,软件开发" }' curl -H "Content-Type: application/json" 'localhost:9200/accounts/person/_search' -d ' { "query" : { "match" : { "desc" : "软件" }} }' curl -H "Content-Type: application/json" 'localhost:9200/accounts/person/_search' -d ' { "query" : { "match" : { "desc" : "管理" }}, "from": 1, "size": 1 }' curl -H "Content-Type: application/json" 'localhost:9200/accounts/person/_search' -d ' { "query" : { "match" : { "desc" : "软件 系统" }} }' curl -H "Content-Type: application/json" 'localhost:9200/accounts/person/_search' -d ' { "query": { "bool": { "must": [ { "match": { "desc": "开发" } }, { "match": { "desc": "库" } } ] } } }' ~~~