Search code examples
solrsolr-query-syntax

Solr Query with 'in' and 'not in' for the same field


I'm completely new to Solr and, I'm trying to create a query that will return all the documents with one or more values in the 'ingredient' field (tab separated String), but exclude those that have some other values on the same field. We're running Solr 4.9

Here the schema:

<fields>  
<field name="_version_" type="long" indexed="true" stored="true" multiValued="false"/>
<field name="uri" type="string" indexed="true" stored="true" required="true" multiValued="false" /> 
<field name="title"  type="text_general"    indexed="true"  stored="true" multiValued="false" />
<field name="text"  type="text_general"    indexed="true"  stored="true" multiValued="false" />
<field name="ingredient"  type="tab-delimited-string"    indexed="true"  stored="true" multiValued="false" />

Here a sample of a document:

  {
    "text": "Preheat oven to 350 degrees F.\n\nPlace potatoes, heavy cream, egg, salt, parmesan and marjoram in a blender container.\n\nCover and blend at high speed for about 30 seconds.\n\nFill dough in a greased baking dish and bake uncovered for 25-30 minutes at 350 F.\n\nPlace remaining ingredients (without the mozzarella) in a blender, cover and blend at high speed for about 10 seconds.\n\nSpread sauce on potato dough, cover with mozzarella and some sprinkles of oil and bake again uncovered for 20-25 minutes at 350 F.",
    "ingredient": "1/2 cup parmesan cheese, grind\t1 lb potato, uncooked,skin removed,diced\t1/2 cup heavy cream\t1 egg\t1 teaspoon salt\t1/2 teaspoon marjoram\t2 cloves garlic, chopped\t1 1/3 cups Tomatoes, diced\tsalt\tpepper\tbasil\t1 cup mozzarella cheese, finely grated\tolive oil",
    "uri": "http://www.food.com/recipe/potato-pizza-58181",
    "title": "Potato Pizza",
    "_version_": 1477518762807656400
  },

After searching and reading some of the manuals I've come up with this:

title:pizza AND ingredient:"olive oil"  AND -ingredient:("beer" "potatoes")

but it doesn't work. I've also tried the stuff below but it doesn't work either

title:pizza AND ingredient:"olive oil"  AND -ingredient:"beer " "potatoes"
title:pizza AND ingredient:"olive oil"  AND -ingredient:"beer" AND -ingredient:"potatoes"

I get results that have potatoes or beer in the ingredient field. Any ideas?, am I missing something?.

Thanks!.


Solution

  • I figured this one out, I just needed to pre/append * to my search terms.

    title:pizza AND ingredient:"olive oil"  AND -ingredient:(*beer* *potatoes*)