ElasticSearch的安装及数据的使用

一共要装三个东西 ElasticSearch   Node.js   ElasticSearch-head

下面的操作主要来源于

https://blog.csdn.net/mottohlm/article/details/80875207https://blog.csdn.net/qq_21383435/article/details/79281507

作在Window环境下操作

ElasticSearch   下载链接 https://www.elastic.co/downloads/elasticsearch

下载Zip后解压,在bin中,双击执行 elasticsearch.bat 。等待打印信息输出完之后打开浏览器,输入:localhost:9200 。页面显示如下,则说明安装好了。

再下一个Node.js 链接:http://nodejs.cn/download/ 下载MSI格式的,然后点击里面的安装包安装

现在下ElasticSearch-head 链接 : https://github.com/mobz/elasticsearch-head 下到某目录  如elasticSearchHead目录下

然后 cd到此目录 

  1. npm install -g grunt-cli 
    grunt是一个很方便的构建工具,可以进行打包压缩、测试、执行等等的工作
D:\elasticsearch\elasticsearch-6.2.0\myplugins\elasticsearch-head-master>npm install -g grunt-cli
C:\Users\Administrator\AppData\Roaming\npm\grunt -> C:\Users\Administrator\AppData\Roaming\npm\node_modules\grunt-cli\bin\grunt
+ [email protected]
updated 1 package in 4.429s

    2.npm install

 D:\elasticsearch\elasticsearch-6.2.0\myplugins\elasticsearch-head-master>npm install
npm WARN deprecated [email protected]: Use the built-in module in node 9.0.0 or newer, instead
npm WARN deprecated [email protected]: CoffeeScript on NPM has moved to "coffeescript" (no hyphen)

> [email protected] install D:\elasticsearch\elasticsearch-6.2.0\myplugins\elasticsearch-head-master\node_modules\phantomjs-prebuilt
> node install.js

PhantomJS not found on PATH
Download already available at C:\Users\ADMINI~1\AppData\Local\Temp\phantomjs\phantomjs-2.1.1-windows.zip
Verified checksum of previously downloaded file
Extracting zip contents
Removing D:\elasticsearch\elasticsearch-6.2.0\myplugins\elasticsearch-head-master\node_modules\phantomjs-prebuilt\lib\phantom
Copying extracted folder C:\Users\ADMINI~1\AppData\Local\Temp\phantomjs\phantomjs-2.1.1-windows.zip-extract-1517991758740\phantomjs-2.1.1-windows -> D:\elasticsearch\elasticsearc
\elasticsearch-head-master\node_modules\phantomjs-prebuilt\lib\phantom
Writing location.js file
Done. Phantomjs binary available at D:\elasticsearch\elasticsearch-6.2.0\myplugins\elasticsearch-head-master\node_modules\phantomjs-prebuilt\lib\phantom\bin\phantomjs.exe
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN [email protected] license should be a valid SPDX license expression
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

added 384 packages in 40.221s  

这一点第一次安装会不一样,会有进度条,我这是第二次,为了写博客

问题1:可能会报错

Error making request.
Error: connect ETIMEDOUT 54.231.112.152:443
    at Object._errnoException (util.js:1022:11)
    at _exceptionWithHostPort (util.js:1044:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1182:14)

Please report this full log at https://github.com/Medium/phantomjs
npm WARN [email protected] license should be a valid SPDX license expression
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] install: `node install.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Administrator\AppData\Roaming\npm-cache\_logs\2018-03-02T02_46_07_354Z-debug.log

原因:网络原因 
解决方法:多次执行,总会成功的

      3. grunt server

D:\elasticsearch\elasticsearch-6.2.0\myplugins\elasticsearch-head-master>grunt server
(node:3492) ExperimentalWarning: The http2 module is an experimental API.
Running "connect:server" (connect) task
Waiting forever...
Started connect web server on http://localhost:9100

这样就完成安装了

elasticSearch的elasticsearch-head-master 登录进去,显示集群不健康值 

问题解决:在config/elasticsearch.yml加上两句

http.cors.enabled: true
http.cors.allow-origin: "*"

ElasticSearch的一些概念

ES中的一些概念

index(索引)

相当于mysql中的数据库

type(类型)

相当于mysql中的一张表

document(文档)

相当于mysql中的一行(一条记录)

field(域)

相当于mysql中的一列(一个字段)

节点

一个服务器,由一个名字来标识

集群

一个或多个节点组织在一起

分片

将一份数据划分为多小份的能力,允许水平分割和扩展容量。多个分片可以响应请求,提高性能和吞吐量。

副本

复制数据,一个节点出问题时,其余节点可以顶上。

ES支持的数据类型

  • text 文本类型,text类型如果不显示指定映射的字段属性,默认是使用标准分词器进行分词,一般数据类型是text要么显示指定分词器,要么不分词,一般不使用默认的分词器(除非文本是纯英文)
  • keyword : 如果数组中的值是字符串,最好使用keyword, 它不会对字符串进行分词
  • date 支持的格式有:可以通过format定义日期的数据格式,也支持毫秒数
  • boolean
  • float
  • double
  • byte
  • short
  • integer
  • long
  • object
  • nested : 嵌套对象, 用于数组中的元素是对象的[{}, {}]
  • ip 即支持ipv4也支持ipv6
  • completion
  • binary
  • geo-point: 支持经纬度存储和距离范围检索
  • geo-shape:支持任意图形范围的检索,例如矩形和平面多边形

1.

下面是操作数据,我看很多博文用cUrl来操作数据,cUrl只是用来发送请求的,这个功能用Postman也是可以的

 索引的增删改查有一个类似的格式,总结如下:

 请求 : <Node>:<Port>/<Index>/<Type>/<ID>

       <Node>:节点ip

  <port>:节点端口号,默认9200

  <Index>:索引名

  <Type>:索引类型

  <ID>:操作对象的ID号

所以我这样加索引 localhost:9200/customer2?pretty 返回添加成功(注意,我这里单加索引用的是PUT请求)

然后我再给这个索引加一个类型,用的还是PUT请求,它报错,要求在添加类型时,要用POST请求

所以改成这样,result是创建成功,它的id因为没指定,是随机生成的一个

所以我又加了一个doc,dme而且指定了id

这里的id主要是用来获取指定数据,比如说想获取customer2索引下类型为order,id是1的文档

查询customer2索引下类型为order的所有文档

查询结果解释:

1,hits

返回结果中最重要的部分是 hits ,它 包含 total 字段来表示匹配到的文档总数,并且一个 hits 数组包含所查询结果的前十个文档。

2,took

took 值告诉我们执行整个搜索请求耗费了多少毫秒。

指定查询条件,发现不能模糊查询

3,timeout

timed_out 值告诉我们查询是否超时。默认情况下,搜索请求不会超时。 如果低响应时间比完成结果更重要,你可以指定 timeout 为 10 或者 10ms(10毫秒),或者 1s(1秒)。以下命令:

GET /_search?timeout=10ms

结果

数据更新,原本我是这样写的

后来查了下,改成这样写了,于是就修改成功了

设置索引的分片数量、副本数量、默认的分词器等

PUT /my_temp_index/_settings { "number_of_replicas": 1 }

为索引创建一个别名

PUT /school_v1/_alias/school

查看别名指向的索引

GET /*/_alias/school

查询索引对应的别名

GET /school_v1/_alias/*

我既可以这样写http://localhost:9200/school/students/_mapping查询映射

也可以这样写http://localhost:9200/school/_mapping/students

总结上面的语句

1.同一个请求,请求方式的不同,作用也会不同,比如说localhost:9200/customer2/order/1,POST请求跟GET请求的作用,一个是添加一个是查询

2.执行查询和修改数据时,要加_search和_update

2.

ElasticSearch的分词器查询,因为要支持中文,所以使用IK分词器

下载链接

https://github.com/medcl/elasticsearch-analysis-ik/releases

因为IK的版本要跟ElasticSearch的版本严格对应起来,所以我els是6.3.2版本,我ik也要选V6.3.2

将下载的ZIP件解压至elasticsearch-6.3.2\elasticsearch-6.3.2\plugins\ik文件夹下(ik目录是我新建的),即可完成 ik 分词器的安装

然后重新启动els,看到有用了ik这个工具

然后写一个分词器的例子来试试

3.

映射就是创建索引时指定都包含哪些字段以及字段的数据类型、分词器等一些设置。

为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成全文本(Full-text)或精确的字符串值,Elasticsearch需要知道每个字段里面都包含了什么类型。这些类型和字段的信息存储(包含)在映射(mapping)中。

当你索引一个包含新字段的文档——一个之前没有的字段——Elasticsearch将使用动态映射猜测字段类型,这意味着,如果你索引一个带引号的数字——"123",它将被映射为"string"类型,而不是"long"类型。然而,如果字段已经被映射为"long"类型,Elasticsearch将尝试转换字符串为long,并在转换失败时会抛出异常。

索引概念简介

通常说的索引有两种词性,名称和动词。

  • 动词索引indexing,索引一个文档,表示把一个文档存储到索引Index里,可以用来查询和检索,es采用倒排索引
  • 名词索引index,简单的理解成关系型数据库中的数据库的概念

索引的命名必须是全部小写,不能以下划线开头

倒排索引

猜你喜欢

转载自blog.csdn.net/AAA821/article/details/81706534