如何将 Solr 中的精确搜索与 space 匹配

How to match exact Search in Solr with space

当 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 的查询?

您的字段 programLocation.

需要使用 string 作为 fieldtype

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

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

String 将 word/sentence 存储为一个精确的字符串,而不对其执行任何标记化。 它在存储精确匹配的情况下很有用,例如,用于分面、排序。

string 类型相反的是 textText 对数据进行标记化,进行小写等处理。当我们要匹配句子的一部分时,它会很有帮助。

如果您想实现小写搜索,请为您的字段使用以下字段类型。

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

如果你有空格,你也可以在小写过滤器工厂之后使用下面的过滤器。

<filter class="solr.TrimFilterFactory" />

那么您的字段定义将如下所示

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