Elasticsearch对数字检索——ngram
生活随笔
收集整理的這篇文章主要介紹了
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
延伸:edgeNGram
ngram 的簡化版,單個分詞,以首字母為起始位置,進行分詞
效果如下
總結
以上是生活随笔為你收集整理的Elasticsearch对数字检索——ngram的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Bootstrap书籍分类
- 下一篇: Freeipa - LDAP与autof