http://www.ruanyifeng.com/blog/2017/08/elasticsearch.html
https://www.cnblogs.com/ming-blogs/p/11001282.html
https://blog.csdn.net/zwgdft/article/details/54585644
ik分词
https://www.cnblogs.com/hengzhi/p/9290667.html
ik英文
http://www.itkeyword.com/doc/4850146215899145x790/IKAnalyzer-elasticSearch
https://www.cnblogs.com/ljhdo/p/5012510.html
[root@lnmp bin]# cat /root/.bashrc
export JAVA_HOME=/root/elasticsearch/jdk1.8.0_121
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
/root/elasticsearch/elasticsearch-2.3.3/bin
[root@lnmp bin]# cat elasticsearch
ES_JAVA_OPTS="-Des.insecure.allow.root=true"
/root/elasticsearch/elasticsearch-2.3.3/config
[root@lnmp config]# cat elasticsearch.yml |grep network.host
# network.host: 192.168.0.1
network.host: 0.0.0.0
一、下载与安装
Elasticsearch 依赖 java,在安装 ES 之前首先要配好 java,这个默认我们的电 脑已经完成。Elasticsearch要求jdk最低版本为1.7。
首先从 elasticsearch官网下载安装包,我们是 linux 系统,下载 tar 包比较方便。当前版本为2.3.3,下载地址:Elasticsearch 2.3.3下载
.下载完成之后解压tar文件:
tar -zxvf elasticsearch-2.3.3.tar.gz
二、运行ElasticSearch
启动ElasticSearch命令:
./elasticsearch-2.3.3/bin/elasticsearch
如果一切顺利,会收到类似于如下的信息:
[2016-06-18 15:07:50,106][INFO ][node ] [Maynard Tiboldt] version[2.3.3], pid[7390], build[218bdf1/2016-05-17T15:40:04Z]
[2016-06-18 15:07:50,106][INFO ][node ] [Maynard Tiboldt] initializing ...
[2016-06-18 15:07:50,590][INFO ][plugins ] [Maynard Tiboldt] modules [reindex, lang-expression, lang-groovy], plugins [], sites []
[2016-06-18 15:07:50,609][INFO ][env ] [Maynard Tiboldt] using [1] data paths, mounts [[/ (/dev/disk1)]], net usable_space [85.6gb], net total_space [111.8gb], spins? [unknown], types [hfs]
[2016-06-18 15:07:50,609][INFO ][env ] [Maynard Tiboldt] heap size [990.7mb], compressed ordinary object pointers [true]
[2016-06-18 15:07:50,610][WARN ][env ] [Maynard Tiboldt] max file descriptors [10240] for elasticsearch process likely too low, consider increasing to at least [65536]
[2016-06-18 15:07:52,323][INFO ][node ] [Maynard Tiboldt] initialized
[2016-06-18 15:07:52,323][INFO ][node ] [Maynard Tiboldt] starting ...
[2016-06-18 15:07:52,401][INFO ][transport ] [Maynard Tiboldt] publish_address {127.0.0.1:9300}, bound_addresses {[fe80::1]:9300}, {[::1]:9300}, {127.0.0.1:9300}
[2016-06-18 15:07:52,406][INFO ][discovery ] [Maynard Tiboldt] elasticsearch/OawW12ZERyO3CuQrt7SmFQ
[2016-06-18 15:07:55,445][INFO ][cluster.service ] [Maynard Tiboldt] new_master {Maynard Tiboldt}{OawW12ZERyO3CuQrt7SmFQ}{127.0.0.1}{127.0.0.1:9300}, reason: zen-disco-join(elected_as_master, [0] joins received)
[2016-06-18 15:07:55,456][INFO ][http ] [Maynard Tiboldt] publish_address {127.0.0.1:9200}, bound_addresses {[fe80::1]:9200}, {[::1]:9200}, {127.0.0.1:9200}
[2016-06-18 15:07:55,457][INFO ][node ] [Maynard Tiboldt] started
[2016-06-18 15:07:55,476][INFO ][gateway ] [Maynard Tiboldt] recovered [0] indices into cluster_state
这样就运行起来了一个节点。(可以多开几个终端,运行起来集群名相同的节点都 在一个集群中).
注意一下有 http 标记的那一行,它提供了有关 HTTP 地址(127.0.0.1)和 端口(9200)的信息,通过这个地址和端口我们就可以访问我们的节点了。默认情况下, Elasticsearch 使用 9200 来提供对其 REST API 的访问。
访问http://127.0.0.1:9200/,浏览器会输出如下信息:
{
"name" : "Obliterator",
"cluster_name" : "elasticsearch",
"version" : {
"number" : "2.3.3",
"build_hash" : "218bdf10790eef486ff2c41a3df5cfa32dadcfde",
"build_timestamp" : "2016-05-17T15:40:04Z",
"build_snapshot" : false,
"lucene_version" : "5.5.0" },
"tagline" : "You Know, for Search" }
如果想通过服务器ip访问es,打开elasticsearch-2.3.3/config/elasticsearch.yml,找到Network部分:
# ---------------------------------- Network ----------------------------------- # # Set the bind address to a specific IP (IPv4 or IPv6): # # network.host: 192.168.0.1 # # Set a custom port for HTTP: # # http.port: 9200 # # For more information, see the documentation at: # <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-network.html> #
把network.host: 192.168.0.1前的注释去掉并修改为network.host: 0.0.0.0
重启es上面返回的信息中 http 部分也会变成真实本机地址,可以通过本机真实ip:9200 访问es。
三、关闭ElasticSearch
需要对ES节点进行重新启动或正常关机的时候,有三种方法可以关闭ES:
- 在控制台中,使用CTRL+C组合键.
- 通过发送TERM信号终止服务器进程.
- 使用REST APIcurl -XPOST 'http://localhost:9200/_shutdown'.
四、插件安装
4.1安装head 插件
安装命令:
./elasticsearch-2.3.3/bin/plugin install mobz/elasticsearch-head
安装好后,在浏览器输入地址:http://localhost:9200/_plugin/head/ 即可调用 head 插件 查看集群状态、节点信息、做查询等等。
4.2安装IK分词器
(a).首先使用git clone命令下载IK分词器源码
git clone https://github.com/medcl/elasticsearch-analysis-ik.git
也可以直接访问github地址(https://github.com/medcl/elasticsearch-analysis-ik)点击右侧Clone or download按钮,然后Download ZIP直接下载.
(b.)解压下载的elasticsearch-analysis-ik-master.zip.
unzip elasticsearch-analysis-ik-master.zip
(c.)使用maven打包
确保系统已经安装maven,使用mvn -version命令查看是否已经安装maven.如果没有安装,可以根据系统选择安装方法,比如mac OS系统可以使用brew install maven命令完成安装.
进入ik分词器的下载目录,运行命令:
mvn package
打包完成以后可以看到根目录下多出一个target文件夹.
(d.) 配置Ik插件
在elasticsearch-2.3.3/plugins/目录下新建名为ik的文件夹.把elasticsearch-analysis-ik-master/target/releases
/elasticsearch-analysis-ik-1.9.3.zip解压,把解压后的所有文件拷贝到elasticsearch-2.3.3/plugins/ik/目录下.
重新启动es,如果配置正确,不会有异常信息输出。
(e.)ik 分词测试
1.首先创建一个索引用于测试:
curl -XPUT localhost:9200/index
2.为索引index创建mapping:
curl -XPOST http://localhost:9200/index/fulltext/_mapping -d' { "fulltext": { "_all": { "analyzer": "ik" }, "properties": { "content": { "type" : "string", "boost" : 8.0, "term_vector" : "with_positions_offsets", "analyzer" : "ik", "include_in_all" : true }
}
}
}' 3.测试:
curl 'http://localhost:9200/index/_analyze?analyzer=ik&pretty=true' -d '
{
"text":"中国有13亿人口"
}'
显示结果如下:
{
"tokens" : [ {
"token" : "中国",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0 }, {
"token" : "国有",
"start_offset" : 1,
"end_offset" : 3,
"type" : "CN_WORD",
"position" : 1 }, {
"token" : "13",
"start_offset" : 3,
"end_offset" : 5,
"type" : "ARABIC",
"position" : 2 }, {
"token" : "亿",
"start_offset" : 5,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 3 }, {
"token" : "人口",
"start_offset" : 6,
"end_offset" : 8,
"type" : "CN_WORD",
"position" : 4 } ] }
五、文档的CRUD
5.1索引、类型、文档、字段
- 索引是ElasticSearch存放数据的地方,可以理解为关系型数据库中的一个数据库。
- 类型用于区分同一个索引下不同的数据类型,相当于关系型数据库中的表
- 文档是ElasticSearch中存储的实体,类比关系型数据库,每个文档相当于数据库表中的一行数据。
-
文档由字段组成,相当于关系数据库中列的属性,不同的是ES的不同文档可以具有不同的字段集合。
对比关系型数据库:
Relational DB -> Databases -> Tables -> Rows -> Columns
Elasticsearch -> Indices -> Types -> Documents -> Fields
5.2 创建文档
以博客内容管理为例,索引名为blog,类型为article,新加一个文档:
curl -XPUT http://localhost:9200/blog/article/1 -d ' { "id": "1", "title": "New version of Elasticsearch released!", "content": "Version 1.0 released today!", "priority": 10, "tags": ["announce", "elasticsearch", "release"] }'
5.3检索文档
http://localhost:9200/blog/article/1?pretty
5.4更新文档
curl -XPOST http://localhost:9200/blog/article/1/_update -d '{ "script": "ctx._source.content = \"new content\"" }'
5.5删除文档
curl -XDELETE http://localhost:9200/blog/article/1
六、相关概念
6.1节点与集群
ElasticSearch是一个分布式全文搜索引擎,既可以做为一个独立的搜索服务器工作,也可以使用多台服务器同时运行,这样就构成了一个集群(cluster),集群的每一个服务器称为一个节点(node).
6.2分片
当数据量比较大的时候,受RAM、硬盘容量的限制,同时一个节点的计算能力有限。可以将数据切分,每部分是一个单独的lucene索引,成为分片(shard)。每个分片可以被存储在集群的不同节点上。当需要查询由多个分片构成的索引时,ElasticSearch将查询发送到每个相关的分片,之后将查询结果合并。过程对应用透明,无须知道分片的存在。
6.3副本
副本是对原始分片的一个精确拷贝,原始分片成为主分片。对索引的所有操作都直接作用在主分片上,每个主分片可以有零个或多个副分片。主分片丢失,集群可以将一个副分片提升为主的新分片。
java配置
http://www.cnblogs.com/samcn/archive/2011/03/16/1986248.html
export JAVA_HOME=/usr/share/jdk1.6.0_14
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
解决linux安装软件:/lib/ld-linux.so.2: bad ELF interpreter问题
yum install glibc.i686
http://www.111cn.net/sys/linux/55374.htm
yum install maven
wget http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /etc/yum.repos.d/epel-apache-maven.repo
yum -y install apache-maven
http://blog.csdn.net/wh211212/article/details/53080278
启动elasticsearch后会生成plugins文件夹,将elasticsearch-analysis-ik-1.9.3.zip解压后文件拷贝到 elasticsearch的plugins/ik文件夹中,重启elasticsearch
elasticsearch jdbc
http://blog.sina.com.cn/s/blog_5fd841bf0102w753.html
https://github.com/jprante/elasticsearch-jdbc
[root@localhost bin]# pwd
/home/test/elasticsearch/elasticsearch-jdbc-2.3.3.0-dist/bin
[root@localhost bin]# cat import.sh
#!/bin/sh
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
bin=${DIR}/../bin
lib=${DIR}/../lib
echo '{
"type" : "jdbc",
"jdbc" : {
"url" : "jdbc:mysql://localhost:3306/myes",
"user" : "root",
"password" : "",
"sql" : "select * from es_test",
"index" : "db_1",
"type" : "category",
"elasticsearch" : {
"cluster" : "elasticsearch",
"host" : "127.0.0.1",
"port" : 9300
}
}
}' | java \
-cp "${lib}/*" \
-Dlog4j.configurationFile=${bin}/log4j2.xml \
org.xbib.tools.Runner \
org.xbib.tools.JDBCImporter
[root@localhost bin]#
mapping
http://note.youdao.com/share/?id=f2890ad0ba672afa3015f9fee45770ac&type=note#/
curl -XPUT http://139.224.81.43:9200/test -d '
{
"settings": {
"refresh_interval": "5s",
"number_of_shards" : 1, // 一个主节点
"number_of_replicas" : 0 // 0个副本,后面可以加
},
"mappings": {
"_default_":{
"_all": { "enabled": false } // 关闭_all字段,因为我们只搜索title字段
},
"resource": {
"dynamic": false, // 关闭“动态修改索引”
"properties": {
"title": {
"type": "string",
"index": "analyzed",
"analyzer" : "smartcn"
}
}
}
}
}'
curl -XPOST 'http://localhost:9200/test/resource/1' -d '
{
"title" : "中石油"
}'
curl -XPOST 'http://localhost:9200/test/resource/2' -d '
{
"title" : "中华人民共和国"
}'
curl -XPOST 'http://localhost:9200/test/resource/3' -d '
{
"title" : "刘德华"
}'
curl -XPOST 'http://localhost:9200/test/resource/4' -d '
{
"title" : "国家"
}'
curl -XPOST 'http://localhost:9200/test/resource/4' -d '
{
"title" : "人民"
}'
curl -XPOST 'http://localhost:9200/test/resource/_search?explain&pretty=true' -d '
{
"query": {
"multi_match": {
"type": "most_fields",
"query": "中华人民共和国",
"fields" : ["title"]
}
}
}'
php5.6.6+
https://lnmp.org/install.html
sense插件
rest client
head插件
http://localhost:9200/_plugin/head/
入门
http://blog.csdn.net/napoay/article/details/51705902
基础教程
http://blog.csdn.net/cnweike/article/details/33736429
初步使用
http://www.cnblogs.com/hanyinglong/p/5409003.html
查询
https://www.elastic.co/guide/en/elasticsearch/reference/2.3/query-dsl-match-query.html
图解Elasticsearch中的_source、_all、store和index属性
http://blog.csdn.net/napoay/article/details/62233031
映射
https://es.xiaoleilu.com/052_Mapping_Analysis/45_Mapping.html
php api
https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_installation_2.html#_version_matrix
http://blog.csdn.net/xujingzhong0077/article/details/52848038
http://www.cnblogs.com/amuge/p/6072162.html
http://blog.csdn.net/zpf336/article/category/5825405
http://www.thinkphp.cn/code/1290.html
https://github.com/elastic/elasticsearch-php
composer
curl -s http://getcomposer.org/installer | php php composer.phar install --no-dev
[test@web_test composer]$ cat composer.json
{
"require": {
"elasticsearch/elasticsearch": "~2.0"
}
}
[test@web_test composer]$ ll
total 1820
-rwxrwxrwx 1 test test 68 Apr 7 19:30 composer.json
-rw-rw-r-- 1 test test 9047 Apr 7 19:49 composer.lock
-rwxrwxrwx 1 test test 1836198 Apr 6 18:47 composer.phar
-rw-r--r-- 1 test test 887 Apr 7 23:31 esphp.php
drwxrwxrwx 7 root root 4096 Apr 7 19:49 vendor
<?php
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$hosts = [
'180.150.178.240:9200', // IP + Port
//'192.168.1.2', // Just IP
//'mydomain.server.com:9201', // Domain + Port
//'mydomain2.server.com', // Just Domain
//'https://localhost', // SSL to localhost
//'https://192.168.1.3:9200' // SSL to IP + Port
];
//$client = Elasticsearch\ClientBuilder::create()->build();
$client = ClientBuilder::create() // Instantiate a new ClientBuilder
->setHosts($hosts) // Set the hosts
->build(); // Build the client object
$params = [
'index' => 'dh7654',
'type' => 'my_type',
'id' => 'my_id',
'body' => ['testField' => 'abc']
];
$response = $client->index($params);
echo "<pre>";
print_r($response);
echo "</pre>";
[test@web_test composer]$
thinkphp中使用elasticsearch
http://www.thinkphp.cn/topic/29297.html
分页
http://blog.csdn.net/hzrandd/article/details/47126945