如何在Solr中进行带空格的精确搜索匹配

huangapple go评论143阅读模式
英文:

How to match exact Search in Solr with space

问题

如何在两个单词之间有空格时搜索单词的一部分。我的查询如下:

/select?q=*:*&fq=pageType:program&fl=programLocation&rows=100&fq=programLocation:"Mohali"

我得到的结果如下:

"response":{"numFound":3,"start":0,"docs":[
      {
        "programLocation":["Mohali"]},
      {
        "programLocation":["Mohali"]},
      {
        "programLocation":["Mohali and Hyderabad"]}]

我想要仅检索 "Mohali",但现在我得到了 "Mohali" 和 "Mohali and Hyderabad"。如何构建查询以仅获取 Mohali?

英文:

How to search a part of the word when space is present between two words. My query this like below

/select?q=*:*&fq=pageType:program&fl=programLocation&rows=100&fq=programLocation:"Mohali"

The result I am getting is as below

"response":{"numFound":3,"start":0,"docs":[
      {
        "programLocation":["Mohali"]},
      {
        "programLocation":["Mohali"]},
      {
        "programLocation":["Mohali and Hyderabad"]}]

I want to retrieve only "Mohali", but now I am getting both "Mohali" and "Mohali and Hyderabad". How to form a query to only fetch Mohali?

答案1

得分: 1

你需要将string作为fieldtype应用于你的字段programLocation

将字符串作为fieldtype应用并重新索引数据。

<field name="programLocation" type="string" indexed="true" stored="true" required="true" multiValued="false" />

String将单词/句子存储为精确字符串,不执行任何标记化操作。
它在存储精确匹配时非常有用,例如用于分面、排序。

string类型相反的是text类型。
Text对数据进行标记化处理,执行诸如转小写等处理。在需要匹配句子的一部分时很有用。

如果您想要实现小写搜索,那么请为您的字段使用以下fieldtype

<fieldType name="forExactMatch" class="solr.TextField" sortMissingLast="true" omitNorms="true">
  <analyzer>
    <!-- KeywordTokenizer不会实际进行标记化,因此整个
         输入字符串将保留为单个标记
      -->
    <tokenizer class="solr.KeywordTokenizerFactory"/>
    <!-- LowerCase TokenFilter按预期执行操作,可用于
         在排序时进行大小写不敏感的匹配
      -->
    <filter class="solr.LowerCaseFilterFactory" />
  </analyzer>
</fieldType>

如果存在空格,您还可以在小写过滤工厂之后使用以下过滤器。

<!-- TrimFilter删除任何前导或尾随空格 -->
<filter class="solr.TrimFilterFactory" />

然后,您的字段定义将如下所示。

<field name="programLocation" type="forExactMatch" indexed="true" stored="true"/>
英文:

You need to use the string as fieldtype for your field programLocation.

Apply the String as fieldtype and reindex the data.

&lt;field name=&quot;programLocation&quot; type=&quot;string&quot; indexed=&quot;true&quot; stored=&quot;true&quot; required=&quot;true&quot; multiValued=&quot;false&quot; /&gt;

String stores the word/sentence as an exact string without performing any tokenization on it.
Its useful in cases for storing exact matches, e.g, for faceting, sorting.

Opposite of string type is text.
Text does the tokenization of data, performs processing such as lower-casing etc. It is helpful in case when we want to match part of a sentence.

If you want achieve the lowercase search then use the below fieldtype for you field.

&lt;fieldType name=&quot;forExactMatch&quot; class=&quot;solr.TextField&quot; sortMissingLast=&quot;true&quot; omitNorms=&quot;true&quot;&gt;
      &lt;analyzer&gt;
        &lt;!-- KeywordTokenizer does no actual tokenizing, so the entire
             input string is preserved as a single token
          --&gt;
        &lt;tokenizer class=&quot;solr.KeywordTokenizerFactory&quot;/&gt;
        &lt;!-- The LowerCase TokenFilter does what you expect, which can be
             when you want your sorting to be case insensitive
          --&gt;
        &lt;filter class=&quot;solr.LowerCaseFilterFactory&quot; /&gt;
      &lt;/analyzer&gt;
    &lt;/fieldType&gt;

If you have spaces the you can also use the below filter after the lowercase filter factory.

<!-- The TrimFilter removes any leading or trailing whitespace -->

&lt;filter class=&quot;solr.TrimFilterFactory&quot; /&gt;

Then your field defination will look like below

&lt;field name=&quot;programLocation&quot; type=&quot;forExactMatch&quot; indexed=&quot;true&quot; stored=&quot;true&quot;/&gt;

答案2

得分: 0

用于在Java中进行与Lucene搜索的精确匹配

`?q=&quot;Your search&quot;~0`

您可以更改整数值以定义单词之间的最大距离

`?q=&quot;Your search&quot;~2`

示例:&quot;这是一个精确匹配的很棒的示例&quot;

- `?q=&quot;awesome exemple&quot;~0` 将匹配
- `?q=&quot;awesome exact match&quot;~0` 不会匹配
- `?q=&quot;awesome exact match&quot;~2` 将匹配
英文:

For an exact match with lucene search in java

?q=&quot;Your search&quot;~0

Your can play with the integer to define the max distance between words

?q=&quot;Your search&quot;~2

Exemple : "This is an awesome exemple of exact match"

  • ?q=&quot;awesome exemple&quot;~0 will match
  • ?q=&quot;awesome exact match&quot;~0 won't match
  • ?q=&quot;awesome exact match&quot;~2 will match

huangapple
  • 本文由 发表于 2020年7月23日 21:17:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63055242.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定