我有以下需要保存到 ES 的文件路径:
/mnt/qfs-X/Asset_Management/XG_Marketing_/Episodic-SG_1001_1233.jpg
我希望能够搜索以下内容并获得匹配项:
search = "qf episodic sg_1001 JPG"
并得到一个匹配,换句话说,它将是在(my)sql中进行如下搜索:
select * from table where fp like '%qf%' and fp like '%episodic%'
and fp like '%sg_1001%' and fp like '%jpg%'
这里有两个问题:
body = {
"mappings": {
"_doc": {
"dynamic": "strict",
"properties": {
"path": {"type": "keyword"},
}
}
}
}
"query": {
"bool": {
"must": [
{ "match": { "fp": "qf" } },
{ "match": { "fp": "episodic" } },
{ "match": { "fp": "sg_1001" } },
{ "match": { "fp": "JPG" } }
]
}
}
最佳答案
假设您的输入是这样的:
/mnt/qfs-X/Asset_Management/XG_Marketing_/Episodic-SG_1001_1233.jpg
我要做的是转换所有这些
forward slash
和 underscore
进入 whitespaces
如此有效地,您的输入现在看起来像
mnt qfs-X Asset_Management XG Marketing Episodic-SG 1001 1233.jpg
使用
standard
标记器以及 token_filter(standard and lowercase)
下面将是您最终拥有的单词列表,这些单词最终将存储在您的倒排索引中,可以查询。mnt qfs X asset management xg marketing episodic sg 1001 1233 jpg
以下是上述的示例映射和查询:
映射
PUT mysampleindex
{
"settings":{
"analysis":{
"analyzer":{
"my_analyzer":{
"tokenizer":"standard",
"char_filter":[
"my_char_filter"
],
"filter":[
"standard",
"lowercase"
]
}
},
"char_filter":{
"my_char_filter":{
"type":"pattern_replace",
"pattern":"\\/|_",
"replacement":" "
}
}
}
},
"mappings":{
"mydocs":{
"properties":{
"mytext":{
"type":"text",
"analyzer":"my_analyzer"
}
}
}
}
}
样本文件
POST mysampleindex/mydocs/1
{
"mytext": "nt/qfs-X/Asset_Management/XG_Marketing_/Episodic-SG_1001_1233.jpg"
}
示例查询
POST mysampleindex/_search
{
"query":{
"match":{
"mytext":"qfs episodic sg 1001 jpg"
}
}
}
请记住,当您将上述查询发送到 Elasticsearch 时,Elasticsearch 会接受输入并应用 Search Time Analysis也有。我建议您阅读此链接以获取有关此内容的更多信息以及即使使用以下查询字符串您也会获得文档的原因。
"mytext": "QFS EPISODIC SG 1001 jpg"
现在,如果您尝试使用
pisodic
进行搜索(e pisodic )即以查询为例,搜索不会返回任何内容,因为您的倒排索引不会以这种方式保存单词。对于这种情况,我建议您使用 N-Gram Tokenizer这样episodic
将进一步创建像 episodi, pisodic
这样的词这将存储在倒排索引中。POST mysampleindex/_search
{
"query":{
"match":{
"mytext":"pisodic"
}
}
}
另请注意,我一直在使用
text
而不是 keyword
数据类型。我希望这有帮助!
关于python - elasticsearch 小写,如 %term% search,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53094755/