目前elasticsearch的稳定最新版6.2.4,建议大家装6.2.3,因为中文分词只兼容到6.2.3。
1,安装java1.8
$ brew install java8 $ java -version java version "1.8.0_112" Java(TM) SE Runtime Environment (build 1.8.0_112-b16) Java HotSpot(TM) 64-Bit Server VM (build 25.112-b16, mixed mode)
2,修改elasticsearch.rb
$ vim /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/elasticsearch.rb class Elasticsearch < Formula desc "Distributed search & analytics engine" homepage "https://www.elastic.co/products/elasticsearch" - url "https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.2.tar.gz" - sha256 "b26e3546784b39ce3eacc10411e68ada427c5764bcda3064e9bb284eca907983" + url "https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.3.tar.gz" //换成这个 + sha256 "01dd8dec5f0acf04336721e404bf4d075675a3acae9f2a9fdcdbb5ca11baca76" //换成这个
3,安装,启动elasticsearch
# brew install elasticsearch # brew services start elasticsearch //启动1 # elasticsearch //启动2
上面二种启动方法,在调试阶段,选择启动2,es2.x与es6.x差别还是挺大的,最坑的地方是官方文档,还没有更新。例如:es6.x mapping中的type已没有string类型了,但是官方给的例子当中,还用的是string类型。
4,创建与删除索引
$ curl -XPUT "http://127.0.0.1:9200/tanktest?pretty" //创建
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "tanktest"
}
$ curl -XDELETE "http://127.0.0.1:9200/tanktest?pretty" //删除
{
"acknowledged" : true
}
这个根oracle的存储空间差不多,规定数据要存储到什么地方
5,创建mapping
ES的mapping非常类似于静态语言中的数据类型:声明一个变量为int类型的变量, 以后这个变量都只能存储int类型的数据。同样的, 一个number类型的mapping字段只能存储number类型的数据。
同语言的数据类型相比,mapping还有一些其他的含义,mapping不仅告诉ES一个field中是什么类型的值, 它还告诉ES如何索引数据以及数据是否能被搜索到。
当你的查询没有返回相应的数据, 你的mapping很有可能有问题。当你拿不准的时候, 直接检查你的mapping。
一个mapping由一个或多个analyzer组成, 一个analyzer又由一个或多个filter组成的。当ES索引文档的时候,它把字段中的内容传递给相应的analyzer,analyzer再传递给各自的filters。
filter的功能很容易理解:一个filter就是一个转换数据的方法, 输入一个字符串,这个方法返回另一个字符串,比如一个将字符串转为小写的方法就是一个filter很好的例子。
一个analyzer由一组顺序排列的filter组成,执行分析的过程就是按顺序一个filter一个filter依次调用, ES存储和索引最后得到的结果。
总结来说, mapping的作用就是执行一系列的指令将输入的数据转成可搜索的索引项。
搞过数据库的人,对这个可能更好理解一点,你可以把它理解成数据库表。
$ curl -XGET "http://127.0.0.1:9200/tanktest/_mapping?pretty"
{
"tanktest" : {
"mappings" : { } //没有
}
}
//创建mapping
$ curl -XPOST "http://127.0.0.1:9200/tanktest/test/_mapping?pretty" -H "Content-Type: application/json" -d '
{
"test": {
"properties": {
"title": {
"type": "text",
},
"description": {
"type": "text"
}
}
}
}
'
//查看mapping
$ curl -XGET "http://127.0.0.1:9200/tanktest/_mapping?pretty"
{
"tanktest" : {
"mappings" : {
"test" : {
"properties" : {
"description" : {
"type" : "text"
},
"title" : {
"type" : "text"
}
}
}
}
}
}
在用post的时候,要加上-H "Content-Type: application/json",不然会报以下错
"error" : "Content-Type header [application/x-www-form-urlencoded] is not supported"
在这里一定要注意,mapping定义的type是不允许修改,所以在设计的时候,一定要考虑清楚
6,向索中插入数据
$ curl -XPOST "http://127.0.0.1:9200/tanktest/test/?pretty" -H "Content-Type: application/json" -d '
{
"title" : "tank test",
"description" : "asdfasdfasdfasdfasd asdfasdfasdf"
}'
$ curl -XPOST "http://127.0.0.1:9200/tanktest/test/?pretty" -H "Content-Type: application/json" -d '
{
"title" : "zhangying test",
"description" : "asdfasdfasdfasdfasd asdfasdfasdf"
}'
插入二条数据
7,检索
$ curl -XPOST "http://127.0.0.1:9200/tanktest/test/_search?pretty" -H "Content-Type: application/json" -d '
> {
> "query": {
> "match": {
> "title": "tank test"
> }
> }
> }'
{
"took" : 98,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "tanktest",
"_type" : "test",
"_id" : "npf7-2IBVvjz0l6Th54k",
"_score" : 0.5753642,
"_source" : {
"title" : "tank test",
"description" : "asdfasdfasdfasdfasd asdfasdfasdf"
}
},
{
"_index" : "tanktest",
"_type" : "test",
"_id" : "n5f7-2IBVvjz0l6TnJ5I",
"_score" : 0.2876821,
"_source" : {
"title" : "zhangying test",
"description" : "asdfasdfasdfasdfasd asdfasdfasdf"
}
}
]
}
}
搜索tank test会出来二条数据,匹配度高的排在前面,后一条含有test,所以搜索出来了,用了默认的英文分词
8,分词
$ curl -XPOST 'http://localhost:9200/tanktest/_analyze?pretty=true' -H 'Content-Type: application/json' -d '{ "analyzer":"standard", "text":"tank test"}'
{
"tokens" : [
{
"token" : "tank",
"start_offset" : 0,
"end_offset" : 4,
"type" : "",
"position" : 0
},
{
"token" : "test",
"start_offset" : 5,
"end_offset" : 9,
"type" : "",
"position" : 1
}
]
}
tank test分成了tank和test
转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/server/1891.html