ES建立mapping時欄位別名

來源:互聯網
上載者:User

標籤:方法   map   解決   分詞   lang   settings   tokenizer   must   結構   

        ES預設是動態建立索引和索引類型的mapping的,但是在學習的時候還能這樣用,在生產中一定是手動制定mapping!在生產中經常會遇到這樣的需求,想用某個欄位進行統計,又想對該欄位進行模糊查詢,解決這種需求的方法就是對該欄位建立別名!

       mapping結構如下:

 1 { 2     "settings" : { 3         "index" : { 4             "analysis" : { 5                 "filter" : { 6                     "english_keywords" : { 7                         "type" : "keyword_marker", 8                         "keywords" : [ 9                             "topsec"10                         ]11                     },12                     "english_stemmer" : {13                         "type" : "stemmer",14                         "language" : "english"15                     },16                     "english_possessive_stemmer" : {17                         "type" : "stemmer",18                         "language" : "possessive_english"19                     },20                     "english_stop" : {21                         "type" : "stop",22                         "stopwords" : "_english_"23                     }24                 },25                 "analyzer" : {26                     "default" : {27                         "tokenizer" : "keyword"28                     },29                     "english" : {30                         "type" : "custom",31                         "filter" : [32                             "lowercase",33                             "english_stop"34                         ],35                         "tokenizer" : "standard"36                     },37                     "ik" : {38                         "filter" : ["lowercase"],39                         "type" : "custom",40                         "tokenizer" : "ik_max_word"41                     },42                     "html" : {43                         "filter" : [44                             "lowercase",45                             "english_stop"46                         ],47                         "char_filter" : [48                             "html_strip"49                         ],50                         "type" : "custom",51                         "tokenizer" : "standard"52                     },53                     "lower" : {54                         "filter" : "lowercase",55                         "type" : "custom",56                         "tokenizer" : "keyword"57                     }58                 }59             },60             "number_of_shards" : "1",61             "number_of_replicas" : "0"62         }63     },64     "mappings" : {65         "test" : {66             "_all" : {67                 "enabled" : false68             },69             "properties" : {70                 "name" : {71                     "type" : "keyword"72                 },73                 "age" : {74                     "type" : "keyword",75                     "fields" : {76                         "cn" : {77                             "analyzer" : "ik",78                             "type" : "text"79                         }80                     }81                 },82 83                 "address" : {84                     "type" : "text"85                 }86             }87         }88     }89 }

欄位age的"type" : "keyword",不分詞,然後起個別名cn,對它使用ik分詞器進行分詞!插入四條資料

用age欄位對資料進行統計的時候,需要用不分詞的age,並且需要使用全匹配規則,語句:

 1 { 2   "query": { 3     "bool": { 4       "must": [ 5         { 6           "term": { 7             "age": "北京市海澱區西二旗中關村西門" 8           } 9         }10       ],11       "must_not": [],12       "should": []13     }14   },15   "from": 0,16   "size": 10,17   "sort": [],18   "aggs": {}19 }

結果:

使用age的分詞age.cn進行統計是有問題的,啟動並執行結果說明對age的別名age.cn進行分詞,查詢條件必須匹配分詞器對age的內容進行分詞的結果進行匹配,

 1 { 2   "query": { 3     "bool": { 4       "must": [ 5         { 6           "term": { 7             "age.cn": "北京市海澱區西二旗中關村西門" 8           } 9         }10       ],11       "must_not": [],12       "should": []13     }14   },15   "from": 0,16   "size": 10,17   "sort": [],18   "aggs": {}19 }

結果:

 1 { 2   "query": { 3     "bool": { 4       "must": [ 5         { 6           "term": { 7             "age.cn": "北京市" 8           } 9         }10       ],11       "must_not": [],12       "should": []13     }14   },15   "from": 0,16   "size": 10,17   "sort": [],18   "aggs": {}19 }

結果:

如果使用match來統計的話也會有問題,會把不正確的資料也統計出來,使用 match進行統計會把查詢條件與內容進行匹配,根據匹配度進行打分,分數高的說明匹配度高,會排在上面

 1 { 2   "query": { 3     "bool": { 4       "must": [ 5         { 6           "match": { 7             "age.cn": "北京市海澱區西二旗中關村" 8           } 9         }10       ],11       "must_not": [],12       "should": []13     }14   },15   "from": 0,16   "size": 10,17   "sort": [],18   "aggs": {}19 }

結果:

 

 下面就是按匹配度打分排名的結果

 1 { 2   "query": { 3     "bool": { 4       "must": [ 5         { 6           "match": { 7             "age.cn": "北京市昌平區" 8           } 9         }10       ],11       "must_not": [],12       "should": []13     }14   },15   "from": 0,16   "size": 10,17   "sort": [],18   "aggs": {}19 }

結果:

 

 總結:統計就用term,不分詞,全匹配;模糊查詢就用match,分詞,不用全匹配!

若有不正之處,請諒解和批評指正,不勝感激!!!!!歡迎大家留言討論!!!

ES建立mapping時欄位別名

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.