Elasticsearch?計數(shù)分詞中的token使用實例
正文
在我們針對 text 類型的字段進行分詞時,分詞器會把該字段分解為一個個的 token。如果你對分詞器還不是很理解的話,請參考我之前的文章 “Elasticsearch: analyzer”。在分詞時,有一個叫做 token_count 的類型。該類型是 token 的計數(shù)器,也就是說,我們可以使用它來了解在索引字段時在字符串中生成的 token 數(shù)量。
我們下面用一個比較簡單的例子來進行展示。在我們的示例中,我們將索引一些書名,并且我們將過濾標題中只有 2 個 token 的書。
` PUT book_token_count_test { "mappings": { "properties": { "book_name": { "type": "text", "fields": { "size": { "type": "token_count", "analyzer": "standard" } } } } } } `
使用命令寫入文檔
我們使用如下的命令來寫入一下文檔:
POST book_token_count_test/_bulk {"index":{}} { "book_name": "Ulysses" } {"index":{}} { "book_name": "Don Quixote" } {"index":{}} { "book_name": "One Hundred Years of Solitude" }
搜索 token 文檔
我們使用如下的命令來搜索 token 數(shù)為 2 的文檔:
GET book_token_count_test/_search { "query": { "term": { "book_name.size": { "value": "2" } } } }
上面搜索的結(jié)果為:
` { "took": 273, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 1, "hits": [ { "_index": "book_token_count_test", "_id": "cxczBoYB6OPboMnB7TQu", "_score": 1, "_source": { "book_name": "Don Quixote" } } ] } } `
我們可以使用 range 查詢來檢索 book_name 中包含 3 個以上 token 的文檔,我們只會得到標題為 “One Hundred Years of Solitude” 的文檔。
GET book_token_count_test/_search { "query": { "range": { "book_name.size": { "gte": 3 } } } }
上面搜索的結(jié)果為:
` { "took": 1, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 1, "hits": [ { "_index": "book_token_count_test", "_id": "dBczBoYB6OPboMnB7TQu", "_score": 1, "_source": { "book_name": "One Hundred Years of Solitude" } } ] } } `
以上就是Elasticsearch 計數(shù)分詞中的token使用實例的詳細內(nèi)容,更多關(guān)于Elasticsearch計數(shù)分詞token的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
基于jmeter實現(xiàn)跨線程組傳遞token過程圖解
這篇文章主要介紹了基于jmeter實現(xiàn)跨線程組傳遞token,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-04-04Java數(shù)據(jù)結(jié)構(gòu)順序表的詳細講解
大家好,今天給大家?guī)淼氖琼樞虮?,我覺得順序表還是有比較難理解的地方的,于是我就把這一塊的內(nèi)容全部整理到了一起,希望能夠給剛剛進行學習數(shù)據(jù)結(jié)構(gòu)的人帶來一些幫助,或者是已經(jīng)學過這塊的朋友們帶來更深的理解,我們現(xiàn)在就開始吧2022-05-05