2015-12-02 154 views
0

我在elasticsearch中存儲了「付款參考號」。在Elasticsearch中搜索加密文本

它的佈局是例如:2-4-3-6358445698191095312-4-2-635844533758635433

我希望能夠或者通過

  1. 使用「整體」參考號碼搜索,以搜索由他們支付業務參考號文件,例如放入2-4-2-635844533758635433
  2. 從'開始'的參考編號的任何'部分'。例如。 2-4-2-63(..所以只返回第二個例子)

注意:我不想搜索'中間'或'結束'等僅從一開始。

無論如何,連字符令我困惑。

問題

1)我不知道我是否應該刪除它們在映射像

"char_filter" : { 
    "removeHyphen" : { 
     "type" : "mapping", 
      "mappings" : ["-=>"] 
     } 
    }, 

與否。我從來沒有以這種方式使用映射,所以不知道這是否有必要。

2)我想我需要一個'ngrams'過濾器,因爲我希望能夠從存在中搜索參考編號的一部分。我認爲像

"partial_word":{ 
    "filter":[ 
     "standard", 
      "lowercase", 
      "name_ngrams" 
     ], 
    "type":"custom", 
    "tokenizer":"whitespace" 
}, 

"name_ngrams":{ 
    "side":"front", 
     "max_gram":50, 
     "min_gram":2, 
    "type":"edgeNGram" 
}, 

我不知道如何把它放在一起的過濾器,但

"paymentReference":{ 
    "type":"string", 
    "analyzer": "??", 
    "fields":{ 
     "partial":{ 
      "search_analyzer":"???", 
      "index_analyzer":"partial_word", 
      "type":"string" 
     } 
    } 
} 

一切,我都試過,似乎總是「休息」在第二個搜索案例中。

如果我做'localhost:9200/orders/_analyze?field=paymentReference&pretty=1' -d "2-4-2-635844533758635433"它總是打破連字符作爲自己的令牌,並返回例如與2-這是'很多'的所有文件!而不是我想要的時候2-4-2-6

有人能告訴我如何映射此字段的兩種類型的搜索,我試圖實現?

更新 - 答

切實什麼瓦爾下面說。我只是稍微改變了映射,以更具體的分析器,我也不需要主索引的字符串,因爲我只是查詢部分。

映射

"paymentReference":{ 
    "type": "string", 
    "index":"not_analyzed", 
    "fields": { 
     "partial": { 
      "search_analyzer":"payment_ref", 
      "index_analyzer":"payment_ref", 
      "type":"string" 
     } 
    } 
} 

分析

"payment_ref": { 
    "type": "custom", 
    "filter": [ 
     "lowercase", 
     "name_ngrams" 
    ], 
    "tokenizer": "keyword" 
} 

過濾

"name_ngrams":{ 
    "side":"front", 
    "max_gram":50, 
    "min_gram":2, 
    "type":"edgeNGram" 
}, 

回答

0

你不需要使用映射字符過濾器這一點。

由於您只需要搜索前綴,因此您使用Edge NGram令牌過濾器處於正確的軌道。我會使用keyword標記器來確保這個術語是作爲一個整體來使用的。因此,設置此方式是這樣的:

curl -XPUT localhost:9200/orders -d '{ 
    "settings": { 
    "analysis": { 
     "analyzer": { 
     "partial_word": { 
      "type": "custom", 
      "filter": [ 
      "lowercase", 
      "ngram_filter" 
      ], 
      "tokenizer": "keyword" 
     } 
     }, 
     "filter": { 
     "ngram_filter": { 
      "type": "edgeNGram", 
      "min_gram": 2, 
      "max_gram": 50 
     } 
     } 
    } 
    }, 
    "mappings": { 
    "order": { 
     "properties": { 
     "paymentReference": { 
      "type": "string", 
      "fields": { 
      "partial": { 
       "analyzer": "partial_word", 
       "type": "string" 
      } 
      } 
     } 
     } 
    } 
    } 
}' 

然後你可以分析哪些將被收錄到您的paymentReference.partial領域:

​​

,你會得到你想要什麼,即所有前綴:

{ 
    "tokens" : [ { 
    "token" : "2-", 
    "start_offset" : 0, 
    "end_offset" : 24, 
    "type" : "word", 
    "position" : 1 
    }, { 
    "token" : "2-4", 
    "start_offset" : 0, 
    "end_offset" : 24, 
    "type" : "word", 
    "position" : 1 
    }, { 
    "token" : "2-4-", 
    "start_offset" : 0, 
    "end_offset" : 24, 
    "type" : "word", 
    "position" : 1 
    }, { 
    "token" : "2-4-2", 
    "start_offset" : 0, 
    "end_offset" : 24, 
    "type" : "word", 
    "position" : 1 
    }, { 
    "token" : "2-4-2-", 
    "start_offset" : 0, 
    "end_offset" : 24, 
    "type" : "word", 
    "position" : 1 
    }, { 
    "token" : "2-4-2-6", 
    "start_offset" : 0, 
    "end_offset" : 24, 
    "type" : "word", 
    "position" : 1 
    }, { 
    "token" : "2-4-2-63", 
    "start_offset" : 0, 
    "end_offset" : 24, 
    "type" : "word", 
    "position" : 1 
    }, { 
    "token" : "2-4-2-635", 
    "start_offset" : 0, 
    "end_offset" : 24, 
    "type" : "word", 
    "position" : 1 
    }, { 
    "token" : "2-4-2-6358", 
    "start_offset" : 0, 
    "end_offset" : 24, 
    "type" : "word", 
    "position" : 1 
    }, { 
    "token" : "2-4-2-63584", 
    "start_offset" : 0, 
    "end_offset" : 24, 
    "type" : "word", 
    "position" : 1 
    }, { 
    ... 

最後,你可以搜索任何前綴:

curl -XGET localhost:9200/orders/order/_search?q=paymentReference.partial:2-4-3 
0

不確定通配符搜索是否符合您的需求。我定義了自定義過濾器並設置了preserve_original並生成了數字部分false。以下是示例代碼:

PUT test1 
{ 
    "settings" : { 
    "analysis" : { 
     "analyzer" : { 
     "myAnalyzer" : { 
      "type" : "custom", 
      "tokenizer" : "whitespace", 
      "filter" : [ "dont_split_on_numerics" ] 
     } 
     }, 
     "filter" : { 
     "dont_split_on_numerics" : { 
      "type" : "word_delimiter", 
      "preserve_original": true, 
      "generate_number_parts" : false 
     } 
     } 
    } 
    }, 
    "mappings": { 
    "type_one": { 
     "properties": { 
     "title": { 
      "type": "text", 
      "analyzer": "standard" 
     } 
     } 
    }, 
    "type_two": { 
     "properties": { 
     "raw": { 
      "type": "text", 
      "analyzer": "myAnalyzer" 
     } 
     } 
    } 
    } 
} 

POST test1/type_two/1 
{ 
    "raw": "2-345-6789" 
} 

GET test1/type_two/_search 
{ 
    "query": { 
    "wildcard": { 
     "raw": "2-345-67*" 
    } 
    } 
}