带 LIKE 子句的 Solr 查询
我正在使用 Solr,我想知道查询中是否可以有 LIKE 子句。例如,我想了解标题中包含“纽约”的所有组织。在 SQL 中,这可以写成 Name LIKE 'New York%'。
我的问题 - 如何在 Solr 中编写 LIKE 查询?
我正在使用 SolrNet 库,如果这有什么区别的话。
I'm working with Solr and I'd like to know if it is possible to have a LIKE clause in the query. For example, I want to know all organizations with "New York" in the title. In SQL, this would be written like Name LIKE 'New York%'.
My question - how do you write a LIKE query in Solr?
I'm using the SolrNet library, if that makes a difference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您只需搜索“纽约”,但首先您需要正确配置您的字段分析器。例如,您可能希望从 默认 Solr 架构。此字段类型将在空格和其他常见单词分隔符上进行标记,然后应用停用词过滤器,然后将术语小写以使搜索不区分大小写。
有关Solr wiki 中的分析器的更多信息。
You just search for "New York", but first you need to properly configure your field's analyzer. For example you might want to start with a field type like
text_general
as defined in the default Solr schema. This field type will tokenize on whitespace and other common word separators, then apply a filter of stopwords, then lowercase the terms in order to make searches case-insensitive.More information about analyzers in the Solr wiki.
如果您使用的是 solr 3.1 或更高版本,请查看
扩展 DisMax 查询解析器
,支持通配符查询。您可以在请求处理程序配置中使用edismax
启用它。然后,您可以使用类似
title:New York*
的查询,其行为与带有 like 子句的查询相同。我的答案与已接受的答案之间的主要区别在于,您甚至可以使用通配符搜索单词片段。例如,New Yorkers
在这种情况下会匹配。不幸的是,即使您使用
LowerCaseFilterFactory
,区分大小写的查询也可能会出现问题。看看这里 了解更多。自 SOLR-2438 以来,大部分问题将在 solr 3.6 版本中得到解决问题已经解决。If you're using solr 3.1 or newer, have a look at the
Extended DisMax Query Parser
, which supports wildcard queries. You can enable it using<str name="defType">edismax</str>
in the request handler configuration.Then you can use a query like
title:New York*
with the same behaviour as a query with like clause. The main difference between my answer and the accepted one is that you can even search for fragment of words using wildcards. For exampleNew Yorkers
would match in this case.Unfortunately you could have problems with case-sensitive queries even if you're using a
LowerCaseFilterFactory
. Have a look here to know more. Most of those problems will be fixed with the solr 3.6 release since the SOLR-2438 issue has been solved.您可以尝试对整个字符串建立索引,而不是将其分解为单词,使用 KeywordTokenizerFactory
这样您就可以通过查询“New York*”进行搜索,这意味着“从”开始
You can try to index the whole string instead of breaking it into words, use KeywordTokenizerFactory
so you will be able to search by query 'New York*' and it will mean 'starts from'