zoukankan      html  css  js  c++  java
  • 安装和使用elasticsearch

    机器 CentOS 6.5

    先去elasticsearch 官网 

      https://www.elastic.co/

    我下载的是1.6版 

    https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.6.0.tar.gz

    下载好后 直接解压

    tar -zxvf   elasticsearch-1.6.0.tar.gz

    启动:./bin/elasticsearch[args] 

    查看状态:http://{es-host}:9200| netstat –ano|grep 9300

    这个时候 我们浏览器上应该就能看到 elasticsearch 的状态了

    启动时 设置 集群 name   和节点 name

    ./elasticsearch --cluster.name my_cluster_name --node.name my_node_name

    启动es

      Cd/usr/local/elasticsearch-0.18.7/bin/service

      ./elasticsearch start    ------后台运行

     停止es

       Cd/usr/local/elasticsearch-0.18.7/bin/service

       /elasticsearch stop

    (说明:可参考https://github.com/elasticsearch/elasticsearch-servicewrapper

    附:插件的主要命令

       ./elasticsearchconsole  ------前台运行

        ./elasticsearch start    ------后台运行

        ./elasticsearchinstall   -------添加到系统自动启动

        ./elasticsearchremove   -----取消随系统自动启动

    elasticsearchservicewrapper安装

           这个是对elasticsearch执行命令的包装服务,安装之后,方便elasticsearch的启动,停止等等操作。

          (1)下载elasticsearchservicewrapper

                    git clone https://github.com/elasticsearch/elasticsearch-servicewrapper,然后将目录下的service目录拷贝至ES_HOME/bin目录下。

          (2)简单配置jvm的内存

                    修改ES_HOME/bin/service/elasticsearch.conf,set.default.ES_HEAP_SIZE=1024,该值根据机器的配置可自定义。

          (3)安装启动服务

                    执行命令:ES_HOME/bin/service/elasticsearch install

          (4)启动/停止/重启服务

                   执行命令:ES_HOME/bin/service/elasticsearch start/stop/restart

    安装  elasticsearch-head 插件

    ./bin/plugin -install mobz/elasticsearch-head

    浏览器查看

    http://localhost:9200/_plugin/head/

    安装  elasticsearch-kopf 插件

    bin/plugin -install lmenezes/elasticsearch-kopf

    浏览器查看

    http://localhost:9200/_plugin/kopf/

    安装  ansj 中文分词插件

    进入Elasticsearch目录运行如下命令

    ./bin/plugin -u http://maven.ansj.org/org/ansj/elasticsearch-analysis-ansj/1.x.1/elasticsearch-analysis-ansj-1.x.1-release.zip -i ansj
    简单配置:
    ################################## ANSJ PLUG CONFIG ################################
    index:
       analysis:
         analyzer:
            index_ansj:
                type: ansj_index
            query_ansj:
                type: ansj_query
    
    index.analysis.analyzer.default.type: ansj_index

    创建一个索引

    PUT   http://192.168.8.80:9200/customer?pretty

    为索引插入一条记录

      curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '

    { "name": "John Doe" }

    插入好后 读取当前索引内容

    http://192.168.8.80:9200/customer/external/1?pretty#

    删除一个索引

    DELETE  http://192.168.8.80:9200/customer?pretty

    一些命令 下面是我一些学习笔记 很多代码是直接在官方文档复制出来的  

    官方文档地址 ES文档

    查看集群是否健康

    http://192.168.8.80:9200/_cat/health?v

    curl 'localhost:9200/_cat/health?v'

    查看节点列表

    http://192.168.8.80:9200/_cat/nodes?v

    curl 'localhost:9200/_cat/nodes?v'

    列出所有索引

    http://192.168.8.80:9200/_cat/indices?v

    curl 'localhost:9200/_cat/indices?v'

    创建索引  索引名customer 默认会有5个分片 1个索引

    curl -XPUT 'localhost:9200/customer?pretty'


    添加一个类型

    curl -XPUT 'localhost:9200/customer/external/2?pretty' -d '

    {
      "gwyy": "John Doe"

    }'


    更新一个类型

    curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '

    {
      "doc": { "name": "Jaffffne Doe" }

    }'


    删除一条记录

    curl-XDELETE'localhost:9200/customer/external/2?pretty'



    删除指定name的数据

    curl -XDELETE 'localhost:9200/customer/external/_query?pretty' -d '

    {
      "query": { "match": { "name": "Jaffffne Doe" } }

    }'





    批量添加

    curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d '
    {"index":{"_id":"1"}}
    {"name": "John Doe" }
    {"index":{"_id":"2"}}
    {"name": "Jane Doe" }
    '

    批处理 更新 和 删除 删除没有对应的 doc 因为删除了 doucment就没了

    curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d '
    {"update":{"_id":"1"}}
    {"doc": { "name": "John Doe becomes Jane Doe" } }
    {"delete":{"_id":"2"}}
    '

    如果批处理一个单独动作失败了 会继续执行下一个动作 然后所有的执行完后 会返回每条执行动作的状态


    批量搜索接口

    curl'localhost:9200/bank/_search?q=*&pretty'

    结果格式

    • took – 搜索用的毫秒
    • timed_out – 搜索超时时间
    • _shards – 搜索了多少个片段 成功搜索多少个 失败了多少个
    • hits – 搜索的返回结果集
    • hits.total – 结果的总数
    • hits.hits – 实际搜索返回的数组
    • _score and max_score - ignore these fields for now

    另一种搜索方法

    curl-XPOST'localhost:9200/bank/_search?pretty'-d'{ "query": { "match_all": {} }}'

    一旦es搜索返回给你 该链接就断开了 es并不会想mysql之类的 一直维护一个连接



    查询匹配所有 但是只查询一个

    curl-XPOST'localhost:9200/bank/_search?pretty'-d'{ "query": { "match_all": {} }, "size": 1}'

    注意 如果没有指定大小 默认返回10 

    从11位开始查询10个

    curl-XPOST'localhost:9200/bank/_search?pretty'-d'{ "query": { "match_all": {} }, "from": 10, "size": 10}'

    未指定from的话 默认是 0


    查询所有 按照 balance 排序

    curl-XPOST'localhost:9200/bank/_search?pretty'-d'{ "query": { "match_all": {} }, "sort": { "balance": { "order": "desc" } }}'


    查询所有数据 但是只获取指定字段

    curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
    {
      "query": { "match_all": {} },
      "_source": ["account_number", "balance"],
      "size": 1
    }'


    真正的条件查询来了

    查询 account_method = 20

    curl-XPOST'localhost:9200/bank/_search?pretty'-d'{ "query": { "match": { "account_number": 20 } }}'


    查询地址里包含 mill的

    curl-XPOST'localhost:9200/bank/_search?pretty'-d'{ "query": { "match": { "address": "mill" } }}'


    布尔值查询 必须2个都满足

    curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
    {
      "query": {
        "bool": {
          "must": [
            { "match": { "address": "mill" } },
            { "match": { "address": "lane" } }
          ]
        }
      }
    }'


    查询2个条件 只要有一个满足就返回

    curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
    {
      "query": {
        "bool": {
          "should": [
            { "match": { "address": "mill" } },
            { "match": { "address": "lane" } }
          ]
        }
      }
    }'


    不包括下面2个搜索项的数据查询

    curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
    {
      "query": {
        "bool": {
          "must_not": [
            { "match": { "address": "mill" } },
            { "match": { "address": "lane" } }
          ]
        }
      }
    }'

    多种状态一起查询

    curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
    {
      "query": {
        "bool": {
          "must": [
            { "match": { "age": "40" } }
          ],
          "must_not": [
            { "match": { "state": "ID" } }
          ]
        }
      }
    }'


    定义过滤器

    curl-XPOST'localhost:9200/bank/_search?pretty'-d'{ "query": { "filtered": { "query": { "match_all": {} }, "filter": { "range": { "balance": { "gte": 20000, "lte": 30000 } } } } }}'



    聚合

    curl-XPOST'localhost:9200/bank/_search?pretty'-d'{ "size": 0, "aggs": { "group_by_state": { "terms": { "field": "state" } } }}'


    elasticsearch格式索引:类型:id

    curl-X<RESTVerb><Node>:<Port>/<Index>/<Type>/<ID>

  • 相关阅读:
    C# FromBase64String 解码换行问题
    Func与Action
    C# 委托及各种写法
    DNS解析过程和域名收敛、域名发散、SPDY应用
    基础数据类型长度
    C#静态常量和动态常量的区别
    dynamic和var的区别
    ADO.NET中的五个主要对象
    Linux 守护进程创建原理及简易方法
    利用/dev/urandom文件创建随机数
  • 原文地址:https://www.cnblogs.com/gwyy/p/8027655.html
Copyright © 2011-2022 走看看