ElasticSearch使用入门

来源:互联网 发布:济宁市高新区网络问政 编辑:程序博客网 时间:2024/05/29 13:46

ES的安装

下载地址:https://www.elastic.co/downloads/past-releases/elasticsearch-2-4-3

»启动
»cd......./elasticsearch-2-4-3
»bin/elasticsearch
»bin/elasticsearch-d(后台运行)

ES安装验证:

http://your ip:9200

注意:默认启动的时候es绑定的网络ip是本机127.0.0.1,只能通过这个ip访问

两种修改方式

1:修改config/elasticsearch.yml文件

network.host: 192.168.1.100

2:在启动es的时候指定参数【指定0.0.0.0也可以】

bin/elasticsearch-Dnetwork.host=192.168.1.100


CURL使用:

简单认为是可以在命令行下访问url的一个工具

curl是利用URL语法在命令行方式下工作的开源文件传输工具,使用curl可以简单实现常见的get/post请求。

curl 

 -x  指定http请求的方法

GET POST PUT DELETE

-d   指定要传递的参数

 

CURL创建索引库:

curl -XPUT 'http://localhost:9200/yehua/'

PUT/POST都可以


CURL创建索引:

curl -XPOSThttp://localhost:9200/yehua/emp/1

-d '{

"name" : "tom",

"age" : 25

}'

索引库名称必须要全部小写,不能以下划线开头,也不能包含逗号

如果没有明确指定索引数据的ID,那么es会自动生成一个随机的ID,需要使用POST参数

curl -XPOSThttp://localhost:9200/yehua/emp/ -d '{"name" : "tom"}'

如果想要确定我们创建的都是全新的数据

1:使用随机ID(post方式)

2:在url后面添加参数

curl -XPUThttp://localhost:9200/yehua/emp/2/_create -d'{"name":"tom","age":25}'

 

ES查询:

根据员工id查询

curl -XGEThttp://localhost:9200/yehua/emp/1?pretty

在任意的查询字符串中添加pretty参数,es可以得到易于识别的json结果。

检索文档中的一部分,如果只需要显示指定字段

curl -XGET 'http://localhost:9200/yehua/emp/1?_source=name,age&pretty'

查询指定索引库指定类型所有数据

curl -XGET http://localhost:9200/yehua/emp/_search?pretty

 

ES更新:

局部更新,可以添加新字段或者更新已有字段(必须使用POST)

curl -XPOSThttp://localhost:9200/yehua/emp/1/_update -d'{"doc":{"name":"mack","age":18}}'


ES删除:

curl -XDELETE http://localhost:9200/yehua/emp/1


ES 2.4 删除 type  
1.需要安装插件 bin/plugin install delete-by-query
2.指导文档位置: https://www.elastic.co/guide/en/elasticsearch/reference/2.4/docs-delete.html
curl -XDELETE 'localhost:9200/yuqing/wx_news/_query?pretty' -H 'Content-Type: application/json' -d'
{
  "query": { 
   "match_all": {}
  }
}
'

curl -XDELETE 'localhost:9200/yuqing/news/_query?pretty' -H 'Content-Type: application/json' -d'
{
  "query": { 
    "term": {
      "time": 0
    }
  }
}
'

Head Plugin

在线安装bin/plugin install mobz/elasticsearch-head


集群配置(修改conf/elasticsearch.yml文件)

discovery.zen.ping.unicast.hosts:["host1", "host2:9300"]


ES安装IK分词器:

elasticsearch官方默认的分词插件,对中文分词效果不理想

curl'http://localhost:9200/yehua/_analyze?pretty=true' -d '{"text":"我们是中国人"}'

如何集成IK分词工具【注意:需要先编译才能使用】

1:下载es的IK插件https://github.com/medcl/elasticsearch-analysis-ik/tree/2.x

2:使用maven对下载的es-ik源码进行编译(mvnclean package -DskipTests)

3:把编译后的target/releases下的elasticsearch-analysis-ik-1.10.3.zip文件拷贝到ES_HOME/plugins/ik目录下面,然后使用unzip命令解压

如果unzip命令不存在,则安装:yum install -y unzip

4:重启es服务

5:测试分词效果:

curl'http://localhost:9200/yehua/_analyze?analyzer=ik_max_word&pretty=true' -d'{"text":"我们是中国人"}'


索引库设计:

curl -XPOST 'localhost:9200/yehua' -d@yehua.json

vi yehua.json

{

       "settings":{

                "number_of_shards":5,

                "number_of_replicas":0

       },

       "mappings":{

                "article":{

                       "dynamic":"strict",

                       "_all":{"enabled":false},

                       "properties":{

                               "title":{"type":"string","index":"analyzed","analyzer":"ik_max_word","search_analyzer": "ik_max_word"},

                               "describe":{"type":"string","index":"analyzed","analyzer":"ik_max_word","search_analyzer": "ik_max_word"},

                               "content":{"type":"string","index":"analyzed","analyzer":"ik_max_word","search_analyzer": "ik_max_word"},

                               "author":{"type":"string","index":"no"},

                "time":{"type":"date","index":"not_analyzed","format":"yyyy-MM-dd HH:mm:ss"}

                        }

                }

       }

}

注意:

dynamic可以有三个选项值:true是开启动态映射,false忽略没有定义的字段,strict当遇到未知字段时抛出异常

index可以有三个选项值:not_analyzed是不分词建立索引,no是指不建立索引,analyzed是指分词建立索引


mapping中的数据类型:

字符串string

数字(byte,short,integer,long,float,double)

日期date

日期的话可以设置格式

{"type":"date","format":"yyyy-MM-dd"}

布尔型boolean

二进制binary


修改mapping示例

curl -XPOST "http://127.0.0.1:9200/test/test/_mapping?pretty" -d '{
     "test": {
                "dynamic":"true"
            }
        }
}'


原创粉丝点击