4

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

javanna
  • 59,145
  • 14
  • 144
  • 125
Luke101
  • 63,072
  • 85
  • 231
  • 359

3 Answers3

5

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.

Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
  • wow..very interesting. When someone types "rocket scientist" in the query box then I pass this to solrnet. What do I put for "mytitle" and "mydescription"? – Luke101 Oct 16 '11 at 15:23
  • @Luke101 : they're the same... just sample variables representing user input. E.g. mytitle = mydescription = Request.QueryString["q"]; – Mauricio Scheffer Oct 16 '11 at 22:23
  • Thank you..I went ahead and implemented this solution. – Luke101 Oct 18 '11 at 00:47
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
Jayendra
  • 52,349
  • 4
  • 80
  • 90
0

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;
    }
Aravindan
  • 855
  • 6
  • 15