以not_analyzed进行映射后获得完全匹配

拉姆赞

我有如下映射的elasticsearch类型,

mappings": {
 "jardata": {
   "properties": {
     "groupID": {
      "index": "not_analyzed",
      "type": "string"
      },
     "artifactID": {
     "index": "not_analyzed",
     "type": "string"
      },
      "directory": {
      "type": "string"
      },
      "jarFileName": {
      "index": "not_analyzed",
      "type": "string"
      },
      "version": {
      "index": "not_analyzed",
      "type": "string"
      }
    }
  }
}

我使用的是分析后的目录索引,因为我只想给出最后一个文件夹并获取结果,但是当我要搜索特定目录时,我需要给出整个路径,因为在两个路径中可以有相同的文件夹。这里的问题是,因为将对它进行分析,然后将所有数据代替我想要的特定数据。

这里的问题是我想像已分析的和未分析的那样进行操作。有办法吗?

乔安娜

假设您已将以下文档编入索引:

{
    "directory": "/home/docs/public"
}

在您的情况下,标准分析器还不够,因为它会在建立索引时创建以下术语:

[home, docs, public]

请注意,它会丢失[/home/docs/public]标记-像“ /”等字符在此处充当分隔符。

一种解决方案是将NGram标记生成器与列表中的punctuation字符类一起使用token_charsElasticsearch会将“ /”视为字母或数字。这将允许使用以下标记进行搜索:

[/hom, /home, ..., /home/docs/publi, /home/docs/public, ..., /docs/public, etc...]

索引映射:

{
    "settings": {
        "analysis": {
          "analyzer": {
            "ngram_analyzer": {
              "tokenizer": "my_tokenizer"
            }
          },
          "tokenizer": {
            "my_tokenizer": {
              "type": "ngram",
              "min_gram": 4,
              "max_gram": 18,
              "token_chars": [
                "letter",
                "digit",
                "punctuation"
              ]
            }
          }
        }
      },
    "mappings": {
     "jardata": {
       "properties": {
          "directory": {
          "type": "string",
          "analyzer": "ngram_analyzer"
          }
        }
      }
    }
}

现在这两个搜索查询:

{
    "query": {
      "bool" : {
        "must" : {
          "term" : {
             "directory": "/docs/private"
           }
        }
      }
    }
}

{
    "query": {
      "bool" : {
        "must" : {
          "term" : {
             "directory": "/home/docs/private"
           }
        }
      }
    }
}

将给出结果中的索引文件。

您必须考虑的一件事是在"max_gram"设置中指定的令牌的最大长度如果是目录路径,则可能需要更长的时间。

另一种解决方案是使用Whitespace tokenizer,它将短语仅在空白上分解为术语,并使用具有以下映射的NGram过滤器

{
    "settings": {
        "analysis": {
            "filter": {
                "ngram_filter": { 
                    "type": "ngram",
                    "min_gram": 4,
                    "max_gram": 20
                }
            },
            "analyzer": {
                "my_analyzer": {
                    "type":      "custom",
                    "tokenizer": "whitespace",
                    "filter": [
                        "lowercase",
                        "ngram_filter" 
                    ]
                }
            }
        }
    },
  "mappings": {
   "jardata": {
     "properties": {
        "directory": {
        "type": "string",
        "analyzer": "my_analyzer"
        }
      }
    }
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何获得多个完全匹配词组

Elasticsearch:根映射定义具有不受支持的参数索引:not_analyzed

Spring Elasticsearch HashMap [String,String]映射值不能not_analyzed

展开并匹配后对数组进行分组

Elasticsearch-将字段从not_analyzed更改为分析

在Nest 5.5.0中为属性设置not_analyzed

Elasticsearch:如何在嵌套字段中获得完全匹配

进行匹配后从文件中删除行

如何从Google Apps脚本中的DriveApp.searchFiles中获得完全匹配的/完全匹配的单词

Regexp_Like获得完全的字符串匹配

MySQL全文搜索-搜索具有完全匹配和通配符的单词,但首先获得完全匹配的其他匹配项

如何从另一张纸上获得完全匹配的数据?

在for循环中使用Grepl获得完全匹配

如何使用fnFilter获得完全匹配?

Matlab:使用strfind获得完全匹配

完全匹配,如果没有完全匹配,则在单个查询中进行部分匹配?

映射时在ElasticSearch中优先于ngram匹配进行完全匹配

与PHP进行预匹配后获得下一个单词

在elasticsearch for kibana中有一个not_analyzed的字段

用于Elasticsearch的River Plugin Not_analyzed选项

如何使用FnFilter()在多选中获得完全匹配?

elasticsearch not_analyzed不起作用

Lucene如何索引not_analyzed字段

弹性搜索中的“ index”:“ not_analyzed”

映射中的not_analyzed被忽略

匹配后仅对给定行数进行Grep

惯用完全匹配与后跳过

无法在 elasticsearch 6.1.2 的映射中插入 "index":"not_analyzed"

如何将 not_analyzed 应用于字段