匹配最接近的祖先与路径层次结构Tokenizer

问题描述:

我已经设置了一个Elasticsearch v5索引,用于将配置哈希映射到URL。匹配最接近的祖先与路径层次结构Tokenizer

{ 
"settings": { 
    "analysis": { 
    "analyzer": { 
     "url-analyzer": { 
      "type": "custom", 
      "tokenizer": "url-tokenizer" 
     } 
    }, 
    "tokenizer": { 
     "url-tokenizer": { 
      "type": "path_hierarchy", 
      "delimiter": "/" 
     } 
    } 
} 
}, 
"mappings": { 
    "route": { 
     "properties": { 
     "uri": { 
      "type": "string", 
      "index": "analyzed", 
      "analyzer": "url-analyzer" 
     }, 
     "config": { 
      "type": "object" 
     }}}}} 

我想,匹配得分最高的最长路径前缀,以便给定的文件

{ "uri": "/trousers/", "config": { "foo": 1 }} 
{ "uri": "/trousers/grey", "config": { "foo": 2 }} 
{ "uri": "/trousers/grey/lengthy", "config": { "foo": 3 }} 

当我搜索/trousers,上面的结果应该是trousers,当我搜索对于/trousers/grey/short,最高结果应为/trousers/grey

取而代之,我发现/trousers的最高结果是/trousers/grey/lengthy

如何索引和查询我的文档以实现此目的?

我有一个解决方案,喝完之后:如果我们将索引中的URI作为关键字,但是仍然在搜索输入中使用PathHierarchyTokenizer?

现在我们存储以下文档:

/trousers /trousers/grey /trousers/grey/lengthy

当我们提交/trousers/grey/short的查询时,search_analyzer可以建立输入[trousers, trousers/grey, trousers/grey/short]

我们的前两个文档将匹配,我们可以使用自定义排序来平均选择最长匹配。

现在我们的映射文件看起来是这样的:

{ 
"settings": { 
"analysis": { 
    "analyzer": { 
     "uri-analyzer": { 
      "type": "custom", 
      "tokenizer": "keyword" 
     }, 
     "uri-query": { 
       "type": "custom", 
       "tokenizer": "uri-tokenizer" 
     } 
    }, 
    "tokenizer": { 
     "uri-tokenizer": { 
      "type": "path_hierarchy", 
      "delimiter": "/" 
     } 
    } 
}}, 

"mappings": { 
    "route": { 
     "properties": { 
     "uri": { 
      "type": "text", 
      "fielddata": true, 
      "analyzer": "uri-analyzer", 
      "search_analyzer": "uri-query" 
     }, 

     "config": { 
      "type": "object" 
     } 
     } 
    } 
    } 
} 

```

和我们的查询看起来是这样的:

{ 
    "sort": { 
      "_script": { 
        "script": "doc.uri.length", 
        "order": "asc", 
        "type": "number" 
      } 
    }, 
    "query": { 
     "match": { 
     "uri": { 
       "query": "/trousers/grey/lengthy", 
       "type": "boolean" 
     } 
    } 
    } 
}