使Solr忽略斜杠?

时间:2019-01-31 14:45:40

标签: solr lucene

我有一个索引字段url,其中某些文档的url字段末尾带有斜杠,而其他文档则没有。我想在Solr中查询此字段,而忽略尾部的斜杠,以便所有匹配的结果都回来,无论它们末尾是否有尾部的斜杠。

例如,如果我发送查询:q=url:https://www.test.com/api/v1/test/,我想取回具有urlhttps://www.test.com/api/v1/test/的{​​{1}}字段的所有文档。

类似地,如果我发送不带斜杠的查询,我希望获得与上述相同的结果(https://www.test.com/api/v1/test带有OR且不带斜杠)。

这怎么办?我知道Solr通配符功能,但我不想忽略任何结尾字符(例如url不应返回url:https://www.test.com/api/v1/test?url的文档)。我只希望对通配符效果使用一个斜杠。

如果有帮助,我正在使用Solr 4(是的,我知道它很旧)。

2 个答案:

答案 0 :(得分:2)

我可能会在索引编制时使用Regex替换,并将^(.*)(?:\/)?$替换为$1 ...

将这些作为输入:

url:https://www.test.com/api/v1/test?
url:https://www.test.com/api/v1/test/
url:https://www.test.com/api/v1/test

它应该存储:

url:https://www.test.com/api/v1/test?
url:https://www.test.com/api/v1/test
url:https://www.test.com/api/v1/test

如果您随后在查询中执行相同的替换操作,则查询应符合预期:带有或不带有斜杠的查询将匹配存储的带有或不带有斜杠的URL。

答案 1 :(得分:0)

您可以使用PathHierarchyTokenizer。另外,您还可以搜索URL的某些部分,而无需使用通配符。

例如q=url:www.test.com也将与索引URL匹配。

这需要在您的 schema.xml

中使用合适的fieldType
<fieldType name="url" class="solr.TextField" positionIncrementGap="100">
  <analyzer>
    <tokenizer class="solr.PathHierarchyTokenizerFactory" delimiter="/"/>
    <filter class="solr.LowerCaseFilterFactory"/>
  </analyzer>
</fieldType>

然后添加或更改文档的字段定义以使用该url fieldType,也可以在 schema.xml

中使用
<field name="url" type="url" indexed="true" stored="true"/>
相关问题