日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Elasticsearch对数字检索——ngram

發布時間:2024/3/24 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Elasticsearch对数字检索——ngram 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

數字可能信息不全,需要對數字進行切分,所以選用 ngram 分詞器進行分詞

測試

POST _analyze {"tokenizer": "ngram","text":"123456" }{"tokens" : [{"token" : "1","start_offset" : 0,"end_offset" : 1,"type" : "word","position" : 0},{"token" : "12","start_offset" : 0,"end_offset" : 2,"type" : "word","position" : 1},{"token" : "2","start_offset" : 1,"end_offset" : 2,"type" : "word","position" : 2},{"token" : "23","start_offset" : 1,"end_offset" : 3,"type" : "word","position" : 3},{"token" : "3","start_offset" : 2,"end_offset" : 3,"type" : "word","position" : 4},{"token" : "34","start_offset" : 2,"end_offset" : 4,"type" : "word","position" : 5},{"token" : "4","start_offset" : 3,"end_offset" : 4,"type" : "word","position" : 6},{"token" : "45","start_offset" : 3,"end_offset" : 5,"type" : "word","position" : 7},{"token" : "5","start_offset" : 4,"end_offset" : 5,"type" : "word","position" : 8},{"token" : "56","start_offset" : 4,"end_offset" : 6,"type" : "word","position" : 9},{"token" : "6","start_offset" : 5,"end_offset" : 6,"type" : "word","position" : 10}] }

創建mapping

PUT test {"settings": {"analysis": {"analyzer": {"my_analyzer": {"tokenizer": "my_tokenizer"}},"tokenizer": {"my_tokenizer": {"type": "ngram","min_gram": 3,"max_gram": 4,"token_chars": ["letter","digit"]}}}}, "mappings": {"properties": {"name":{"type": "text","analyzer": "my_analyzer"}}} } POST test/_analyze {"analyzer": "my_analyzer","text":"渝A253DC" }{"tokens" : [{"token" : "渝A2","start_offset" : 0,"end_offset" : 3,"type" : "word","position" : 0},{"token" : "渝A25","start_offset" : 0,"end_offset" : 4,"type" : "word","position" : 1},{"token" : "A25","start_offset" : 1,"end_offset" : 4,"type" : "word","position" : 2},{"token" : "A253","start_offset" : 1,"end_offset" : 5,"type" : "word","position" : 3},{"token" : "253","start_offset" : 2,"end_offset" : 5,"type" : "word","position" : 4},{"token" : "253D","start_offset" : 2,"end_offset" : 6,"type" : "word","position" : 5},{"token" : "53D","start_offset" : 3,"end_offset" : 6,"type" : "word","position" : 6},{"token" : "53DC","start_offset" : 3,"end_offset" : 7,"type" : "word","position" : 7},{"token" : "3DC","start_offset" : 4,"end_offset" : 7,"type" : "word","position" : 8}] } POST test/_analyze {"analyzer": "my_analyzer","text":"123456" }{"tokens" : [{"token" : "123","start_offset" : 0,"end_offset" : 3,"type" : "word","position" : 0},{"token" : "1234","start_offset" : 0,"end_offset" : 4,"type" : "word","position" : 1},{"token" : "234","start_offset" : 1,"end_offset" : 4,"type" : "word","position" : 2},{"token" : "2345","start_offset" : 1,"end_offset" : 5,"type" : "word","position" : 3},{"token" : "345","start_offset" : 2,"end_offset" : 5,"type" : "word","position" : 4},{"token" : "3456","start_offset" : 2,"end_offset" : 6,"type" : "word","position" : 5},{"token" : "456","start_offset" : 3,"end_offset" : 6,"type" : "word","position" : 6}] }

錯誤提示:


The difference between max_gram and min_gram in NGram Tokenizer must be less than or equal to: [1] but was [7]. This limit can be set by changing the [index.max_ngram_diff] index level setting。
從ES 7.0 以上,需要從新對 index.max_ngram_diff 進行設置

重構mapping

PUT test {"settings": {"index.max_ngram_diff":8,"analysis": {"analyzer": {"my_analyzer": {"tokenizer": "my_tokenizer"}},"tokenizer": {"my_tokenizer": {"type": "ngram","min_gram": 1,"max_gram": 8,"token_chars": ["letter","digit"]}}}}, "mappings": {"properties": {"name":{"type": "text","analyzer": "my_analyzer"}}} } POST test/_analyze {"analyzer": "my_analyzer","text":"123456" } {"tokens" : [{"token" : "1","start_offset" : 0,"end_offset" : 1,"type" : "word","position" : 0},{"token" : "12","start_offset" : 0,"end_offset" : 2,"type" : "word","position" : 1},{"token" : "123","start_offset" : 0,"end_offset" : 3,"type" : "word","position" : 2},{"token" : "1234","start_offset" : 0,"end_offset" : 4,"type" : "word","position" : 3},{"token" : "12345","start_offset" : 0,"end_offset" : 5,"type" : "word","position" : 4},{"token" : "123456","start_offset" : 0,"end_offset" : 6,"type" : "word","position" : 5},{"token" : "2","start_offset" : 1,"end_offset" : 2,"type" : "word","position" : 6},{"token" : "23","start_offset" : 1,"end_offset" : 3,"type" : "word","position" : 7},{"token" : "234","start_offset" : 1,"end_offset" : 4,"type" : "word","position" : 8},{"token" : "2345","start_offset" : 1,"end_offset" : 5,"type" : "word","position" : 9},{"token" : "23456","start_offset" : 1,"end_offset" : 6,"type" : "word","position" : 10},{"token" : "3","start_offset" : 2,"end_offset" : 3,"type" : "word","position" : 11},{"token" : "34","start_offset" : 2,"end_offset" : 4,"type" : "word","position" : 12},{"token" : "345","start_offset" : 2,"end_offset" : 5,"type" : "word","position" : 13},{"token" : "3456","start_offset" : 2,"end_offset" : 6,"type" : "word","position" : 14},{"token" : "4","start_offset" : 3,"end_offset" : 4,"type" : "word","position" : 15},{"token" : "45","start_offset" : 3,"end_offset" : 5,"type" : "word","position" : 16},{"token" : "456","start_offset" : 3,"end_offset" : 6,"type" : "word","position" : 17},{"token" : "5","start_offset" : 4,"end_offset" : 5,"type" : "word","position" : 18},{"token" : "56","start_offset" : 4,"end_offset" : 6,"type" : "word","position" : 19},{"token" : "6","start_offset" : 5,"end_offset" : 6,"type" : "word","position" : 20}] }

延伸:edgeNGram

ngram 的簡化版,單個分詞,以首字母為起始位置,進行分詞
效果如下

PUT test {"settings": {"analysis": {"analyzer": {"my_analyzer": {"tokenizer": "my_tokenizer"}},"tokenizer": {"my_tokenizer": {"type": "edgeNGram","min_gram": 1,"max_gram": 8,"token_chars": ["letter","digit"]}}}}, "mappings": {"properties": {"name":{"type": "text","analyzer": "my_analyzer"}}} } POST test/_analyze {"analyzer": "my_analyzer","text":"123456" } {"tokens" : [{"token" : "1","start_offset" : 0,"end_offset" : 1,"type" : "word","position" : 0},{"token" : "12","start_offset" : 0,"end_offset" : 2,"type" : "word","position" : 1},{"token" : "123","start_offset" : 0,"end_offset" : 3,"type" : "word","position" : 2},{"token" : "1234","start_offset" : 0,"end_offset" : 4,"type" : "word","position" : 3},{"token" : "12345","start_offset" : 0,"end_offset" : 5,"type" : "word","position" : 4},{"token" : "123456","start_offset" : 0,"end_offset" : 6,"type" : "word","position" : 5}] }

總結

以上是生活随笔為你收集整理的Elasticsearch对数字检索——ngram的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。