Search in sources :

Example 6 with IntervalQuery

use of org.apache.lucene.queries.intervals.IntervalQuery in project OpenSearch by opensearch-project.

the class IntervalQueryBuilderTests method testFuzzy.

public void testFuzzy() throws IOException {
    String json = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"fuzzy\" : { \"term\" : \"Term\" } } } }";
    IntervalQueryBuilder builder = (IntervalQueryBuilder) parseQuery(json);
    Query expected = new IntervalQuery(TEXT_FIELD_NAME, buildFuzzySource("term", "Term", FuzzyQueryBuilder.DEFAULT_PREFIX_LENGTH, true, Fuzziness.AUTO.asDistance("term")));
    assertEquals(expected, builder.toQuery(createShardContext()));
    String json_with_prefix = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"fuzzy\" : { \"term\" : \"Term\", \"prefix_length\" : 2 } } } }";
    builder = (IntervalQueryBuilder) parseQuery(json_with_prefix);
    expected = new IntervalQuery(TEXT_FIELD_NAME, buildFuzzySource("term", "Term", 2, true, Fuzziness.AUTO.asDistance("term")));
    assertEquals(expected, builder.toQuery(createShardContext()));
    String json_with_fuzziness = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"fuzzy\" : { \"term\" : \"Term\", \"prefix_length\" : 2, \"fuzziness\" : \"1\" } } } }";
    builder = (IntervalQueryBuilder) parseQuery(json_with_fuzziness);
    expected = new IntervalQuery(TEXT_FIELD_NAME, buildFuzzySource("term", "Term", 2, true, Fuzziness.ONE.asDistance("term")));
    assertEquals(expected, builder.toQuery(createShardContext()));
    String json_no_transpositions = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"fuzzy\" : { \"term\" : \"Term\", \"prefix_length\" : 2, \"transpositions\" : false } } } }";
    builder = (IntervalQueryBuilder) parseQuery(json_no_transpositions);
    expected = new IntervalQuery(TEXT_FIELD_NAME, buildFuzzySource("term", "Term", 2, false, Fuzziness.AUTO.asDistance("term")));
    assertEquals(expected, builder.toQuery(createShardContext()));
    String json_with_analyzer = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"fuzzy\" : { \"term\" : \"Term\", \"prefix_length\" : 2, \"analyzer\" : \"keyword\" } } } }";
    builder = (IntervalQueryBuilder) parseQuery(json_with_analyzer);
    expected = new IntervalQuery(TEXT_FIELD_NAME, buildFuzzySource("Term", "Term", 2, true, Fuzziness.AUTO.asDistance("term")));
    assertEquals(expected, builder.toQuery(createShardContext()));
    String json_with_fixfield = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"fuzzy\" : { \"term\" : \"Term\", \"prefix_length\" : 2, \"fuzziness\" : \"1\", " + "\"use_field\" : \"" + MASKED_FIELD + "\" } } } }";
    builder = (IntervalQueryBuilder) parseQuery(json_with_fixfield);
    expected = new IntervalQuery(TEXT_FIELD_NAME, Intervals.fixField(MASKED_FIELD, buildFuzzySource("term", "Term", 2, true, Fuzziness.ONE.asDistance("term"))));
    assertEquals(expected, builder.toQuery(createShardContext()));
}
Also used : Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) IntervalQuery(org.apache.lucene.queries.intervals.IntervalQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) IntervalQuery(org.apache.lucene.queries.intervals.IntervalQuery)

Example 7 with IntervalQuery

use of org.apache.lucene.queries.intervals.IntervalQuery in project OpenSearch by opensearch-project.

the class IntervalQueryBuilderTests method testPrefixes.

public void testPrefixes() throws IOException {
    String json = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"prefix\" : { \"prefix\" : \"term\" } } } }";
    IntervalQueryBuilder builder = (IntervalQueryBuilder) parseQuery(json);
    Query expected = new IntervalQuery(TEXT_FIELD_NAME, Intervals.prefix(new BytesRef("term")));
    assertEquals(expected, builder.toQuery(createShardContext()));
    String no_positions_json = "{ \"intervals\" : { \"" + NO_POSITIONS_FIELD + "\": { " + "\"prefix\" : { \"prefix\" : \"term\" } } } }";
    expectThrows(IllegalArgumentException.class, () -> {
        IntervalQueryBuilder builder1 = (IntervalQueryBuilder) parseQuery(no_positions_json);
        builder1.toQuery(createShardContext());
    });
    String no_positions_fixed_field_json = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"prefix\" : { \"prefix\" : \"term\", \"use_field\" : \"" + NO_POSITIONS_FIELD + "\" } } } }";
    expectThrows(IllegalArgumentException.class, () -> {
        IntervalQueryBuilder builder1 = (IntervalQueryBuilder) parseQuery(no_positions_fixed_field_json);
        builder1.toQuery(createShardContext());
    });
    String prefix_json = "{ \"intervals\" : { \"" + PREFIXED_FIELD + "\": { " + "\"prefix\" : { \"prefix\" : \"term\" } } } }";
    builder = (IntervalQueryBuilder) parseQuery(prefix_json);
    expected = new IntervalQuery(PREFIXED_FIELD, Intervals.fixField(PREFIXED_FIELD + "._index_prefix", Intervals.term("term")));
    assertEquals(expected, builder.toQuery(createShardContext()));
    String short_prefix_json = "{ \"intervals\" : { \"" + PREFIXED_FIELD + "\": { " + "\"prefix\" : { \"prefix\" : \"t\" } } } }";
    builder = (IntervalQueryBuilder) parseQuery(short_prefix_json);
    expected = new IntervalQuery(PREFIXED_FIELD, Intervals.or(Intervals.fixField(PREFIXED_FIELD + "._index_prefix", Intervals.wildcard(new BytesRef("t?"))), Intervals.term("t")));
    assertEquals(expected, builder.toQuery(createShardContext()));
    String fix_field_prefix_json = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"prefix\" : { \"prefix\" : \"term\", \"use_field\" : \"" + PREFIXED_FIELD + "\" } } } }";
    builder = (IntervalQueryBuilder) parseQuery(fix_field_prefix_json);
    // This looks weird, but it's fine, because the innermost fixField wins
    expected = new IntervalQuery(TEXT_FIELD_NAME, Intervals.fixField(PREFIXED_FIELD, Intervals.fixField(PREFIXED_FIELD + "._index_prefix", Intervals.term("term"))));
    assertEquals(expected, builder.toQuery(createShardContext()));
    String keyword_json = "{ \"intervals\" : { \"" + PREFIXED_FIELD + "\": { " + "\"prefix\" : { \"prefix\" : \"Term\", \"analyzer\" : \"keyword\" } } } }";
    builder = (IntervalQueryBuilder) parseQuery(keyword_json);
    expected = new IntervalQuery(PREFIXED_FIELD, Intervals.fixField(PREFIXED_FIELD + "._index_prefix", Intervals.term("Term")));
    assertEquals(expected, builder.toQuery(createShardContext()));
    String keyword_fix_field_json = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"prefix\" : { \"prefix\" : \"Term\", \"analyzer\" : \"keyword\", \"use_field\" : \"" + PREFIXED_FIELD + "\" } } } }";
    builder = (IntervalQueryBuilder) parseQuery(keyword_fix_field_json);
    expected = new IntervalQuery(TEXT_FIELD_NAME, Intervals.fixField(PREFIXED_FIELD, Intervals.fixField(PREFIXED_FIELD + "._index_prefix", Intervals.term("Term"))));
    assertEquals(expected, builder.toQuery(createShardContext()));
}
Also used : Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) IntervalQuery(org.apache.lucene.queries.intervals.IntervalQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) IntervalQuery(org.apache.lucene.queries.intervals.IntervalQuery) BytesRef(org.apache.lucene.util.BytesRef)

Example 8 with IntervalQuery

use of org.apache.lucene.queries.intervals.IntervalQuery in project OpenSearch by opensearch-project.

the class IntervalQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    MappedFieldType fieldType = context.fieldMapper(field);
    if (fieldType == null) {
        // Be lenient with unmapped fields so that cross-index search will work nicely
        return new MatchNoDocsQuery();
    }
    Set<String> maskedFields = new HashSet<>();
    sourceProvider.extractFields(maskedFields);
    for (String maskedField : maskedFields) {
        MappedFieldType ft = context.fieldMapper(maskedField);
        if (ft == null) {
            // Be lenient with unmapped fields so that cross-index search will work nicely
            return new MatchNoDocsQuery();
        }
    }
    return new IntervalQuery(field, sourceProvider.getSource(context, fieldType));
}
Also used : MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) IntervalQuery(org.apache.lucene.queries.intervals.IntervalQuery) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) HashSet(java.util.HashSet)

Example 9 with IntervalQuery

use of org.apache.lucene.queries.intervals.IntervalQuery in project OpenSearch by opensearch-project.

the class QueryAnalyzerTests method testIntervalQueries.

public void testIntervalQueries() {
    IntervalsSource source = Intervals.or(Intervals.term("term1"), Intervals.term("term2"));
    Result result = analyze(new IntervalQuery("field", source), Version.CURRENT);
    assertThat(result.verified, is(false));
    assertThat(result.matchAllDocs, is(false));
    assertThat(result.minimumShouldMatch, equalTo(1));
    assertTermsEqual(result.extractions, new Term("field", "term1"), new Term("field", "term2"));
    source = Intervals.ordered(Intervals.term("term1"), Intervals.term("term2"), Intervals.or(Intervals.term("term3"), Intervals.term("term4")));
    result = analyze(new IntervalQuery("field", source), Version.CURRENT);
    assertThat(result.verified, is(false));
    assertThat(result.matchAllDocs, is(false));
    assertThat(result.minimumShouldMatch, equalTo(3));
    assertTermsEqual(result.extractions, new Term("field", "term1"), new Term("field", "term2"), new Term("field", "term3"), new Term("field", "term4"));
    source = Intervals.ordered(Intervals.term("term1"), Intervals.wildcard(new BytesRef("a*")));
    result = analyze(new IntervalQuery("field", source), Version.CURRENT);
    assertThat(result.verified, is(false));
    assertThat(result.matchAllDocs, is(false));
    assertThat(result.minimumShouldMatch, equalTo(1));
    assertTermsEqual(result.extractions, new Term("field", "term1"));
    source = Intervals.ordered(Intervals.wildcard(new BytesRef("a*")));
    result = analyze(new IntervalQuery("field", source), Version.CURRENT);
    assertEquals(Result.UNKNOWN, result);
    source = Intervals.or(Intervals.term("b"), Intervals.wildcard(new BytesRef("a*")));
    result = analyze(new IntervalQuery("field", source), Version.CURRENT);
    assertEquals(Result.UNKNOWN, result);
    source = Intervals.ordered(Intervals.term("term1"), Intervals.prefix(new BytesRef("a")));
    result = analyze(new IntervalQuery("field", source), Version.CURRENT);
    assertThat(result.verified, is(false));
    assertThat(result.matchAllDocs, is(false));
    assertThat(result.minimumShouldMatch, equalTo(1));
    assertTermsEqual(result.extractions, new Term("field", "term1"));
    source = Intervals.ordered(Intervals.prefix(new BytesRef("a")));
    result = analyze(new IntervalQuery("field", source), Version.CURRENT);
    assertEquals(Result.UNKNOWN, result);
    source = Intervals.or(Intervals.term("b"), Intervals.prefix(new BytesRef("a")));
    result = analyze(new IntervalQuery("field", source), Version.CURRENT);
    assertEquals(Result.UNKNOWN, result);
    source = Intervals.containedBy(Intervals.term("a"), Intervals.ordered(Intervals.term("b"), Intervals.term("c")));
    result = analyze(new IntervalQuery("field", source), Version.CURRENT);
    assertThat(result.verified, is(false));
    assertThat(result.matchAllDocs, is(false));
    assertThat(result.minimumShouldMatch, equalTo(3));
    assertTermsEqual(result.extractions, new Term("field", "a"), new Term("field", "b"), new Term("field", "c"));
    source = Intervals.containing(Intervals.term("a"), Intervals.ordered(Intervals.term("b"), Intervals.term("c")));
    result = analyze(new IntervalQuery("field", source), Version.CURRENT);
    assertThat(result.verified, is(false));
    assertThat(result.matchAllDocs, is(false));
    assertThat(result.minimumShouldMatch, equalTo(3));
    assertTermsEqual(result.extractions, new Term("field", "a"), new Term("field", "b"), new Term("field", "c"));
    source = Intervals.overlapping(Intervals.term("a"), Intervals.ordered(Intervals.term("b"), Intervals.term("c")));
    result = analyze(new IntervalQuery("field", source), Version.CURRENT);
    assertThat(result.verified, is(false));
    assertThat(result.matchAllDocs, is(false));
    assertThat(result.minimumShouldMatch, equalTo(3));
    assertTermsEqual(result.extractions, new Term("field", "a"), new Term("field", "b"), new Term("field", "c"));
    source = Intervals.within(Intervals.term("a"), 2, Intervals.ordered(Intervals.term("b"), Intervals.term("c")));
    result = analyze(new IntervalQuery("field", source), Version.CURRENT);
    assertThat(result.verified, is(false));
    assertThat(result.matchAllDocs, is(false));
    assertThat(result.minimumShouldMatch, equalTo(3));
    assertTermsEqual(result.extractions, new Term("field", "a"), new Term("field", "b"), new Term("field", "c"));
    source = Intervals.notContainedBy(Intervals.term("a"), Intervals.ordered(Intervals.term("b"), Intervals.term("c")));
    result = analyze(new IntervalQuery("field", source), Version.CURRENT);
    assertThat(result.verified, is(false));
    assertThat(result.matchAllDocs, is(false));
    assertThat(result.minimumShouldMatch, equalTo(1));
    assertTermsEqual(result.extractions, new Term("field", "a"));
    source = Intervals.notContaining(Intervals.term("a"), Intervals.ordered(Intervals.term("b"), Intervals.term("c")));
    result = analyze(new IntervalQuery("field", source), Version.CURRENT);
    assertThat(result.verified, is(false));
    assertThat(result.matchAllDocs, is(false));
    assertThat(result.minimumShouldMatch, equalTo(1));
    assertTermsEqual(result.extractions, new Term("field", "a"));
    source = Intervals.nonOverlapping(Intervals.term("a"), Intervals.ordered(Intervals.term("b"), Intervals.term("c")));
    result = analyze(new IntervalQuery("field", source), Version.CURRENT);
    assertThat(result.verified, is(false));
    assertThat(result.matchAllDocs, is(false));
    assertThat(result.minimumShouldMatch, equalTo(1));
    assertTermsEqual(result.extractions, new Term("field", "a"));
    source = Intervals.notWithin(Intervals.term("a"), 2, Intervals.ordered(Intervals.term("b"), Intervals.term("c")));
    result = analyze(new IntervalQuery("field", source), Version.CURRENT);
    assertThat(result.verified, is(false));
    assertThat(result.matchAllDocs, is(false));
    assertThat(result.minimumShouldMatch, equalTo(1));
    assertTermsEqual(result.extractions, new Term("field", "a"));
}
Also used : IntervalQuery(org.apache.lucene.queries.intervals.IntervalQuery) IntervalsSource(org.apache.lucene.queries.intervals.IntervalsSource) Term(org.apache.lucene.index.Term) BytesRef(org.apache.lucene.util.BytesRef) Result(org.opensearch.percolator.QueryAnalyzer.Result) QueryAnalyzer.selectBestResult(org.opensearch.percolator.QueryAnalyzer.selectBestResult)

Example 10 with IntervalQuery

use of org.apache.lucene.queries.intervals.IntervalQuery in project OpenSearch by opensearch-project.

the class IntervalQueryBuilderTests method testWildcard.

public void testWildcard() throws IOException {
    String json = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"wildcard\" : { \"pattern\" : \"Te?m\" } } } }";
    IntervalQueryBuilder builder = (IntervalQueryBuilder) parseQuery(json);
    Query expected = new IntervalQuery(TEXT_FIELD_NAME, Intervals.wildcard(new BytesRef("te?m")));
    assertEquals(expected, builder.toQuery(createShardContext()));
    String no_positions_json = "{ \"intervals\" : { \"" + NO_POSITIONS_FIELD + "\": { " + "\"wildcard\" : { \"pattern\" : \"term\" } } } }";
    expectThrows(IllegalArgumentException.class, () -> {
        IntervalQueryBuilder builder1 = (IntervalQueryBuilder) parseQuery(no_positions_json);
        builder1.toQuery(createShardContext());
    });
    String keyword_json = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"wildcard\" : { \"pattern\" : \"Te?m\", \"analyzer\" : \"keyword\" } } } }";
    builder = (IntervalQueryBuilder) parseQuery(keyword_json);
    expected = new IntervalQuery(TEXT_FIELD_NAME, Intervals.wildcard(new BytesRef("Te?m")));
    assertEquals(expected, builder.toQuery(createShardContext()));
    String fixed_field_json = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"wildcard\" : { \"pattern\" : \"Te?m\", \"use_field\" : \"masked_field\" } } } }";
    builder = (IntervalQueryBuilder) parseQuery(fixed_field_json);
    expected = new IntervalQuery(TEXT_FIELD_NAME, Intervals.fixField(MASKED_FIELD, Intervals.wildcard(new BytesRef("te?m"))));
    assertEquals(expected, builder.toQuery(createShardContext()));
    String fixed_field_json_no_positions = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"wildcard\" : { \"pattern\" : \"Te?m\", \"use_field\" : \"" + NO_POSITIONS_FIELD + "\" } } } }";
    expectThrows(IllegalArgumentException.class, () -> {
        IntervalQueryBuilder builder1 = (IntervalQueryBuilder) parseQuery(fixed_field_json_no_positions);
        builder1.toQuery(createShardContext());
    });
    String fixed_field_analyzer_json = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"wildcard\" : { \"pattern\" : \"Te?m\", \"use_field\" : \"masked_field\", \"analyzer\" : \"keyword\" } } } }";
    builder = (IntervalQueryBuilder) parseQuery(fixed_field_analyzer_json);
    expected = new IntervalQuery(TEXT_FIELD_NAME, Intervals.fixField(MASKED_FIELD, Intervals.wildcard(new BytesRef("Te?m"))));
    assertEquals(expected, builder.toQuery(createShardContext()));
    String wildcard_max_expand_json = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"wildcard\" : { \"pattern\" : \"Te?m\", \"max_expansions\" : 500 } } } }";
    builder = (IntervalQueryBuilder) parseQuery(wildcard_max_expand_json);
    expected = new IntervalQuery(TEXT_FIELD_NAME, Intervals.wildcard(new BytesRef("te?m"), 500));
    assertEquals(expected, builder.toQuery(createShardContext()));
    String wildcard_neg_max_expand_json = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"wildcard\" : { \"pattern\" : \"Te?m\", \"max_expansions\" : -20 } } } }";
    builder = (IntervalQueryBuilder) parseQuery(wildcard_neg_max_expand_json);
    // max expansions use default
    expected = new IntervalQuery(TEXT_FIELD_NAME, Intervals.wildcard(new BytesRef("te?m")));
    assertEquals(expected, builder.toQuery(createShardContext()));
    String wildcard_over_max_expand_json = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"wildcard\" : { \"pattern\" : \"Te?m\", \"max_expansions\" : " + (BooleanQuery.getMaxClauseCount() + 1) + " } } } }";
    expectThrows(IllegalArgumentException.class, () -> {
        IntervalQueryBuilder builder1 = (IntervalQueryBuilder) parseQuery(wildcard_over_max_expand_json);
        builder1.toQuery(createShardContext());
    });
}
Also used : Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) IntervalQuery(org.apache.lucene.queries.intervals.IntervalQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) IntervalQuery(org.apache.lucene.queries.intervals.IntervalQuery) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

IntervalQuery (org.apache.lucene.queries.intervals.IntervalQuery)11 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)10 BooleanQuery (org.apache.lucene.search.BooleanQuery)9 BoostQuery (org.apache.lucene.search.BoostQuery)9 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)9 Query (org.apache.lucene.search.Query)9 BytesRef (org.apache.lucene.util.BytesRef)3 HashSet (java.util.HashSet)1 Term (org.apache.lucene.index.Term)1 IntervalsSource (org.apache.lucene.queries.intervals.IntervalsSource)1 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)1 Result (org.opensearch.percolator.QueryAnalyzer.Result)1 QueryAnalyzer.selectBestResult (org.opensearch.percolator.QueryAnalyzer.selectBestResult)1 Script (org.opensearch.script.Script)1 ScriptContext (org.opensearch.script.ScriptContext)1 ScriptService (org.opensearch.script.ScriptService)1