Beispiel #1
0
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_equal(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',
        ])
Beispiel #2
0
def test_span_near2():
    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_equal(m.spans(), [spans.Span(1, 8)])
Beispiel #3
0
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()
Beispiel #4
0
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 dist > 0 and dist < 3
            if "bravo" in orig:
                assert orig.index("bravo") != i1 + 1
            m.next()
Beispiel #5
0
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_equal(r, [
            u('alfa bravo charlie delta echo'),
            u('alfa charlie bravo delta echo')
        ])
Beispiel #6
0
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_equal(orig[sps[0].start], "alfa")
            assert_equal(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