Elasticsearch初体验

Elasticsearch学习笔记

一、简介

ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。
设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。
我们建立一个网站或应用程序,并要添加搜索功能,但是想要完成搜索工作的创建是非常困难的。我们希望搜索解决方案要运行速度快,
我们希望能有一个零配置和一个完全免费的搜索模式,我们希望能够简单地使用JSON通过HTTP来索引数据,我们希望我们的搜索服务器始终可用,我们希望能够从一台开始并扩展到数百台,
我们要实时搜索,我们要简单的多租户,我们希望建立一个云的解决方案。
因此我们利用Elasticsearch来解决所有这些问题及可能出现的更多其它问题。

二、安装

1.安装Elasticsearch

1.查询相关镜像
 docker search elasticsearch
2.下载镜像
 docker pull elasticsearch:6.5.0
3.查看镜像
 docker images
4.启动实例
 docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:6.5.0
5.查看是否安装启动成功
  浏览器访问 http://localhost:9200/ (curl http://localhost:9200/)
  返回
  {
    "name" : "gkQsy39",
    "cluster_name" : "docker-cluster",
    "cluster_uuid" : "P8xh_EGhRIKSVO0Lnr_iSA",
    "version" : {
      "number" : "6.5.0",
      "build_flavor" : "default",
      "build_type" : "tar",
      "build_hash" : "816e6f6",
      "build_date" : "2018-11-09T18:58:36.352602Z",
      "build_snapshot" : false,
      "lucene_version" : "7.5.0",
      "minimum_wire_compatibility_version" : "5.6.0",
      "minimum_index_compatibility_version" : "5.0.0"
    },
    "tagline" : "You Know, for Search"
  }


Tips:
    默认elasticsearch 占用2G内存,要是启动不了,可以进入实例修改内存大小
    docker exec -it elasticsearch /bin/bash
    或者
    winpty docker exec -it elasticsearch bash
    打开jvm配置
    vi ./config/jvm.options
    修改参数
    -Xms512M(默认配置2g)
    -Xmx512M(默认配置2g)

2.安装Kibana

1.查询相关镜像
 docker search kibana
2.下载镜像
  docker pull kibana:6.5.0
3.查看镜像
 docker images
4.启动实例
 docker run --name kibana -e ELASTICSEARCH_URL=http://172.17.0.1:9200 -p 5601:5601 kibana:6.5.0
5.浏览器输入  http://localhost:5061

Tips:
    (1).ELASTICSEARCH_URL 一定是 elasticSearch 容器地址,
        ELASTICSEARCH_URL=http://172.17.0.1:9200
    (2).查看elasticsearch容器地址方法
      docker inspect elasticsearch
    (3). kibana 版本一定要和 elasticsearch版本号对应上(踩坑)
    

三、简单集群操作(以下所有操作均在 kibana Dev Tools界面操作)

  • 1 快速检查集群的健康状况 GET /_cat/health?v
    如何快色了解集群的监控状况? green\yellow\red
    epoch      timestamp cluster        status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
    1552790807 02:46:47  docker-cluster green           1         1      1   1    0    0        0             0                  -                100.0%
    
    status:green\yellow\red
    green: 每个索引的primary shard和replica shard都是active状态
    yellow: 每个索引的primary shard都是active状态,但是部分replica shard不是active状态
    red:不是所有的primary shard都是active状态,部分索引的数据丢失了
  • 2 快速查看集群中有哪些缩影 GET /_cat/indices?/v
    kibana 自己创建的索引
    
    health status index     uuid                   pri rep docs.count docs.deleted store.size pri.store.size
    green  open   .kibana_1 Wr34CaR8TbW3G49wpjzotA   1   0          2            0      8.5kb          8.5kb
  • 3 简单索引的操作

    (1) 创建索引:
PUT /test_index?pretty

#! Deprecation: the default number of shards will change from [5] to [1] in 7.0.0; if you wish to continue using the default of [5] shards, you must manage this on the create index request or with an index template
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "test_index"
}

再次查询索引
GET /_cat/indices?/v
health status index      uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .kibana_1  Wr34CaR8TbW3G49wpjzotA   1   0          2            0      8.5kb          8.5kb
yellow open   test_index CUKaD3AdQ1OzycNVxYpZEg   5   1          0            0      1.1kb          1.1kb

(2) 创建索引:

DELETE /test_index?pretty

{
  "acknowledged" : true
}

四、简单CRUD操作

  • 1 新增商品、新增文档、建立索引 eg:PUT /index/type/id
PUT /index/type/id
    PUT /leafproduct/product/1
    {
        "name":"haifeisi xifalu",
        "desc":"rousishunhua",
        "price":30.00,
        "producer":"haifeisi",
        "tags":["roushui","sihua"]
    }

插入成功提示
    
    {
      "_index" : "leafproduct",
      "_type" : "product",
      "_id" : "1",
      "_version" : 1,
      "result" : "created",
      "_shards" : {
        "total" : 2,
        "successful" : 1,
        "failed" : 0
      },
      "_seq_no" : 0,
      "_primary_term" : 1
    }




PUT /leafproduct/product/2
{
    "name":"oulaiya xifalu",
    "desc":"buxiu",
    "price":49.50,
    "producer":"oulaiya",
    "tags":["roushui","oulaiya"]
}
PUT /leafproduct/product/3
{
    "name":"lishi xifalu",
    "desc":"lishiniubi",
    "price":79.99,
    "producer":"lishi",
    "tags":["lishi","sihua"]
}

elasticsearch 会自动建立index 和type ,不需要体检创建,
而且elasticsearch 默认会对document每个field进行倒排索引,让其可以被搜索。
  • 2 查询商品 eg:GET /index/type/id

GET /leafproduct/product/1
结果
{
  "_index" : "leafproduct",
  "_type" : "product",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "name" : "haifeisi xifalu",
    "desc" : "rousishunhua",
    "price" : 30.0,
    "producer" : "haifeisi",
    "tags" : [
      "roushui",
      "sihua"
    ]
  }
}


  • 3 修改商品
(1) 替换更新 eg:PUT /index/type/id
    PUT /leafproduct/product/1
    {
        "name":"haifeisi xifalu",
        "desc":"rousishunhua",
        "price":30.99,
        "producer":"haifeisi_PRODUCER",
        "tags":["roushui","sihua"]
    }
    修改后查询结果:
    {
      "_index" : "leafproduct",
      "_type" : "product",
      "_id" : "1",
      "_version" : 2,
      "found" : true,
      "_source" : {
        "name" : "haifeisi xifalu",
        "desc" : "rousishunhua",
        "price" : 30.99,
        "producer" : "haifeisi_PRODUCER",
        "tags" : [
          "roushui",
          "sihua"
        ]
      }
    }


(2) 只修改某些字段  eg: POST /index/type/id/_update

POST /leafproduct/product/1/_update
{
  "doc":{
    "name":"zhangbaobao xifalu"
  }
}

修改后查询
{
  "_index" : "leafproduct",
  "_type" : "product",
  "_id" : "1",
  "_version" : 4,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 3,
  "_primary_term" : 1
}

  • 4 删除商品 eg: DELETE /index/type/id?pretty
DELETE /leafproduct/product/2?pretty
执行后查询
{
  "_index" : "leafproduct",
  "_type" : "product",
  "_id" : "2",
  "found" : false
}

五、搜索方式

    1. query string search
    2. query DSL
    3. query filter
    4. full-text search
    5. pharse search
    6. highlight search

1.query string search

1.查询所有商品
GET /leafproduct/product/_search
2.查询名字包含xifulu的商品,并且按照价格倒叙排列
GET /leafproduct/product/_search?q=name:xifalu&sort=price:desc

2.query DSL

1.查询所有商品
GET /leafproduct/product/_search
{
  "query": {
    "match_all": {}
  }
}
2.匹配name字段,价格由大到小排序
GET /leafproduct/product/_search
{
  "query": {
    "match": {
      "name": "xifalu"
    }
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ]
}
2.匹配name字段,价格由大到小排序
GET /leafproduct/product/_search
{
  "query": {
    "match": {
      "name": "xifalu"
    }
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ]
}
3.分页查询 page=2. size=2
GET /leafproduct/product/_search
{
  "query": {
    "match_all": {}
  },
  "from": 2,
  "size": 2
}
4.查询部分字段(name,price)
GET /leafproduct/product/_search
{
  "query": {
    "match_all": {}
  },
  "_source": ["name","price"]
}

3.query filter

对数据进行过滤操作
查询 价格在 30-50之间的 名字为xifalu的商品

GET /leafproduct/product/_search
{
  "query": {
    "bool": {
      "must": {
        "match":{
          "name":"xifalu" 
        }
      },
      "filter": {
        "range": {
          "price": {
            "gte": 30,
            "lte": 50
          }
        } 
      }
    }
  }
}

4.full-text search全文检索

会拆解输入的关键词,只要匹配任何一个就会hit命中

查询语法,
GET /leafproduct/product/_search
{
  "query": {
    "match": {
      "name": "oulaiya xifalu"
    }
  }
}


结果如下,_score权重不同

{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "leafproduct",
        "_type" : "product",
        "_id" : "2",
        "_score" : 0.5753642,
        "_source" : {
          "name" : "oulaiya xifalu",
          "desc" : "buxiu",
          "price" : 49.5,
          "producer" : "oulaiya",
          "tags" : [
            "roushui",
            "oulaiya"
          ]
        }
      },
      {
        "_index" : "leafproduct",
        "_type" : "product",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "name" : "haifeisi xifalu",
          "desc" : "rousishunhua",
          "price" : 30.99,
          "producer" : "haifeisi_PRODUCER",
          "tags" : [
            "roushui",
            "sihua"
          ]
        }
      },
      {
        "_index" : "leafproduct",
        "_type" : "product",
        "_id" : "3",
        "_score" : 0.2876821,
        "_source" : {
          "name" : "lishi xifalu",
          "desc" : "lishiniubi",
          "price" : 79.99,
          "producer" : "lishi",
          "tags" : [
            "lishi",
            "sihua"
          ]
        }
      }
    ]
  }
}

5.pharse search 短语搜索

要求输入的 搜素串必须在字段文本中,完全一模一样,才算匹配到吗,才返回结果

语法
GET /leafproduct/product/_search
{
  "query": {
    "match_phrase": {
      "name": "oulaiya xifalu"
    }
  }
}

结果:

{
  "took" : 22,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "leafproduct",
        "_type" : "product",
        "_id" : "2",
        "_score" : 0.5753642,
        "_source" : {
          "name" : "oulaiya xifalu",
          "desc" : "buxiu",
          "price" : 49.5,
          "producer" : "oulaiya",
          "tags" : [
            "roushui",
            "oulaiya"
          ]
        }
      }
    ]
  }
}

6.highlight search高亮搜索

关键词所在字段高亮显示

语法:
GET /leafproduct/product/_search
{
  "query": {
    "match": {
      "name": "oulaiya xifalu"
    }
  },
  "highlight": {
    "fields": {
      "name": {}
    }
  }
}

结果:
{
  "took" : 52,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "leafproduct",
        "_type" : "product",
        "_id" : "2",
        "_score" : 0.5753642,
        "_source" : {
          "name" : "oulaiya xifalu",
          "desc" : "buxiu",
          "price" : 49.5,
          "producer" : "oulaiya",
          "tags" : [
            "roushui",
            "oulaiya"
          ]
        },
        "highlight" : {
          "name" : [
            "<em>oulaiya</em> <em>xifalu</em>"
          ]
        }
      },
      {
        "_index" : "leafproduct",
        "_type" : "product",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "name" : "haifeisi xifalu",
          "desc" : "rousishunhua",
          "price" : 30.99,
          "producer" : "haifeisi_PRODUCER",
          "tags" : [
            "roushui",
            "sihua"
          ]
        },
        "highlight" : {
          "name" : [
            "haifeisi <em>xifalu</em>"
          ]
        }
      },
      {
        "_index" : "leafproduct",
        "_type" : "product",
        "_id" : "3",
        "_score" : 0.2876821,
        "_source" : {
          "name" : "lishi xifalu",
          "desc" : "lishiniubi",
          "price" : 79.99,
          "producer" : "lishi",
          "tags" : [
            "lishi",
            "sihua"
          ]
        },
        "highlight" : {
          "name" : [
            "lishi <em>xifalu</em>"
          ]
        }
      }
    ]
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值