Question

I have been building Solr queries manually as strings and passing them to SolrNet. The queries can be complicated combinations of ANDs and ORs like this:

 _query_:"field1:[1 TO 10] OR 
          field2:[1 TO 10] OR 
          field3:[1 TO 10]"
  AND  
 _query_:"field4:(keyword)"
  AND  
 _query_:"field5:(keyword)"

This was working well, but looking into the API for SolrNet, I see there are objects I could use for each clause and then pull these objects together to form the complete query. I would much rather implement this with that approach than build and concatenate the strings.

(I should mention that I am using the eDisMax parser, which allows me to use the _query_ field as you see above.)

The API is well documented for ANDs and ORs but I need to have the ANDs and ORs grouped to handle the sitations like above--things like (a OR b) AND (c OR d). Has anyone done this before with SolrNet? Thanks!

UPDATE: I found an example that I think combines ANDs and ORs with parenthesis here. Unfortunately, this assumes that I know the structure of the query in advance. Instead, I will be creating a SolrNet query dynamically based on user input, so I can't hardcode a pattern like (a) && (b || c).

Was it helpful?

Solution

The following code should give you what you are looking for:

var queryList = new List<ISolrQuery>();
if (condition1)
    queryList.Add(new SolrMultipleCriteriaQuery(new List<ISolrQuery>
                {
                    new SolrQueryByRange<decimal>("field1", 1, 10),
                    new SolrQueryByRange<decimal>("field2", 1, 10),
                    new SolrQueryByRange<decimal>("field3", 1, 10)
                }, "OR"));
if (condition2)
    queryList.Add(new SolrQueryByField("field3", keyword));

if (condition3)
    queryList.Add(new SolrQueryByField("field4", keyword));

var finalQuery = new SolrMultipleCriteriaQuery(queryList, "AND");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top