Solr: How to search multiple fields

2019-04-12 05:45发布

问题:

I am using solrnet. I have a title and Description fields. I need to search both fields simultaneously. How do I do this?

回答1:

Jayendra's answer is correct, but if you want to do this without aggregating data in a single field at index-time (copyFields) and want to do it at query-time instead using the standard handler instead of dismax, in SolrNet you can do:

var query = Query.Field("title").Is(mytitle) || Query.Field("Description").Is(mydescription);
var results = solr.Query(query);

See query operators and DSL for more information.



回答2:

If you are using a standard request handler -
Create a new field title_description and copy the title and description field to this field.
Use that field as the default search field.

<defaultSearchField>title_description</defaultSearchField>

Query q fired with search on the default search field -

q=bank

OR

If you can use dismax or edismax query parser, you can define a new request handler.
Define the query fields as qf.

<requestHandler name="dismax" class="solr.SearchHandler">
   <lst name="defaults">
     <str name="echoParams">explicit</str>
     <!-- Query settings -->
     <str name="defType">edismax</str>
     <str name="qf">
        title description
     </str>
     <str name="q.alt">*:*</str>
     <str name="rows">10</str>
     <str name="fl">*,score</str>
   </lst>
</requestHandler>

Query - pass the dismax as the qt parameter which would search on the title and description fields

q=bank&qt=dismax


回答3:

Please try to pass the string array that contains multiple field names and search text in the below method. I will return the solrnet query for search with multiple filed name with OR condition.

public ISolrQuery BuildQuery(string[] SearchFields, string SearchText)
    {

        AbstractSolrQuery firstQuery = new SolrQueryByField(SearchFields[0], SearchText) { Quoted = false };
        for (var i = 1; i < SearchFields.Length; i++)
        {
            firstQuery = firstQuery || new SolrQueryByField(SearchFields[i], SearchText) { Quoted = false };
        }

        return firstQuery;
    }