ES学习笔记

es是什么?
es是基于Apache Lucene的开源分布式(全文)搜索引擎,,提供简单的RESTful API来隐藏Lucene的复杂性。

es除了全文搜索引擎之外,还可以这样描述它:
1、分布式的实时文件存储,每个字段都被索引并可被搜索
2、分布式的实时分析搜索引擎
3、可以扩展到成百上千台服务器,处理PB级结构化或非结构化数据。


ES的数据组织类比

Relational DBElasticsearch
数据库(database)索引(indices)
表(tables)types
行(rows)documents
字段(columns)fields

mac安装ES

    - 1、更新brew   
    ```brew update```
    - 2、安装java1.8版本
    ```brew cask install homebrew/cask-versions/java8```
    - 3、安装ES
    ```brew install elasticsearch```
    - 4、启动本地ES
    ```brew services start elasticsearch```
    - 5、本地访问9200端口查看ES安装
    ```http://localhost:9200```
    - 6、安装kibana
    ```Kibana是ES的一个配套工具,可以让用户在网页中与ES进行交互```
    ```brew install kibana```
    - 7、本地启动kibana
    ```brew services start kibana```
    - 8、本地访问5601端口进入kibana交互界面
    ```http://localhost:5601```

一、 ES简单的增删改查
1、创建一篇文档(有则修改,无则创建)

PUT test/doc/2
{
  "name":"wangfei",
  "age":27,
  "desc":"热天还不让后人不认同"
}

PUT test/doc/1
{
  "name":"wangjifei",
  "age":27,
  "desc":"萨芬我反胃为范围额"
}

PUT test/doc/3
{
  "name":"wangyang",
  "age":30,
  "desc":"点在我心内的几首歌"
}

2、查询指定索引信息
GET test
3、 查询指定文档信息
GET test/doc/1
GET test/doc/2
4、查询对应索引下所有数据

GET test/doc/_search
或
GET test/doc/_search
{
  "query": {
    "match_all": {}
  }
}

5、删除指定文档
DELETE test/doc/3

6、删除索引
DELETE test

7、修改指定文档方式
修改时,不指定的属性会自动覆盖,只保留指定的属性(不正确的修改指定文档方式)

PUT test/doc/1
{
  "name":"王计飞"
}

使用POST命令,在id后面跟_update,要修改的内容放到doc文档(属性)中(正确的修改指定文档方式)

POST test/doc/1/_update
{
  "doc":{
    "desc":"生活就像 茫茫海上"
  }
}

二、ES查询的两种方式
1、查询字符串搜索
GET test/doc/_search?q=name:test

2、结构化查询(单字段查询,不能多字段组合查询)

GET test/doc/_search
{
  "query":{
    "match":{
      "name":"test"
    }
  }
}

三、match系列之操作
1、match系列之match_all (查询全部)

GET test/doc/_search
{
  "query":{
    "match_all": {
    }
  }
}

2、match系列之match_phrase(短语查询)

准备数据

PUT test1/doc/1
{
  "title": "中国是世界上人口最多的国家"
}
PUT test1/doc/2
{
  "title": "美国是世界上军事实力最强大的国家"
}
PUT test1/doc/3
{
  "title": "北京是中国的首都"
}

查询如下

查询语句

GET test1/doc/_search
{
  "query":{
    "match":{
      "title":"中国"
    }
  }
}

>>>输出结果
{
  "took" : 241,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 0.68324494,
    "hits" : [
      {
        "_index" : "test1",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 0.68324494,
        "_source" : {
          "title" : "中国是世界上人口最多的国家"
        }
      },
      {
        "_index" : "test1",
        "_type" : "doc",
        "_id" : "3",
        "_score" : 0.5753642,
        "_source" : {
          "title" : "北京是中国的首都"
        }
      },
      {
        "_index" : "test1",
        "_type" : "doc",
        "_id" : "2",
        "_score" : 0.39556286,
        "_source" : {
          "title" : "美国是世界上军事实力最强大的国家"
        }
      }
    ]
  }
}

添加新评论