1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
from __future__ import with_statement
from whoosh import analysis, fields, formats
from whoosh.compat import u, xrange, permutations
from whoosh.filedb.filestore import RamStorage
from whoosh.query import spans
from whoosh.query import And, Or, Term, Phrase
from whoosh.util.testing import TempIndex
domain = ("alfa", "bravo", "bravo", "charlie", "delta", "echo")
_ix = None
def get_index():
global _ix
if _ix is not None:
return _ix
charfield = fields.FieldType(formats.Characters(),
analysis.SimpleAnalyzer(),
scorable=True, stored=True)
schema = fields.Schema(text=charfield)
st = RamStorage()
_ix = st.create_index(schema)
w = _ix.writer()
for ls in permutations(domain, 4):
w.add_document(text=u(" ").join(ls), _stored_text=ls)
w.commit()
return _ix
def test_multimatcher():
schema = fields.Schema(content=fields.TEXT(stored=True))
ix = RamStorage().create_index(schema)
domain = ("alfa", "bravo", "charlie", "delta")
for _ in xrange(3):
w = ix.writer()
for ls in permutations(domain):
w.add_document(content=u(" ").join(ls))
w.commit(merge=False)
q = Term("content", "bravo")
with ix.searcher() as s:
m = q.matcher(s)
while m.is_active():
content = s.stored_fields(m.id())["content"].split()
spans = m.spans()
for span in spans:
assert content[span.start] == "bravo"
m.next()
def test_excludematcher():
schema = fields.Schema(content=fields.TEXT(stored=True))
ix = RamStorage().create_index(schema)
domain = ("alfa", "bravo", "charlie", "delta")
for _ in xrange(3):
w = ix.writer()
for ls in permutations(domain):
w.add_document(content=u(" ").join(ls))
w.commit(merge=False)
w = ix.writer()
w.delete_document(5)
w.delete_document(10)
w.delete_document(28)
w.commit(merge=False)
q = Term("content", "bravo")
with ix.searcher() as s:
m = q.matcher(s)
while m.is_active():
content = s.stored_fields(m.id())["content"].split()
spans = m.spans()
for span in spans:
assert content[span.start] == "bravo"
m.next()
def test_span_term():
ix = get_index()
with ix.searcher() as s:
alllists = [d["text"] for d in s.all_stored_fields()]
for word in domain:
q = Term("text", word)
m = q.matcher(s)
ids = set()
while m.is_active():
id = m.id()
sps = m.spans()
ids.add(id)
original = list(s.stored_fields(id)["text"])
assert word in original
if word != "bravo":
assert len(sps) == 1
assert original.index(word) == sps[0].start
assert original.index(word) == sps[0].end
m.next()
for i, ls in enumerate(alllists):
if word in ls:
assert i in ids
else:
assert i not in ids
def test_span_first():
ix = get_index()
with ix.searcher() as s:
for word in domain:
q = spans.SpanFirst(Term("text", word))
m = q.matcher(s)
while m.is_active():
sps = m.spans()
original = s.stored_fields(m.id())["text"]
assert original[0] == word
assert len(sps) == 1
assert sps[0].start == 0
assert sps[0].end == 0
m.next()
q = spans.SpanFirst(Term("text", "bravo"), limit=1)
m = q.matcher(s)
while m.is_active():
orig = s.stored_fields(m.id())["text"]
for sp in m.spans():
assert orig[sp.start] == "bravo"
m.next()
def test_span_near():
ix = get_index()
with ix.searcher() as s:
def test(q):
m = q.matcher(s)
while m.is_active():
yield s.stored_fields(m.id())["text"], m.spans()
m.next()
for orig, sps in test(spans.SpanNear(Term("text", "alfa"),
Term("text", "bravo"),
ordered=True)):
assert orig[sps[0].start] == "alfa"
assert orig[sps[0].end] == "bravo"
for orig, sps in test(spans.SpanNear(Term("text", "alfa"),
Term("text", "bravo"),
ordered=False)):
first = orig[sps[0].start]
second = orig[sps[0].end]
assert ((first == "alfa" and second == "bravo") or (first == "bravo" and second == "alfa"))
for orig, sps in test(spans.SpanNear(Term("text", "bravo"),
Term("text", "bravo"),
ordered=True)):
text = " ".join(orig)
assert text.find("bravo bravo") > -1
q = spans.SpanNear(spans.SpanNear(Term("text", "alfa"),
Term("text", "charlie")),
Term("text", "echo"))
for orig, sps in test(q):
text = " ".join(orig)
assert text.find("alfa charlie echo") > -1
q = spans.SpanNear(Or([Term("text", "alfa"), Term("text", "charlie")]),
Term("text", "echo"), ordered=True)
for orig, sps in test(q):
text = " ".join(orig)
assert (text.find("alfa echo") > -1
or text.find("charlie echo") > -1)
def test_near_unordered():
schema = fields.Schema(text=fields.TEXT(stored=True))
st = RamStorage()
ix = st.create_index(schema)
w = ix.writer()
w.add_document(text=u("alfa bravo charlie delta echo"))
w.add_document(text=u("alfa bravo delta echo charlie"))
w.add_document(text=u("alfa charlie bravo delta echo"))
w.add_document(text=u("echo delta alfa foxtrot"))
w.commit()
with ix.searcher() as s:
q = spans.SpanNear(Term("text", "bravo"), Term("text", "charlie"),
ordered=False)
r = sorted(d["text"] for d in s.search(q))
assert r == [u('alfa bravo charlie delta echo'), u('alfa charlie bravo delta echo')]
def test_span_near_tree():
ana = analysis.SimpleAnalyzer()
schema = fields.Schema(text=fields.TEXT(analyzer=ana, stored=True))
st = RamStorage()
ix = st.create_index(schema)
w = ix.writer()
w.add_document(text=u("The Lucene library is by Doug Cutting and Whoosh " +
"was made by Matt Chaput"))
w.commit()
nq1 = spans.SpanNear(Term("text", "lucene"), Term("text", "doug"), slop=5)
nq2 = spans.SpanNear(nq1, Term("text", "whoosh"), slop=4)
with ix.searcher() as s:
m = nq2.matcher(s)
assert m.spans() == [spans.Span(1, 8)]
def test_spannear2():
schema = fields.Schema(id=fields.STORED, text=fields.TEXT)
with TempIndex(schema) as ix:
with ix.writer() as w:
w.add_document(id="a", text=u"alfa echo")
w.add_document(id="b", text=u"alfa bravo echo")
w.add_document(id="c", text=u"alfa bravo charlie echo")
w.add_document(id="d", text=u"alfa bravo charlie delta echo")
w.add_document(id="e", text=u"alfa bravo charlie fox delta echo")
w.add_document(id="f", text=u"charlie delta echo fox golf hotel")
with ix.searcher() as s:
q = spans.SpanNear2([Term("text", "bravo"), Term("text", "echo")],
slop=3)
assert q.estimate_size(s.reader()) == 4
ids = "".join(sorted(hit["id"] for hit in s.search(q)))
assert ids == "bcd"
def test_span_not():
ix = get_index()
with ix.searcher() as s:
nq = spans.SpanNear(Term("text", "alfa"), Term("text", "charlie"),
slop=2)
bq = Term("text", "bravo")
q = spans.SpanNot(nq, bq)
m = q.matcher(s)
while m.is_active():
orig = list(s.stored_fields(m.id())["text"])
i1 = orig.index("alfa")
i2 = orig.index("charlie")
dist = i2 - i1
assert 0 < dist < 3
if "bravo" in orig:
assert orig.index("bravo") != i1 + 1
m.next()
def test_span_or():
ix = get_index()
with ix.searcher() as s:
nq = spans.SpanNear(Term("text", "alfa"), Term("text", "charlie"),
slop=2)
bq = Term("text", "bravo")
q = spans.SpanOr([nq, bq])
m = q.matcher(s)
while m.is_active():
orig = s.stored_fields(m.id())["text"]
assert ("alfa" in orig and "charlie" in orig) or "bravo" in orig
m.next()
def test_span_contains():
ix = get_index()
with ix.searcher() as s:
nq = spans.SpanNear(Term("text", "alfa"), Term("text", "charlie"),
slop=3)
cq = spans.SpanContains(nq, Term("text", "echo"))
m = cq.matcher(s)
ls = []
while m.is_active():
orig = s.stored_fields(m.id())["text"]
ls.append(" ".join(orig))
m.next()
ls.sort()
assert ls == ['alfa bravo echo charlie', 'alfa bravo echo charlie',
'alfa delta echo charlie', 'alfa echo bravo charlie',
'alfa echo bravo charlie', 'alfa echo charlie bravo',
'alfa echo charlie bravo', 'alfa echo charlie delta',
'alfa echo delta charlie', 'bravo alfa echo charlie',
'bravo alfa echo charlie', 'delta alfa echo charlie',
]
def test_span_before():
ix = get_index()
with ix.searcher() as s:
bq = spans.SpanBefore(Term("text", "alfa"), Term("text", "charlie"))
m = bq.matcher(s)
while m.is_active():
orig = list(s.stored_fields(m.id())["text"])
assert "alfa" in orig
assert "charlie" in orig
assert orig.index("alfa") < orig.index("charlie")
m.next()
def test_span_condition():
ix = get_index()
with ix.searcher() as s:
sc = spans.SpanCondition(Term("text", "alfa"), Term("text", "charlie"))
m = sc.matcher(s)
while m.is_active():
orig = list(s.stored_fields(m.id())["text"])
assert "alfa" in orig
assert "charlie" in orig
for span in m.spans():
assert orig[span.start] == "alfa"
m.next()
def test_regular_or():
ix = get_index()
with ix.searcher() as s:
oq = Or([Term("text", "bravo"), Term("text", "alfa")])
m = oq.matcher(s)
while m.is_active():
orig = s.stored_fields(m.id())["text"]
for span in m.spans():
v = orig[span.start]
assert v == "bravo" or v == "alfa"
m.next()
def test_regular_and():
ix = get_index()
with ix.searcher() as s:
aq = And([Term("text", "bravo"), Term("text", "alfa")])
m = aq.matcher(s)
while m.is_active():
orig = s.stored_fields(m.id())["text"]
for span in m.spans():
v = orig[span.start]
assert v == "bravo" or v == "alfa"
m.next()
def test_span_characters():
ix = get_index()
with ix.searcher() as s:
pq = Phrase("text", ["bravo", "echo"])
m = pq.matcher(s)
while m.is_active():
orig = " ".join(s.stored_fields(m.id())["text"])
for span in m.spans():
startchar, endchar = span.startchar, span.endchar
assert orig[startchar:endchar] == "bravo echo"
m.next()
|