如何使用正则表达式在 Solr 4 中进行查询 [英] How to use regex for querying in Solr 4

查看:68
本文介绍了如何使用正则表达式在 Solr 4 中进行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经到了绝望的地步,所以我寻求帮助.我正在尝试使用正则表达式从 Solr 4 引擎查询结果.

I've reached the point of desperation, so I'm asking for help. I'm trying to query results from a Solr 4 engine using regex.

假设我要查询的文档是:

Let's asume the document I want to query is:

<str name="text">description: best company; name: roca mola</str>

我想使用这个正则表达式查询:

And I want to query using this regex:

description:(.*)?company(.*)?;

我读了一些 论坛认为在 Solr 4 中使用正则表达式就像添加斜杠一样简单,例如:

I read in some forums that using regex in Solr 4 was as easy as adding slashes, like:

localhost:8080/solr/q=text:/description\:(.*)?company(.*)?;/

但它不起作用.而这个也不起作用:

but it isn't working. And this one doesn't work either:

localhost:8080/solr/q=text:/description(.*)?company(.*)?;/

我不想要一个简单的查询,例如:

I don't want a simple query like:

localhost:8080/solr/q=text:*company*

因为这会使文档不匹配,例如:

Since that would mismatch documents like:

<str name="text">description: my home; name: mother company"</str>

如果我不清楚,请告诉我.

If I'm not clear please let me know.

来自智利的欢呼:D

注意:我在我的方案中使用了 text_general 字段.正如@arun 指出的那样,string 字段可以处理我正在使用的正则表达式类型.

NOTE: I was using text_general fields on my scheme. As @arun pointed out, string fields can handle the type of regex I'm using.

推荐答案

不要在 text 字段类型上尝试正则表达式搜索,而是在 string 字段类型上尝试,因为您的正则表达式涵盖多个单词.(如果您的正则表达式需要匹配单个单词,那么您可以使用 text 字段.)

Instead of trying regex search on text field type, try it on a string field type, since your regex is spanning more than one word. (If your regex needs to match a single word, then you can use a text field.)

还要对特殊字符进行百分比编码,以确保它们不是导致不匹配的原因.

Also do percent encoding of special characters just to make sure they are not the cause for the mismatches.

q=strfield:/description%3A(.*?)company(.*?)%3B.*/

更新:刚刚在字符串字段上尝试过.上面的正则表达式有效.即使没有百分比编码它也能工作,即

Update: Just tried it on a string field. The above regex works. It works even without the percent encoding too i.e.

q=strfield:/description:.*?company.*?;.*/

这篇关于如何使用正则表达式在 Solr 4 中进行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆