# Collection of code snippets by Arne Vajhøj 
# (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import urllib.request
import json

def dump(baseurl, searchterm):
    print('search for : %s' % (searchterm))
    req = urllib.request.Request(baseurl + '/select?q=' + searchterm.replace(' ', '+') + '&df=alltext', headers={ 'Accept': 'application/json'})
    rawresp = urllib.request.urlopen(req)
    resp = json.load(rawresp)
    for doc in resp['response']['docs']:
        print('  %s - %s' % (doc['names'], doc['moves']))

# all
dump('http://localhost:8888/solr/chessopening', '*:*')
# one
dump('http://localhost:8888/solr/chessopening', 'names:defense')
dump('http://localhost:8888/solr/chessopening', 'moves:d4')
# dual
dump('http://localhost:8888/solr/chessopening', 'alltext:defense')
dump('http://localhost:8888/solr/chessopening', 'alltext:d4')
dump('http://localhost:8888/solr/chessopening', 'defense') # default field
dump('http://localhost:8888/solr/chessopening', 'd4') # default field
# boolean
dump('http://localhost:8888/solr/chessopening', 'names:defense moves:d4') # implicit or
dump('http://localhost:8888/solr/chessopening', 'names:defense AND moves:d4') # and
dump('http://localhost:8888/solr/chessopening', 'names:defense NOT moves:d4') # not
dump('http://localhost:8888/solr/chessopening', 'defense d4') # default field + implicit or
dump('http://localhost:8888/solr/chessopening', 'defense AND d4') # default field + and
dump('http://localhost:8888/solr/chessopening', 'defense NOT d4') # default field + not    
# wildcard
dump('http://localhost:8888/solr/chessopening', 'names:def*')
dump('http://localhost:8888/solr/chessopening', 'moves:N*')
