0

我的对象上的自定义标识符Rails包括正斜杠。例如,标识符可能看起来像ncsu.edu/123456789. 当我尝试向 Solr 查询该标识符时,我将返回其中包含ncsu.edu的任何结果。Rails 对象的元数据如下:

class IntellectualObjectMetadata < ActiveFedora::RdfxmlRDFDatastream
  map_predicates do |map|
    map.intellectual_object_identifier(in: RDF::DC, to: 'identifier') do |index|
      index.as :stored_searchable
    end
  end
end

我这样查询:

IntellectualObject.where(desc_metadata__intellectual_object_identifier_tesim: params[:intellectual_object_identifier]).first

我想知道是否有人对如何标记 Solr 查询有任何提示,以便它只返回匹配整个标识符而不是部分匹配的对象。谢谢。

4

1 回答 1

1

这里的答案开始,您可以在搜索时使用反斜杠对其进行转义,因此在您的情况下:

IntellectualObject.where(desc_metadata__intellectual_object_identifier_tesim: params[:intellectual_object_identifier].gsub("/","\/")).first

注意gsub要分你/\/

编辑:您可以在此处的文档中看到:

Solr 4.0 添加了正则表达式支持,这意味着 '/' 现在是一个特殊字符,如果搜索文字正斜杠,则必须对其进行转义。

因此,如果您保存了一个令牌,就像aaa/bbb您搜索它一样aaa\/bbb

编辑#2:来自上面链接的 lucene 文档。

Lucene 支持对属于查询语法一部分的特殊字符进行转义。当前列表特殊字符是

+ - && || ! ( ) { } [ ] ^ " ~ * ? : \ /

于 2014-04-24T10:28:57.953 回答