在弹性搜索中搜索列数据的最后四位数字
Search last Four Digits of a column data in elastic search
我需要使用弹性搜索根据信用卡号的最后四位进行搜索。类似于 sql LIKE。提前致谢
对字符串末尾的字符进行 wildcard/regex 搜索(在评论中建议)效率极低。
相反,您应该:
- 在分析器中反转字符串
- 使用前缀搜索来搜索字符串
因此,123456789
将被索引为 987654321
- 然后您还将搜索词 6789
反转为 9876
并进行 9876
的前缀搜索=18=] 反对 987654321
.
要在 Elasticsearch 中进行设置,它比听起来更简单:
创建索引并定义一个新的分析器,它将在存储数据时反转数据:
curl -XDELETE 'http://localhost:9200/test'
curl -XPOST 'http://localhost:9200/test' -d '{
"analysis": {
"analyzer": {
"suffix_analyzer": {
"filter": ["lowercase", "reverse"],
"tokenizer": "keyword",
"type": "custom"}
}
}
}'
在映射中引用分析器:
curl -XPUT "http://localhost:9200/test/creditcard/_mapping" -d' {
"creditcard" : {
"properties": {
"cardnumber":{"type" : "string", "analyzer" : "suffix_analyzer"}
}
}
}'
post一些数据(注意卡号不是反的):
curl -XPOST 'http://localhost:9200/test/creditcard' -d'
{
"cardnumber": "1234567890"
}'
然后match_phrase_prefix查询数据:
curl -XGET 'http://localhost:9200/test/creditcard/_search?pretty' -d '{
"query": { "match_phrase_prefix": { "cardnumber" : "7890"} }
}'
然后你应该取回你的数据:
{
"took" : 38,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.30685282,
"hits" : [ {
"_index" : "test",
"_type" : "creditcard",
"_id" : "AUxpxo4tY42JLXs1QAbE",
"_score" : 0.30685282,
"_source":
{
"cardnumber": "1234567890"
}
我需要使用弹性搜索根据信用卡号的最后四位进行搜索。类似于 sql LIKE。提前致谢
对字符串末尾的字符进行 wildcard/regex 搜索(在评论中建议)效率极低。
相反,您应该:
- 在分析器中反转字符串
- 使用前缀搜索来搜索字符串
因此,123456789
将被索引为 987654321
- 然后您还将搜索词 6789
反转为 9876
并进行 9876
的前缀搜索=18=] 反对 987654321
.
要在 Elasticsearch 中进行设置,它比听起来更简单:
创建索引并定义一个新的分析器,它将在存储数据时反转数据:
curl -XDELETE 'http://localhost:9200/test'
curl -XPOST 'http://localhost:9200/test' -d '{
"analysis": {
"analyzer": {
"suffix_analyzer": {
"filter": ["lowercase", "reverse"],
"tokenizer": "keyword",
"type": "custom"}
}
}
}'
在映射中引用分析器:
curl -XPUT "http://localhost:9200/test/creditcard/_mapping" -d' {
"creditcard" : {
"properties": {
"cardnumber":{"type" : "string", "analyzer" : "suffix_analyzer"}
}
}
}'
post一些数据(注意卡号不是反的):
curl -XPOST 'http://localhost:9200/test/creditcard' -d'
{
"cardnumber": "1234567890"
}'
然后match_phrase_prefix查询数据:
curl -XGET 'http://localhost:9200/test/creditcard/_search?pretty' -d '{
"query": { "match_phrase_prefix": { "cardnumber" : "7890"} }
}'
然后你应该取回你的数据:
{
"took" : 38,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.30685282,
"hits" : [ {
"_index" : "test",
"_type" : "creditcard",
"_id" : "AUxpxo4tY42JLXs1QAbE",
"_score" : 0.30685282,
"_source":
{
"cardnumber": "1234567890"
}