1 | #!/usr/bin/env python
|
---|
2 |
|
---|
3 | from xapian import Document, Query, WritableDatabase, Enquire, DB_CREATE_OR_OPEN
|
---|
4 |
|
---|
5 | def search(db, query):
|
---|
6 | print("Query: %s" % query)
|
---|
7 |
|
---|
8 | enquire = Enquire(db)
|
---|
9 | enquire.set_query(query)
|
---|
10 |
|
---|
11 | for m in enquire.get_mset(0, 10):
|
---|
12 | print '%s - %s - %s' % (m.docid, m.percent, ', '.join(t.term for t in m.document))
|
---|
13 |
|
---|
14 | OWNER = 'Ome'
|
---|
15 |
|
---|
16 | db = WritableDatabase('/tmp/test.db', DB_CREATE_OR_OPEN)
|
---|
17 |
|
---|
18 | for t in ['cold', 'kids']:
|
---|
19 | doc = Document()
|
---|
20 |
|
---|
21 | doc.add_term(t)
|
---|
22 | doc.add_boolean_term(OWNER)
|
---|
23 |
|
---|
24 | db.add_document(doc)
|
---|
25 |
|
---|
26 | term_q = Query(Query.OP_OR, ['cold', 'kids'])
|
---|
27 | owner_q = Query(OWNER)
|
---|
28 | filtered_term_q = Query(Query.OP_FILTER, term_q, owner_q)
|
---|
29 |
|
---|
30 | search(db, term_q)
|
---|
31 | search(db, filtered_term_q)
|
---|
32 |
|
---|
33 | db.close()
|
---|