彈性搜索索引具有具有 CPF 的資料。
{
"name": "A",
"cpf": "718.881.683-23",
}
{
"name": "B",
"cpf": "404.833.187-60",
}
我想按欄位 cpf 搜索資料,如下所示:
query: 718
output: doc with name "A"
query: 718.881.683-23
output: doc with name "A"
以上作業。
但以下不起作用。
query: 71888168323
output: doc with name "A"
在這里,我想按欄位 CPF 資料搜索檔案,但也沒有句點和連字符。
uj5u.com熱心網友回復:
您可以添加一個自定義分析器,該分析器將洗掉所有非數字字符并僅索引數字。
分析儀如下所示:
PUT test
{
"settings": {
"analysis": {
"filter": {
"number_only": {
"type": "pattern_replace",
"pattern": "\\D"
}
},
"analyzer": {
"cpf_analyzer": {
"type": "custom",
"tokenizer": "keyword",
"filter": [
"number_only"
]
}
}
}
},
"mappings": {
"properties": {
"cpf": {
"type": "text",
"analyzer": "cpf_analyzer"
}
}
}
}
然后你可以像往常一樣索引你的檔案:
POST test/_doc
{
"name": "A",
"cpf": "718.881.683-23"
}
POST test/_doc
{
"name": "B",
"cpf": "404.833.187-60"
}
搜索前綴 like718可以這樣完成:
POST test/_search
{
"query": {
"prefix": {
"cpf": "718"
}
}
}
可以像這樣使用非數字字符搜索確切的值:
POST test/_search
{
"query": {
"match": {
"cpf": "718.881.683-23"
}
}
}
最后,您還可以僅使用數字進行搜索:
POST test/_search
{
"query": {
"match": {
"cpf": "71888168323"
}
}
}
使用給定的分析器,上述所有查詢都將回傳您期望的檔案。
如果由于某種原因無法重新創建索引,則可以使用正確的分析器創建一個子欄位并就地更新資料:
PUT test/_mapping
{
"properties": {
"cpf": {
"type": "text",
"fields": {
"numeric": {
"type": "text",
"analyzer": "cpf_analyzer"
}
}
}
}
}
然后只需運行以下命令,它將重新索引所有資料并填充該cpf.numeric欄位:
POST test/_update_by_query
然后,您的所有搜索都需要在cpf.numeric現場完成,而不是cpf直接進行。
uj5u.com熱心網友回復:
718.881.683-23718 881 683 23由標準分析器標記為。因此,默認情況下,您會發現檔案 A 帶有718, 718 881, 718 and 23,但不帶有 ,7188因為該欄位中沒有這樣的標記。可能您想指定不同的分析器,例如使用邊緣 n-gram 標記器。
您可以創建一個指定過濾器的自定義分析器- 例如,如下所示的模式替換(去除所有不是數字的內容)
"my_char_filter": {
"type": "pattern_replace",
"pattern": "[^\d]",
"replacement": ""
}
和一個邊 n-gram
"my_tokenizer": {
"type": "edge_ngram",
"min_gram": 1,
"max_gram": 11,
"token_chars": [
"digit"
]
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/428931.html
標籤:弹性搜索
