summaryrefslogtreecommitdiff
path: root/nouveau/src/test/java/org/apache/couchdb/nouveau/lucene9/NouveauQueryParserTest.java
blob: 4c1e23d2ab1081855da5948a5f62c3ad51cd9cdd (plain)
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
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.apache.couchdb.nouveau.lucene9;

import static org.assertj.core.api.Assertions.assertThat;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.DoublePoint;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TermRangeQuery;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.util.BytesRef;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class NouveauQueryParserTest {

    private static final String DEFAULT_FIELD = "foo";

    private static NouveauQueryParser qp;

    @BeforeAll
    public static void setup() {
        qp = new NouveauQueryParser(new StandardAnalyzer());
    }

    @Test
    public void testTermQuery() throws Exception {
        assertThat(qp.parse("foo:bar", DEFAULT_FIELD)).isEqualTo(new TermQuery(new Term("foo", "bar")));
    }

    @Test
    public void testPrefixQuery() throws Exception {
        assertThat(qp.parse("foo:bar*", DEFAULT_FIELD)).isEqualTo(new PrefixQuery(new Term("foo", "bar")));
    }

    @Test
    public void testWildcardQuery() throws Exception {
        assertThat(qp.parse("foo:ba*r", DEFAULT_FIELD)).isEqualTo(new WildcardQuery(new Term("foo", "ba*r")));
    }

    @Test
    public void testStringRangeQuery() throws Exception {
        assertThat(qp.parse("foo:[bar TO foo]", DEFAULT_FIELD)).isEqualTo(new TermRangeQuery("foo",
                new BytesRef("bar"), new BytesRef("foo"), true, true));
    }

    @Test
    public void testMixedRangeQuery() throws Exception {
        assertThat(qp.parse("foo:[12.0 TO foo]", DEFAULT_FIELD)).isEqualTo(new TermRangeQuery("foo",
                new BytesRef("12.0"), new BytesRef("foo"), true, true));
    }

    @Test
    public void testInferredPointQuery() throws Exception {
        assertThat(qp.parse("foo:12", DEFAULT_FIELD)).isEqualTo(DoublePoint.newExactQuery("foo", 12.0));
    }

    @Test
    public void testInferredPointRangeQuery() throws Exception {
        assertThat(qp.parse("foo:[1 TO 12]", DEFAULT_FIELD))
                .isEqualTo(DoublePoint.newRangeQuery("foo", new double[] { 1 }, new double[] { 12 }));
    }

    @Test
    public void testOpenLeftPointRangeQuery() throws Exception {
        assertThat(qp.parse("foo:[* TO 100.0]", DEFAULT_FIELD))
                .isEqualTo(DoublePoint.newRangeQuery("foo", new double[] { Double.NEGATIVE_INFINITY },
                        new double[] { 100 }));
    }

    @Test
    public void testOpenRightPointRangeQuery() throws Exception {
        assertThat(qp.parse("foo:[1.0 TO *]", DEFAULT_FIELD))
                .isEqualTo(DoublePoint.newRangeQuery("foo", new double[] { 1 },
                        new double[] { Double.POSITIVE_INFINITY }));
    }

    @Test
    public void testOpenLeftPointRangeQueryLegacy() throws Exception {
        assertThat(qp.parse("foo:[-Infinity TO 100.0]", DEFAULT_FIELD))
                .isEqualTo(DoublePoint.newRangeQuery("foo", new double[] { Double.NEGATIVE_INFINITY },
                        new double[] { 100 }));
    }

    @Test
    public void testOpenRightPointRangeQueryLegacy() throws Exception {
        assertThat(qp.parse("foo:[1.0 TO Infinity]", DEFAULT_FIELD))
                .isEqualTo(DoublePoint.newRangeQuery("foo", new double[] { 1 },
                        new double[] { Double.POSITIVE_INFINITY }));
    }

}