Elasticsearch笔记第十七篇

103 阅读4分钟

Elasticsearch核心知识篇(37)

初识搜索引擎_快速掌握query string search语法以及_all metadata原理揭秘

query string基础语法

GET /waws_index/waws_type/_search?q=test_field:test  # 在test_fields中包含test测试的词(词整体)
GET /waws_index/waws_type/_search?q=+test_field:test  # 在test_fields中包含test测试的词(词整体)
GET /waws_index/waws_type/_search?q=-test_field:test  # 在test_fields中不包含test测试的词(词整体) 反向搜索

上机实战的代码展示

GET /waws_index/waws_type/_search?q=name:waws2
    
{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "waws_index",
        "_type": "waws_type",
        "_id": "2",
        "_score": 0.2876821,
        "_source": {
          "name": "waws2"
        }
      }
    ]
  }
}

一个是掌握q=field:search content的语法,还有一个是掌握 +- 的含义

_all metadata的原理和作用

GET /test_index/test_type/_search?q=test

直接可以搜索所有的field,任意一个field包含指定的关键字就可以搜索出来

  • 我们在进行中搜索的时候,难道是对document中的每一个field都进行一次搜索吗?不是的
    • es中的_all元数据,在建立索引的时候,我们插入一条document,它里面包含了多个field,此时,es会自动将多个field的值,全部用字符串的方式串联起来,变成一个长的字符串,作为_all field的值,同时建立索引
    • 后面如果在搜索的时候,没有对某个field指定搜索,就默认搜索_all field,其中是包含了所有field的值的

举个例子

{
  "name": "jack",
  "age": 26,
  "email": "jack@sina.com",
  "address": "guamgzhou"
}

"jack 26 jack@sina.com guangzhou",作为这一条document的_all field的值,同时进行分词后建立对应的倒排索引

Elasticsearch核心知识篇(38)

初识搜索引擎_mapping到底是什么

插入几条数据,让es自动为我们建立一个索引

 PUT /waws_web/waws_article/1
 {
   "post_date": "2017-01-01",
   "title": "my first article",
   "content": "this is my first article in this website",
   "author_id": 11400
 }
 
 PUT /waws_web/waws_article/2
 {
   "post_date": "2017-01-02",
   "title": "my second article",
   "content": "this is my second article in this website",
   "author_id": 11400
 }
 
 PUT /waws_web/waws_article/3
 {
   "post_date": "2017-01-03",
   "title": "my third article",
   "content": "this is my third article in this website",
   "author_id": 11400
 }

尝试各种搜索

 GET /waws_web/waws_article/_search?q=2017           3条结果             
 GET /waws_web/waws_article/_search?q=2017-01-01         3条结果
 GET /waws_web/waws_article/_search?q=post_date:2017-01-01       1条结果
 GET /waws_web/waws_article/_search?q=post_date:2017             1条结果
  • 查看es自动建立的mapping,带出什么是mapping的知识点

    • 自动或手动为index中的type建立的一种数据结构和相关配置,简称为mapping
    • dynamic mapping,自动为我们建立index,创建type,以及type对应的mapping,mapping中包含了每个field对应的数据类型,以及如何分词等设置
    • 我们当然,后面会讲解,也可以手动在创建数据之前,先创建index和type,以及type对应的mapping
 GET /waws_web/_mapping/waws_article
 
 {
   "waws_web": {
     "mappings": {
       "waws_article": {
         "properties": {
           "author_id": {
             "type": "long"
           },
           "content": {
             "type": "text",
             "fields": {
               "keyword": {
                 "type": "keyword",
                 "ignore_above": 256
               }
             }
           },
           "post_date": {
             "type": "date"
           },
           "title": {
             "type": "text",
             "fields": {
               "keyword": {
                 "type": "keyword",
                 "ignore_above": 256
               }
             }
           }
         }
       }
     }
   }
 }
  • 搜索结果为什么不一致
    • 因为es自动建立mapping的时候,设置了不同的field不同的data type
    • 不同的data type的分词、搜索等行为是不一样的。所以出现了_all field和post_date field的搜索表现完全不一样。

Elasticsearch核心知识篇(39)

初识搜索引擎_精确匹配与全文搜索的对比分析

exact value

  • 精准搜索:我们的字段的值和我们的数据库中的数据完整匹配,才能搜索出来

  • 2017-01-01,exact value,搜索的时候,必须输入2017-01-01,才能搜索出来

  • 如果你输入一个01,是搜索不出来的

full text

  • 缩写 vs. 全程:cn vs. china
  • 格式转化:like liked likes
  • 大小写:Tom vs tom
  • 同义词:like vs love

示例如下:

  • 2017-01-01,2017 01 01,搜索2017,或者01,都可以搜索出来
  • china,搜索cn,也可以将china搜索出来
  • likes,搜索like,也可以将likes搜索出来
  • Tom,搜索tom,也可以将Tom搜索出来
  • like,搜索love,同义词,也可以将like搜索出来

就不是说单纯的只是匹配完整的一个值,而是可以对值进行拆分词语后(分词)进行匹配,也可以通过缩写、时态、大小写、同义词等进行匹配