Elasticsearch笔记第十九篇

154 阅读2分钟

Elasticsearch核心知识篇(43)

初识搜索引擎_mapping总结透彻理解

  • 往es里面直接插入数据,es会自动建立索引,同时建立type以及对应的mapping

  • mapping中就自动定义了每个field的数据类型

  • 不同的数据类型(比如说text和date),可能有的是exact value,有的是full text

    • exact value,在建立倒排索引的时候,分词的时候,是将整个值一起作为一个关键词建立到倒排索引中的;
    • full text,会经历各种各样的处理,分词,normaliztion(时态转换,同义词转换,大小写转换),才会建立到倒排索引中
  • exact value和full text类型的field就决定了,在一个搜索过来的时候,对exact value field或者是full text field进行搜索的行为也是不一样的,会跟建立倒排索引的行为保持一致

    • exact value搜索的时候,就是直接按照整个值进行匹配
    • full text query string,也会进行分词和normalization再去倒排索引中去搜索
  • 可以用es的dynamic mapping,让其自动建立mapping,包括自动设置数据类型;也可以提前手动创建index和type的mapping,自己对各个field进行设置,包括数据类型,包括索引行为,包括分词器,等等

总结:

  • mapping,就是index的type的元数据,每个type都有一个自己的mapping,决定了数据类型,建立倒排索引的行为,还有进行搜索的行为

Elasticsearch核心知识篇(44)

初识搜索引擎_mapping的核心数据类型以及dynamic mapping

核心的数据类型

  • string
  • byte,short,integer,long
  • float,double
  • boolean
  • date

dynamic mapping

  • true or false --> boolean
  • 123 --> long
  • 123.45 --> double
  • 2017-01-01 --> date
  • "hello world" --> string/text

查看mapping

 GET /index/_mapping/type
 
 {
  "waws_index": {
    "mappings": {
      "waws_type": {
        "properties": {
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "waws_name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

Elasticsearch核心知识篇(45)

初识搜索引擎_手动建立和修改mapping以及定制string类型数据是否分词

如何建立索引

  • analyzed
  • not_analyzed
  • no

修改mapping

  • 手动建立mapping

    • 只能创建index时手动建立mapping,或者新增field mapping,但是不能update field mapping
 PUT /website
 {
   "mappings": {
     "article": {
       "properties": {
         "author_id": {
           "type": "long"
         },
         "title": {
           "type": "text",
           "analyzer": "english"
         },
         "content": {
           "type": "text"
         },
         "post_date": {
           "type": "date"
         },
         "publisher_id": {
           "type": "text",
           "index": "not_analyzed"
         }
       }
     }
   }
 }
 ​
 返回结果:
 {
   "acknowledged": true,
   "shards_acknowledged": true
 }
 ​
 GET /website/_mapping/article
 ​
 {
   "website": {
     "mappings": {
       "article": {
         "properties": {
           "author_id": {
             "type": "long"
           },
           "content": {
             "type": "text"
           },
           "post_date": {
             "type": "date"
           },
           "publisher_id": {
             "type": "text"
           },
           "title": {
             "type": "text",
             "analyzer": "english"
           }
         }
       }
     }
   }
 }
  • 修改mapping
 PUT /website
 {
   "mappings": {
     "article": {
       "properties": {
         "author_id": {
           "type": "text"
         }
       }
     }
   }
 }
 
 {
   "error": {
     "root_cause": [
       {
         "type": "index_already_exists_exception",
         "reason": "index [website/eserwmIfRcy34XRpNN58jA] already exists",
         "index_uuid": "eserwmIfRcy34XRpNN58jA",
         "index": "website"
       }
     ],
     "type": "index_already_exists_exception",
     "reason": "index [website/eserwmIfRcy34XRpNN58jA] already exists",
     "index_uuid": "eserwmIfRcy34XRpNN58jA",
     "index": "website"
   },
   "status": 400
 }
  • 新增字段
 PUT /website/_mapping/article
 {
   "properties" : {
     "new_field" : {
       "type" :    "string",
       "index":    "not_analyzed"
     }
   }
 }
 
 {
   "acknowledged": true
 }

测试mapping

 GET /website/_analyze
 {
   "field": "content",
   "text": "my-dogs" 
 }
 
 {
   "tokens": [
     {
       "token": "my",
       "start_offset": 0,
       "end_offset": 2,
       "type": "<ALPHANUM>",
       "position": 0
     },
     {
       "token": "dogs",
       "start_offset": 3,
       "end_offset": 7,
       "type": "<ALPHANUM>",
       "position": 1
     }
   ]
 }
  • keyword是不会进行分词的
 GET website/_analyze
 {
   "field": "new_field",
   "text": "my dogs"
 }
 
 {
   "error": {
     "root_cause": [
       {
         "type": "remote_transport_exception",
         "reason": "[w85Pu0f][127.0.0.1:9300][indices:admin/analyze[s]]"
       }
     ],
     "type": "illegal_argument_exception",
     "reason": "Can't process field [new_field], Analysis requests are only supported on tokenized fields"
   },
   "status": 400
 }