Elasticsearch:一个方便易用的全文搜索库

注:本文内容均来自《Elasticsearch权威指南》,是做读书笔记,同时做一个总结。
Elasticsearch是使用基于http的RestFul来实现和使用的,因此使用curl来测试。如果不使用curl,使用各种语言的对应http请求即可使用。

Elasticsearch

集群包含索引,索引包含类型,类型存储文档,文档具有属性

能力

高可拓展、全文搜索、分析、存储

使用举例

  1. 在线web商店,可支持用户搜索产品(存储产品目录、产品搜索、补全建议)
  2. 收集运行数据、分析数据
  3. 价格预警平台
  4. 分析、快速投资、分析、可视化等
  5. ……

基本概念:

  1. 接近实时性的
  2. 集群
  3. 节点:集群中的单独服务
  4. 索引:存储数据到Elasticsearch的行为叫做索引
  5. 文档:最基本的可以被索引的信息、JSON
  6. 碎片 & 副本:碎片(切分/扩展 内容 卷、分发 和 并行 操作)、 副本:(高可用性(当碎片或节点不可用时)、允许扩展搜索卷)

使用

放入数据及搜索

放入数据

curl -X PUT "localhost:9200/megacorp/employee/1" -H 'Content-Type: application/json' -d'
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}
'

读取数据

curl -X GET "localhost:9200/megacorp/employee/1"

简单搜索

curl -X GET "localhost:9200/megacorp/employee/_search"

带参数简单搜索

curl -X GET "localhost:9200/megacorp/employee/_search?q=last_name:Smith"

查询表达式搜索

curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d'
{
    "query" : {
        "match" : {
            "last_name" : "Smith"
        }
    }
}

带过滤器的搜索

curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d'
{
    "query" : {
        "bool": {
            "must": {
                "match" : {
                    "last_name" : "smith" 
                }
            },
            "filter": {
                "range" : {
                    "age" : { "gt" : 30 } 
                }
            }
        }
    }
}
'

全文搜索
此功能即可实现相关性搜索

curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d'
{
    "query" : {
        "match" : {
            "about" : "rock climbing"
        }
    }
}
'

短语搜索
即精确搜索,使用此搜索模式将返回精确结果。

curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d'
{
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    }
}
'

高亮搜索
高亮显示搜索结果,会在搜索结果的匹配部分以<em></em>封装。

curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d'
{
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    },
    "highlight": {
        "fields" : {
            "about" : {}
        }
    }
}
'

分析

curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d'
{
  "aggs": {
    "all_interests": {
      "terms": { "field": "interests" }
    }
  }
}
'

分析的同时搜索
这种方式会匹配match字段内的值,然后再进行分析。

curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "last_name": "smith"
    }
  },
  "aggs": {
    "all_interests": {
      "terms": {
        "field": "interests"
      }
    }
  }
}
'

聚合分级汇总
按照排序进行分级分析并汇总。

curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d'
{
    "aggs" : {
        "all_interests" : {
            "terms" : { "field" : "interests" },
            "aggs" : {
                "avg_age" : {
                    "avg" : { "field" : "age" }
                }
            }
        }
    }
}
'

文档控制

放入文档
其中的website是索引,blog是类型,123是id。
使用此功能也能更新文档。

curl -X PUT "localhost:9200/website/blog/123" -H 'Content-Type: application/json' -d'
{
  "title": "My first blog entry",
  "text":  "Just trying this out...",
  "date":  "2014/01/01"
}
'

因为使用上述方法,会对文档造成更新,而有时候我们并不想更新文档。所以也可以使用以下两种方法来进行创建:

第一种方法使用 op_type 查询 -字符串参数:
PUT /website/blog/123?op_type=create
{ ... }

第二种方法是在 URL 末端使用 /_create :
PUT /website/blog/123/_create
{ ... }

这样,在成功创建时,返回201,否则返回409冲突响应码。
自动生成id

curl -X POST "localhost:9200/website/blog/" -H 'Content-Type: application/json' -d'
{
  "title": "My second blog entry",
  "text":  "Still trying this out...",
  "date":  "2014/01/01"
}
'

取回文档

curl -X GET "localhost:9200/website/blog/123?pretty"

检查文档是否存在

curl -i -XHEAD http://localhost:9200/website/blog/123

删除文档

curl -X DELETE "localhost:9200/website/blog/123"

处理并发

对于并发有两种方式:

悲观并发控制
这种方法被关系型数据库广泛使用,它假定有变更冲突可能发生,因此阻塞访问资源以防止冲突。 一个典型的例子是读取一行数据之前先将其锁住,确保只有放置锁的线程能够对这行数据进行修改。

乐观并发控制
Elasticsearch 中使用的这种方法假定冲突是不可能发生的,并且不会阻塞正在尝试的操作。 然而,如果源数据在读写当中被修改,更新将会失败。应用程序接下来将决定该如何解决冲突。 例如,可以重试更新、使用新的数据、或者将相关情况报告给用户。

乐观并发控制
指定版本号来修改应用。

curl -X PUT "localhost:9200/website/blog/1?version=1" -H 'Content-Type: application/json' -d'
{
  "title": "My first blog entry",
  "text":  "Starting to get the hang of this..."
}
'

通过外部系统使用版本控制

curl -X PUT "localhost:9200/website/blog/2?version=5&version_type=external" -H 'Content-Type: application/json' -d'
{
  "title": "My first external blog entry",
  "text":  "Starting to get the hang of this..."
}
'

文档的部分更新

curl -X POST "localhost:9200/website/blog/1/_update" -H 'Content-Type: application/json' -d'
{
   "doc" : {
      "tags" : [ "testing" ],
      "views": 0
   }
}
'

批量取回文档

curl -X GET "localhost:9200/_mget" -H 'Content-Type: application/json' -d'
{
   "docs" : [
      {
         "_index" : "website",
         "_type" :  "blog",
         "_id" :    2
      },
      {
         "_index" : "website",
         "_type" :  "pageviews",
         "_id" :    1,
         "_source": "views"
      }
   ]
}
'

使用默认的索引值甚至类型值,可以覆盖

curl -X GET "localhost:9200/website/blog/_mget" -H 'Content-Type: application/json' -d'
{
   "docs" : [
      { "_id" : 2 },
      { "_type" : "pageviews", "_id" :   1 }
   ]
}
'

结论

Elasticsearch使用十分简单方便,对于应用开发者,只需在Elasticsearch的基础上再集成自己的应用框架,即可轻松使用Elasticsearch,同时Elasticsearch也可作为NoSQL的存储方式来使用,即可以使用Elasticsearch来做存储和数据分析、检索等功能,实现上也非常强大。

猜你喜欢

转载自blog.csdn.net/qq_34911465/article/details/81150267