简体   繁体   English

如何配置Solr对字段名称(不是值)执行不区分大小写的搜索?

[英]How can I configure Solr to perform case-insensitive searches on field names (not values)?

In my Solr core, I have a field defined like this: 在我的Solr核心中,我有一个定义如下的字段:

<field name="firstName" type="text_general" multiValued="false" indexed="true" stored="true"/>

I can query this field using something like this: "firstName:nathan". 我可以使用以下内容查询此字段:​​“ firstName:nathan”。 However, I'd like to be able to also search on this field with any of these: 但是,我希望也可以使用以下任意一项在此字段上进行搜索:

  • "FirstName:nathan" “姓:弥敦道”
  • "firstname:nathan" “姓:弥敦道”
  • "FIRSTNAME:nathan" “姓:弥敦道”

Is it possible to configure Solr to allow for case-insensitive searching on field names? 是否可以将Solr配置为允许对字段名称进行不区分大小写的搜索?

Note that I'm not asking about case-insensitive searching on the field's value - this question has already been answered many times on StackOverflow. 请注意,我并不是在询问对字段的不区分大小写的搜索-在StackOverflow上已经多次回答了这个问题。

The short answer is no (as of Solr's default config) 简短的答案是“否”(根据Solr的默认配置)

The long answer is yes, BUT you'll need to code a little... 长答案是肯定的,但是您需要编写一些代码...

I guess the best option is to change it in your application before it hits Solr both in index and query time. 我猜最好的选择是在索引和查询时间都达到Solr之前在应用程序中对其进行更改。 But if indexing is under your control and querying is not, and you need Solr to handle this only in query time, you can customize the query parser for what you need. 但是,如果索引控制在您的控制之下,而查询不在控制之下,并且您需要Solr仅在查询时间内进行处理,则可以根据需要自定义查询解析器。 To do so, you'll need to: 为此,您需要:

1- Write a QParserPlugin: 1-编写一个QParserPlugin:

package com.hoss.solr;

import org.apache.solr.common.params.SolrParams;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.search.LuceneQParserPlugin;
import org.apache.solr.search.QParser;

/**
 * @author alehoss
 */
public class MyQParserPlugin extends LuceneQParserPlugin {

    @Override
    public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
        return new MyQParser(qstr, localParams, params, req);
    }

}

2- Write a QParser: 2-编写QParser:

package com.hoss.solr;

import org.apache.lucene.search.Query;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.search.LuceneQParser;
import org.apache.solr.search.SyntaxError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author alehoss
 */
public class MyQParser extends LuceneQParser {

    private static final Logger log = LoggerFactory.getLogger(MyQParser.class);

    public MyQParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
        super(qstr, localParams, params, req);
    }

    @Override
    public Query parse() throws SyntaxError {
        String qstr = getString();
        if (qstr == null || qstr.length()==0) return null;
        log.warn("original query = " + qstr + "; querying for " + qstr.toLowerCase());
        setString(qstr.toLowerCase());
        return super.parse();
    }

}

3- Export this into a JAR and add it to your solr contrib/custom directory (you can create it with another name); 3-将其导出到JAR中并将其添加到您的solr contrib / custom目录中(可以使用其他名称创建它);

4- Reference it in your solrconfig.xml: 4-在您的solrconfig.xml中引用它:

<lib dir="${solr.install.dir:../../../..}/contrib/custom" regex=".*\.jar" />

5- Change the query parser for the handler you want to customize (/select, for example). 5-更改您要自定义的处理程序的查询解析器(例如,/ select)。 The important here is the defType parameter, which references myparser : 此处重要的是defType参数,该参数引用myparser

<requestHandler name="/select" class="solr.SearchHandler">
    <!-- default values for query parameters can be specified, these
         will be overridden by parameters in the request
      -->
    <lst name="defaults">
      <str name="echoParams">explicit</str>
      <str name="defType">myparser</str>
      <int name="rows">10</int>
      <!-- <str name="df">text</str> -->
    </lst>
.
.
.
</requestHandler>

6 - Uncomment or declare your queryParser: 6-取消注释或声明您的queryParser:

<queryParser name="myparser" class="com.hoss.solr.MyQParserPlugin"/>

In this hurry example I didn't consider lowercasing only the field name. 在这个匆忙的示例中,我没有考虑仅将字段名称大写。 So it'll lowercase the entire query ('q' parameter), including the values , but if this is a problem (if you do have a field where tokens aren't lowercased), you can change the implementation and parse the query string to achieve your needs. 因此,它将对整个查询(“ q”参数)(包括值)进行小写转换 ,但是如果这是一个问题(如果您确实拥有不对标记进行小写转换的字段),则可以更改实现并解析查询字符串满足您的需求。

Another thing is, considering this example, you'll need to declare all your field names in lowercase, not with camel case like your example. 另一件事是,考虑此示例,您需要用小写声明所有字段名,而不是像示例中那样使用驼峰大写。 It doesn't matter how the user do the input, the query will always be done with lowercase field name, so the field name must exist in your schema in this form. 不管用户如何进行输入,查询都将始终使用小写的字段名进行,因此字段名必须以这种形式存在于您的模式中。

With this you'll be able to search for any of these: 有了这个,您将可以搜索以下任何一个:

  • "FirstName:nathan" “姓:弥敦道”
  • "firstname:nathan" “姓:弥敦道”
  • "FIRSTNAME:nathan" “姓:弥敦道”

And the query will be performed always with "firstname:nathan". 并且查询将始终使用“ firstname:nathan”执行。 So, the definition of the field in your schema must be name="firstname" 因此,架构中字段的定义必须为name =“ firstname”

Downside: you won't be able to search for a field name or dynamic field name that is indexed with capitalized letters, but if you control indexing, it's not a problem. 缺点:您将无法搜索以大写字母索引的字段名或动态字段名,但是,如果您控制索引编制,则不会有问题。

My example was built on top of LuceneParser (the default parser for Solr), but you can choose another one or create an entirely new. 我的示例基于LuceneParser(Solr的默认解析器)构建,但是您可以选择另一个示例或创建一个全新的示例。 Here's some useful docs about this: 以下是一些有用的文档:

Oficial Solr doc about parsing: https://lucene.apache.org/solr/guide/6_6/query-syntax-and-parsing.html 有关解析的官方Solr文档: https : //lucene.apache.org/solr/guide/6_6/query-syntax-and-parsing.html

Nice article with full example similar to the one posted here: https://medium.com/@wkaichan/custom-query-parser-in-apache-solr-4634504bc5da 不错的文章,带有与此处发布的示例相似的完整示例: https : //medium.com/@wkaichan/custom-query-parser-in-apache-solr-4634504bc5da

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM