一、前期准备
1、配置elasticsearch允许跨域(elasticsearch安装目录->config->elasticsearch.yml): http.cors.enabled: true http.cors.allow-origin: "*" 2、可视化工具elasticsearch-head-master安装目录运行cmd命令: npm i -g grunt-cli npm i . grunt server
二、入门操作
1、新建索引库 PUT localhost:9200/blog Content-Type: application/json { "settings":{ "index":{ "number_of_shards":"5", "number_of_replicas":"1" } }, "mappings":{ "properties":{ "id":{ "type":"long", "store":true }, "title":{ "type":"text", "store":true, "analyzer":"standard" }, "content":{ "type":"text", "store":true, "analyzer":"standard" } } } } 2、删除索引库 DELETE localhost:9200/blog Content-Type: application/json 3、根据id添加或修改文档 POST localhost:9200/blog/_doc/1 Content-Type: application/json { "id":1, "title":"新建的文档", "content":"新建的文档内容新建的文档内容" } 4、根据id删除文档 DELETE localhost:9200/blog/_doc/1 Content-Type: application/json 5、根据id查询文档 GET localhost:9200/blog/_doc/1 6、根据关键词查询文档 POST localhost:9200/blog/_doc/_search Content-Type: application/json { "query":{ "term":{ "content":"建" } } } 7、根据分词查询文档 POST localhost:9200/blog/_doc/_search Content-Type: application/json { "query":{ "query_string":{ "default_field":"title", "query":"哈建" } } } 8、查看分词效果 POST localhost:9200/_analyze Content-Type: application/json { "analyzer":"standard", "text":"这是一段中文" }
三、中文分词
1、下载,下载地址:https://github.com/medcl/elasticsearch-analysis-ik/archive/master.zip 2、解压,在安装目录运行cmd命令:mvn package 3、将安装目录/target/releases/elasticsearch-analysis-ik-*.zip文件解压后移至elasticsearct安装目录/plugins/,并重命名ik
4、进入ik目录,查看plugin-descriptor.properties文件elasticsearch.version所兼容的elasticsearch版本 5、重启elasticsearch,测试分词效果(ik_smart最少切分、ik_max_word最细粒度划分): POST localhost:9200/_analyze Content-Type: application/json { "analyzer":"ik_smart", "text":"这是一段中文" }
四、集群
1、搭建前要先删除安装目录下的data目录
2、配置安装目录/config/elasticsearch.yml :
#节点的配置信息: #集群名称,保证唯一 cluster.name: my-elasticsearch #节点名称,必须不一样 node.name: node-1 #必须为本机的ip地址 network.host: 127.0.0.1 #服务端口号,在同一机器下必须不一样 http.port: 9201 #集群间通信端口号,在同一机器下必须不一样 transport.tcp.port: 9301 #设置集群自动发现机器ip集合 discovery.zen.ping.unicast.hosts: ["127.0.0.1:9301","127.0.0.1:9302","127.0.0.1:9303"] #指定master节点 cluster.initial_master_nodes: ["127.0.0.1:9301"]