Search code examples
luceneazure-cognitive-search

Lucene Query - AND operator failing in Azure Search?


I have a search index of sandwiches. The index has three fields: id, meat, and bread. Each field is an Edm.String. In this index, here is a subset of my data:

ID | Meat       | Bread
-----------------------
1  | Ham        | White
2  | Turkey     | Hoagie
3  | Tuna       | Wheat
4  | Roast Beef | White
5  | Ham        | Wheat
6  | Roast Beef | Rye
7  | Turkey     | Wheat

I need to write a query that returns all ham or turkey sandwiches on wheat bread. In an attempt to do this, I've created the following:

{
  "search":"(meat:(Ham|Turkey) AND bread:\"Wheat\")",
  "searchMode":"all",
  "select":"id,meat,bread"
}

When I run this query, I'm not seeing any results. What am I missing? What am I doing wrong? I'm trying to understand full queries. Do field-level queries support the phrase operator? I'm not sure what I'm doing wrong.


Solution

  • You need to use "queryType": "full" to request the Lucene syntax. See an example on MSDN.

    That said, what you're trying to accomplish is easier and more efficiently done using filters. Assuming you make the relevant fields in your index filterable, you can use the following filter expression for your example: $filter=(meat eq 'Ham' or meat eq 'Turkey') and bread eq 'Wheat'. For more on filters, see this article. Hope this helps!