return to index |
nucular project page with download links |
This discussion describes the syntax and usage of boolean query strings illustrating the discussion with examples using the nucular archive built in the API Examples document.
result = session.result(booleanString)As explained in the API summary document result objects allow programs to find some of the matches to a query without fully extracting all of the matches to the query -- and this can allow the program to run more quickly.
For smaller queries it is often more convenient to get all matches
for the query in one step.
To find all matching dictionaries for a boolean query string
use the dictionaries
method of the session object.
For example the following Python command prompt
interaction finds all the dictionary matches mentioning "pizza" from the example
archive
$ python Python 2.5 ... >>> from nucular import Nucular >>> session = Nucular.Nucular("../testdata/APIExamples") >>> for d in session.dictionaries("pizza"): ... print d ... {'i': 'Sandy Waller', 'p': '333-2222', 'c': 'delicious pizza', 'g': 'female'} >>>
"bunny | snail"
to
find all dictionaries mentioning "bunny" or "snail" anywhere.
>>> for d in session.dictionaries("bunny | snail"): ... print d ... {'food': 'just delicious with garlic', 'i': '456BUNNY', 'name': 'bunny rabbit', 'descr': 'cute and cuddly'} {'i': 'Lola Waller', 'p': '333-2222', 'g': 'female', 'n': 'thinks snails are delicious'} >>>Here the matches for the query string
"bunny | snail"
evaluates to the
matches for the query string bunny
unioned with the matches for the query
string snail
.
"delicious ~snail"
to request all dictionaries which mention "delicious" but don't mention "snail".
>>> for d in session.dictionaries("delicious ~snail"): ... print d ... {'food': 'tastes delicious, like chicken', 'i': '123FROG', 'name': 'frog', 'descr': 'little green slimy things'} {'food': 'just delicious with garlic', 'i': '456BUNNY', 'name': 'bunny rabbit', 'descr': 'cute and cuddly'} {'i': 'Sandy Waller', 'p': '333-2222', 'c': 'delicious pizza', 'g': 'female'} >>>Here the query string
"delicious ~snail"
evaluates to the matches for the
query string delicious
intersected with the matches for the query string ~snail
.
>>> for d in session.dictionaries("(delicious | cuddly) ~snail"): ... print d ... {'food': 'tastes delicious, like chicken', 'i': '123FROG', 'name': 'frog', 'descr': 'little green slimy things'} {'food': 'just delicious with garlic', 'i': '456BUNNY', 'name': 'bunny rabbit', 'descr': 'cute and cuddly'} {'i': '789KITTEN', 'note': 'not edible', 'name': 'kitten', 'descr': 'cute and cuddly'} {'i': 'Sandy Waller', 'p': '333-2222', 'c': 'delicious pizza', 'g': 'female'}The discussions to follow present the individual atomic notations.
>>> for d in session.dictionaries("cute"): ... print d ... {'food': 'just delicious with garlic', 'i': '456BUNNY', 'name': 'bunny rabbit', 'descr': 'cute and cuddly'} {'i': '789KITTEN', 'note': 'not edible', 'name': 'kitten', 'descr': 'cute and cuddly'}The boolean query parser recognizes any alphanumeric string as a name like
this1
and also recognizes
strings enclosed by double quotes as a name like "this one"
.
>>> for d in session.dictionaries("p=3.."): ... print d ... {'i': 'Joe Blow', 'p': '333-2222', 'c': 'great at a grill', 'g': 'male'} {'i': 'Lola Waller', 'p': '333-2222', 'g': 'female', 'n': 'thinks snails are delicious'} {'i': 'Sandy Waller', 'p': '333-2222', 'c': 'delicious pizza', 'g': 'female'}
>>> for d in session.dictionaries("p:33"): ... print d ... {'i': 'Joe Blow', 'p': '333-2222', 'c': 'great at a grill', 'g': 'male'} {'i': 'Joe Smithers', 'p': '111-3333', 'c': "can't cook", 'g': 'male'} {'i': 'Lola Waller', 'p': '333-2222', 'g': 'female', 'n': 'thinks snails are delicious'} {'i': 'Sally Smithers', 'p': '111-3333', 'c': 'uses too much salt', 'g': 'female'} {'i': 'Sandy Waller', 'p': '333-2222', 'c': 'delicious pizza', 'g': 'female'} >>>
food
attribute value exactly matching tastes delicious, like chicken
.
>>> for d in session.dictionaries('food="tastes delicious, like chicken"'): ... print d ... {'food': 'tastes delicious, like chicken', 'i': '123FROG', 'name': 'frog', 'descr': 'little green slimy things'} >>>This example also illustrates that a name may be enclosed in double quotes like
"tastes delicious, like chicken"
.
i
attribute lies between
Sally
and Silly
.
>>> for d in session.dictionaries("i=[Sally:Silly]"): ... print d ... {'i': 'Sally Smithers', 'p': '111-3333', 'c': 'uses too much salt', 'g': 'female'} {'i': 'Sandy Waller', 'p': '333-2222', 'c': 'delicious pizza', 'g': 'female'} >>>
>>> for d in session.dictionaries("<3>cute..cuddly"): ... print d ... {'food': 'just delicious with garlic', 'i': '456BUNNY', 'name': 'bunny rabbit', 'descr': 'cute and cuddly'} {'i': '789KITTEN', 'note': 'not edible', 'name': 'kitten', 'descr': 'cute and cuddly'} >>>
>>> for d in session.dictionaries("cute cuddly ~delicious"): ... print d ... {'i': '789KITTEN', 'note': 'not edible', 'name': 'kitten', 'descr': 'cute and cuddly'} >>>Negations are only permitted in conjunctions with positive constraints. For example you cannot search for entries that don't contain the word delicious unless you also provide a positive search condition to search for.
>>> for d in session.dictionaries("~delicious"): ... print d ... Traceback (most recent call last): File "", line 1, in File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/nucular/Nucular.py", line 551, in dictionaries result = self.result(queryString) File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/nucular/Nucular.py", line 543, in result result = booleanQuery.booleanResult(queryString, self) File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/nucular/booleanQuery.py", line 13, in booleanResult result = getResult(parse, session, queryString) File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/nucular/booleanQuery.py", line 18, in getResult assert indicator!="NOT", "unrestricted negation not permitted "+repr((qs, parse)) AssertionError: unrestricted negation not permitted ('~delicious', ['NOT', ['anyWord', 'delicious']])