搜索——elasticsearch学习笔记(五)

es的强大之处在于文档中的每个字段都会被索引并且可以被查询
在关系型数据库中可以利用select … from table where condition1 = text order by field…来检索数据,同样的,在es当中,可以使用“搜索”来检索你需要的数据,并且功能更加强大,搜索更加智能。

数据准备

向es里面索引以下文档,可以看到搜索时候的效果

curl -XPUT 'http://localhost:9200/us/user/1?pretty' -H 'Content-Type:application/json'  -d '
{
   "email" : "john@smith.com",
   "name" : "John Smith",
   "username" : "@john"
}
'


curl -XPUT 'http://localhost:9200/gb/tweet/3?pretty' -H 'Content-Type:application/json' -d '
{
   "date" : "2014-09-13",
   "name" : "Mary Jones",
   "tweet" : "Elasticsearch means full text search has never been so easy",
   "user_id" : 2
}
'

curl -XPUT 'http://localhost:9200/gb/tweet/5?pretty' -H 'Content-Type:application/json' -d '
{
   "date" : "2014-09-15",
   "name" : "Mary Jones",
   "tweet" : "However did I manage before Elasticsearch?",
   "user_id" : 2
}
'


curl -XPUT 'http://localhost:9200/gb/tweet/7?pretty' -H 'Content-Type:application/json'  -d '
{
   "date" : "2014-09-17",
   "name" : "Mary Jones",
   "tweet" : "The Query DSL is really powerful and flexible",
   "user_id" : 2
}
'

curl -XPUT 'http://localhost:9200/gb/tweet/13?pretty' -H 'Content-Type:application/json' -d '
{
   "date" : "2014-09-23",
   "name" : "Mary Jones",
   "tweet" : "So yes, I am an Elasticsearch fanboy",
   "user_id" : 2
}
'

一个普通的搜索

空搜索

GET gb/_search

返回结果

{
   "hits" : {
      "total" :       14,
      // 查询结果的前十个文档
      "hits" : [
        {
          "_index":   "us",
          "_type":    "tweet",
          "_id":      "7",
          // 衡量了文档与查询的匹配程度
          "_score":   1,
          "_source": {
             "date":    "2014-09-17",
             "name":    "John Smith",
             "tweet":   "The Query DSL is really powerful and flexible",
             "user_id": 2
          }
       },
        ......
      ],
      // max_score 值是与查询所匹配文档的 _score 的最大值
      "max_score" :   1
   },
   // 执行整个搜索请求耗费了多少毫秒
   "took" :           4,
   // 部分告诉我们在查询中参与分片的总数,以及这些分片成功了多少个失败了多少个
   "_shards" : {
      "failed" :      0,
      "successful" :  10,
      "total" :       10
   },
   // 告诉我们查询是否超时
   "timed_out" :      false
}

分页

GET /gb/_search?size=5&from=10 显示第三页,每页5条数据
GET /gb/_search?size=5&from=5 显示第二页,每页5条数据

注意:分页不能太深,例如查看第1000页的数据,会造成大量索引数据,并且排序返回,比较浪费时间。

轻量级搜索

就是在请求路径中拼接搜索条件,不需要在请求体内添加json来描述搜索条件:

  • 查询所有索引中tweet中包含elasticsearch的所有文档
curl -XGET 'http://localhost:9200/_search?q=tweet:elasticsearch&pretty=true'
  • 查询所有索引中含有mary的文档。es会取出文档所有的字符,拼接为一个大字符串,并进行搜索
curl -XGET 'http://localhost:9200arch?q=mary&pretty=true'

映射Mapping

——描述数据在每个字段内如何存储

映射定义了文档中的字段,字段的类型。
es支持以下字段类型:

  • 字符串: string
  • 整数 : byte, short, integer, long
  • 浮点数: float, double
  • 布尔型: boolean
  • 日期: date
  • 数组 array
  • 对象 object
  • 空字段 null
    以下就是一个映射:
{
   "gb": {
      "mappings": {
        // 类型为sweet
         "tweet": {
         	// properties下定义字段
            "properties": {
               "date": {
                  // type为字段类型
                  "type": "date",
                  "format": "strict_date_optional_time||epoch_millis"
               },
               "name": {
                  "type": "string"
               },
               "tweet": {
                  "type": "string"
               },
               "user_id": {
                  "type": "long"
               }
            }
         }
      }
   }
}

分析 Analysis

——全文是如何处理使之可以被搜索的

分析是一个过程,

string的字段映射

string的字段映射有两个很重要属性:index和analyzer,index的值可以为以下值。

index

index定义该字段需不需要被分析,被索引

  • analyzer:该字段既会被索引,又会被分析
  • not_analyzer: 该字段只会被索引,不会被分析,只能做精确值索引
  • no:该字段不会被索引,即不会被搜索

analyzer

指定在需要搜索和分析时使用的分析器类型

  • standard
  • simple
  • whitespace

分析与分析器

分析的过程是指,将字段包含的内容进行排序、过滤、同义词替换等操作,从而使其可以更加快速精准的被索引出来。
而分析器就是处理这些字段的工具,例如有些分析器可以把leep和jump作为同义词进行转换,有些分析器可以识别特定的语言,等等。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值