use of org.apache.lucene.queries.intervals.IntervalQuery in project OpenSearch by opensearch-project.
the class IntervalQueryBuilderTests method testRegexp.
public void testRegexp() throws IOException {
final int DEFAULT_FLAGS = RegexpFlag.ALL.value();
String json = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"regexp\" : { \"pattern\" : \"te.m\" } } } }";
IntervalQueryBuilder builder = (IntervalQueryBuilder) parseQuery(json);
Query expected = new IntervalQuery(TEXT_FIELD_NAME, buildRegexpSource("te.m", DEFAULT_FLAGS, null));
assertEquals(expected, builder.toQuery(createShardContext()));
String no_positions_json = "{ \"intervals\" : { \"" + NO_POSITIONS_FIELD + "\": { " + "\"regexp\" : { \"pattern\" : \"[Tt]erm\" } } } }";
expectThrows(IllegalArgumentException.class, () -> {
IntervalQueryBuilder builder1 = (IntervalQueryBuilder) parseQuery(no_positions_json);
builder1.toQuery(createShardContext());
});
String fixed_field_json = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"regexp\" : { \"pattern\" : \"te.m\", \"use_field\" : \"masked_field\" } } } }";
builder = (IntervalQueryBuilder) parseQuery(fixed_field_json);
expected = new IntervalQuery(TEXT_FIELD_NAME, Intervals.fixField(MASKED_FIELD, buildRegexpSource("te.m", DEFAULT_FLAGS, null)));
assertEquals(expected, builder.toQuery(createShardContext()));
String fixed_field_json_no_positions = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"regexp\" : { \"pattern\" : \"te.m\", \"use_field\" : \"" + NO_POSITIONS_FIELD + "\" } } } }";
expectThrows(IllegalArgumentException.class, () -> {
IntervalQueryBuilder builder1 = (IntervalQueryBuilder) parseQuery(fixed_field_json_no_positions);
builder1.toQuery(createShardContext());
});
String flags_json = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"regexp\" : { \"pattern\" : \"te.m\", \"flags\" : \"NONE\" } } } }";
builder = (IntervalQueryBuilder) parseQuery(flags_json);
expected = new IntervalQuery(TEXT_FIELD_NAME, buildRegexpSource("te.m", RegexpFlag.NONE.value(), null));
assertEquals(expected, builder.toQuery(createShardContext()));
String flags_value_json = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"regexp\" : { \"pattern\" : \"te.m\", \"flags_value\" : \"" + RegexpFlag.ANYSTRING.value() + "\" } } } }";
builder = (IntervalQueryBuilder) parseQuery(flags_value_json);
expected = new IntervalQuery(TEXT_FIELD_NAME, buildRegexpSource("te.m", RegexpFlag.ANYSTRING.value(), null));
assertEquals(expected, builder.toQuery(createShardContext()));
String regexp_max_expand_json = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"regexp\" : { \"pattern\" : \"te.m\", \"max_expansions\" : 500 } } } }";
builder = (IntervalQueryBuilder) parseQuery(regexp_max_expand_json);
expected = new IntervalQuery(TEXT_FIELD_NAME, buildRegexpSource("te.m", DEFAULT_FLAGS, 500));
assertEquals(expected, builder.toQuery(createShardContext()));
String regexp_case_insensitive_json = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"regexp\" : { \"pattern\" : \"TE.M\", \"case_insensitive\" : true } } } }";
builder = (IntervalQueryBuilder) parseQuery(regexp_case_insensitive_json);
expected = new IntervalQuery(TEXT_FIELD_NAME, buildRegexpSource("TE.M", DEFAULT_FLAGS, RegExp.ASCII_CASE_INSENSITIVE, null));
assertEquals(expected, builder.toQuery(createShardContext()));
String regexp_neg_max_expand_json = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"regexp\" : { \"pattern\" : \"te.m\", \"max_expansions\" : -20 } } } }";
builder = (IntervalQueryBuilder) parseQuery(regexp_neg_max_expand_json);
// max expansions use default
expected = new IntervalQuery(TEXT_FIELD_NAME, buildRegexpSource("te.m", DEFAULT_FLAGS, null));
assertEquals(expected, builder.toQuery(createShardContext()));
String regexp_over_max_expand_json = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"regexp\" : { \"pattern\" : \"te.m\", \"max_expansions\" : " + (BooleanQuery.getMaxClauseCount() + 1) + " } } } }";
expectThrows(IllegalArgumentException.class, () -> {
IntervalQueryBuilder builder1 = (IntervalQueryBuilder) parseQuery(regexp_over_max_expand_json);
builder1.toQuery(createShardContext());
});
String regexp_max_expand_with_flags_json = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"regexp\" : { \"pattern\" : \"te.m\", \"flags\": \"NONE\", \"max_expansions\" : 500 } } } }";
builder = (IntervalQueryBuilder) parseQuery(regexp_max_expand_with_flags_json);
expected = new IntervalQuery(TEXT_FIELD_NAME, buildRegexpSource("te.m", RegexpFlag.NONE.value(), 500));
assertEquals(expected, builder.toQuery(createShardContext()));
}
use of org.apache.lucene.queries.intervals.IntervalQuery in project OpenSearch by opensearch-project.
the class IntervalQueryBuilderTests method testCombineInterval.
public void testCombineInterval() throws IOException {
String json = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": {" + " \"all_of\" : {" + " \"mode\" : \"ordered\"," + " \"intervals\" : [" + " { \"match\" : { \"query\" : \"one\" } }," + " { \"all_of\" : { " + " \"mode\" : \"unordered\"," + " \"intervals\" : [" + " { \"match\" : { \"query\" : \"two\" } }," + " { \"match\" : { \"query\" : \"three\" } } ] } } ]," + " \"max_gaps\" : 30," + " \"filter\" : { " + " \"contained_by\" : { " + " \"match\" : { " + " \"query\" : \"SENTENCE\"," + " \"analyzer\" : \"keyword\" } } } }," + " \"boost\" : 1.5 } } }";
IntervalQueryBuilder builder = (IntervalQueryBuilder) parseQuery(json);
Query expected = new BoostQuery(new IntervalQuery(TEXT_FIELD_NAME, Intervals.containedBy(Intervals.maxgaps(30, Intervals.ordered(Intervals.term("one"), Intervals.unordered(Intervals.term("two"), Intervals.term("three")))), Intervals.term("SENTENCE"))), 1.5f);
assertEquals(expected, builder.toQuery(createShardContext()));
json = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": {" + " \"all_of\" : {" + " \"mode\" : \"unordered_no_overlap\"," + " \"intervals\" : [" + " { \"match\" : { \"query\" : \"one\" } }," + " { \"match\" : { \"query\" : \"two\" } } ]," + " \"max_gaps\" : 30 }," + " \"boost\" : 1.5 } } }";
builder = (IntervalQueryBuilder) parseQuery(json);
expected = new BoostQuery(new IntervalQuery(TEXT_FIELD_NAME, Intervals.maxgaps(30, Intervals.unorderedNoOverlaps(Intervals.term("one"), Intervals.term("two")))), 1.5f);
assertEquals(expected, builder.toQuery(createShardContext()));
json = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": {" + " \"all_of\" : {" + " \"mode\" : \"unordered_no_overlap\"," + " \"intervals\" : [" + " { \"match\" : { \"query\" : \"one\" } }," + " { \"match\" : { \"query\" : \"two\" } }," + " { \"match\" : { \"query\" : \"three\" } } ]," + " \"max_gaps\" : 3 }," + " \"boost\" : 3.5 } } }";
builder = (IntervalQueryBuilder) parseQuery(json);
expected = new BoostQuery(new IntervalQuery(TEXT_FIELD_NAME, Intervals.maxgaps(3, Intervals.unorderedNoOverlaps(Intervals.maxgaps(3, Intervals.unorderedNoOverlaps(Intervals.term("one"), Intervals.term("two"))), Intervals.term("three")))), 3.5f);
assertEquals(expected, builder.toQuery(createShardContext()));
}
use of org.apache.lucene.queries.intervals.IntervalQuery in project OpenSearch by opensearch-project.
the class IntervalQueryBuilderTests method testScriptFilter.
public void testScriptFilter() throws IOException {
IntervalFilterScript.Factory factory = () -> new IntervalFilterScript() {
@Override
public boolean execute(Interval interval) {
return interval.getStart() > 3;
}
};
ScriptService scriptService = new ScriptService(Settings.EMPTY, Collections.emptyMap(), Collections.emptyMap()) {
@Override
@SuppressWarnings("unchecked")
public <FactoryType> FactoryType compile(Script script, ScriptContext<FactoryType> context) {
assertEquals(IntervalFilterScript.CONTEXT, context);
assertEquals(new Script("interval.start > 3"), script);
return (FactoryType) factory;
}
};
QueryShardContext baseContext = createShardContext();
QueryShardContext context = new QueryShardContext(baseContext.getShardId(), baseContext.getIndexSettings(), BigArrays.NON_RECYCLING_INSTANCE, null, null, baseContext.getMapperService(), null, scriptService, null, null, null, null, null, null, null, () -> true, null);
String json = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"match\" : { " + " \"query\" : \"term1\"," + " \"filter\" : { " + " \"script\" : { " + " \"source\" : \"interval.start > 3\" } } } } } }";
IntervalQueryBuilder builder = (IntervalQueryBuilder) parseQuery(json);
Query q = builder.toQuery(context);
IntervalQuery expected = new IntervalQuery(TEXT_FIELD_NAME, new IntervalsSourceProvider.ScriptFilterSource(Intervals.term("term1"), "interval.start > 3", null));
assertEquals(expected, q);
}
use of org.apache.lucene.queries.intervals.IntervalQuery in project OpenSearch by opensearch-project.
the class IntervalQueryBuilderTests method testMatchInterval.
public void testMatchInterval() throws IOException {
String json = "{ \"intervals\" : " + "{ \"" + TEXT_FIELD_NAME + "\" : { \"match\" : { \"query\" : \"Hello world\" } } } }";
IntervalQueryBuilder builder = (IntervalQueryBuilder) parseQuery(json);
Query expected = new IntervalQuery(TEXT_FIELD_NAME, Intervals.unordered(Intervals.term("hello"), Intervals.term("world")));
assertEquals(expected, builder.toQuery(createShardContext()));
json = "{ \"intervals\" : " + "{ \"" + TEXT_FIELD_NAME + "\" : { " + " \"match\" : { " + " \"query\" : \"Hello world\"," + " \"max_gaps\" : 40 } } } }";
builder = (IntervalQueryBuilder) parseQuery(json);
expected = new IntervalQuery(TEXT_FIELD_NAME, Intervals.maxgaps(40, Intervals.unordered(Intervals.term("hello"), Intervals.term("world"))));
assertEquals(expected, builder.toQuery(createShardContext()));
json = "{ \"intervals\" : " + "{ \"" + TEXT_FIELD_NAME + "\" : { " + " \"match\" : { " + " \"query\" : \"Hello world\"," + " \"mode\" : \"ordered\" }," + " \"boost\" : 2 } } }";
builder = (IntervalQueryBuilder) parseQuery(json);
expected = new BoostQuery(new IntervalQuery(TEXT_FIELD_NAME, Intervals.ordered(Intervals.term("hello"), Intervals.term("world"))), 2);
assertEquals(expected, builder.toQuery(createShardContext()));
json = "{ \"intervals\" : " + "{ \"" + TEXT_FIELD_NAME + "\" : { " + " \"match\" : { " + " \"query\" : \"Hello world\"," + " \"mode\" : \"unordered_no_overlap\" }," + " \"boost\" : 2 } } }";
builder = (IntervalQueryBuilder) parseQuery(json);
expected = new BoostQuery(new IntervalQuery(TEXT_FIELD_NAME, Intervals.unorderedNoOverlaps(Intervals.term("hello"), Intervals.term("world"))), 2);
assertEquals(expected, builder.toQuery(createShardContext()));
json = "{ \"intervals\" : " + "{ \"" + TEXT_FIELD_NAME + "\" : { " + " \"match\" : { " + " \"query\" : \"Hello world\"," + " \"mode\" : \"unordered_no_overlap\"," + " \"max_gaps\" : 11 }," + " \"boost\" : 2 } } }";
builder = (IntervalQueryBuilder) parseQuery(json);
expected = new BoostQuery(new IntervalQuery(TEXT_FIELD_NAME, Intervals.maxgaps(11, Intervals.unorderedNoOverlaps(Intervals.term("hello"), Intervals.term("world")))), 2);
assertEquals(expected, builder.toQuery(createShardContext()));
json = "{ \"intervals\" : " + "{ \"" + TEXT_FIELD_NAME + "\" : { " + " \"match\" : { " + " \"query\" : \"Hello Open Search\"," + " \"mode\" : \"unordered_no_overlap\" }," + " \"boost\" : 3 } } }";
builder = (IntervalQueryBuilder) parseQuery(json);
expected = new BoostQuery(new IntervalQuery(TEXT_FIELD_NAME, Intervals.unorderedNoOverlaps(Intervals.unorderedNoOverlaps(Intervals.term("hello"), Intervals.term("open")), Intervals.term("search"))), 3);
assertEquals(expected, builder.toQuery(createShardContext()));
json = "{ \"intervals\" : " + "{ \"" + TEXT_FIELD_NAME + "\" : { " + " \"match\" : { " + " \"query\" : \"Hello Open Search\"," + " \"mode\" : \"unordered_no_overlap\"," + " \"max_gaps\": 12 }," + " \"boost\" : 3 } } }";
builder = (IntervalQueryBuilder) parseQuery(json);
expected = new BoostQuery(new IntervalQuery(TEXT_FIELD_NAME, Intervals.maxgaps(12, Intervals.unorderedNoOverlaps(Intervals.maxgaps(12, Intervals.unorderedNoOverlaps(Intervals.term("hello"), Intervals.term("open"))), Intervals.term("search")))), 3);
assertEquals(expected, builder.toQuery(createShardContext()));
json = "{ \"intervals\" : " + "{ \"" + TEXT_FIELD_NAME + "\" : { " + " \"match\" : { " + " \"query\" : \"Hello world\"," + " \"max_gaps\" : 10," + " \"analyzer\" : \"whitespace\"," + " \"mode\" : \"ordered\" } } } }";
builder = (IntervalQueryBuilder) parseQuery(json);
expected = new IntervalQuery(TEXT_FIELD_NAME, Intervals.maxgaps(10, Intervals.ordered(Intervals.term("Hello"), Intervals.term("world"))));
assertEquals(expected, builder.toQuery(createShardContext()));
json = "{ \"intervals\" : " + "{ \"" + TEXT_FIELD_NAME + "\" : { " + " \"match\" : { " + " \"query\" : \"Hello world\"," + " \"max_gaps\" : 10," + " \"analyzer\" : \"whitespace\"," + " \"use_field\" : \"" + MASKED_FIELD + "\"," + " \"mode\" : \"ordered\" } } } }";
builder = (IntervalQueryBuilder) parseQuery(json);
expected = new IntervalQuery(TEXT_FIELD_NAME, Intervals.fixField(MASKED_FIELD, Intervals.maxgaps(10, Intervals.ordered(Intervals.term("Hello"), Intervals.term("world")))));
assertEquals(expected, builder.toQuery(createShardContext()));
json = "{ \"intervals\" : " + "{ \"" + TEXT_FIELD_NAME + "\" : { " + " \"match\" : { " + " \"query\" : \"Hello world\"," + " \"max_gaps\" : 10," + " \"analyzer\" : \"whitespace\"," + " \"mode\" : \"ordered\"," + " \"filter\" : {" + " \"containing\" : {" + " \"match\" : { \"query\" : \"blah\" } } } } } } }";
builder = (IntervalQueryBuilder) parseQuery(json);
expected = new IntervalQuery(TEXT_FIELD_NAME, Intervals.containing(Intervals.maxgaps(10, Intervals.ordered(Intervals.term("Hello"), Intervals.term("world"))), Intervals.term("blah")));
assertEquals(expected, builder.toQuery(createShardContext()));
}
use of org.apache.lucene.queries.intervals.IntervalQuery in project OpenSearch by opensearch-project.
the class IntervalQueryBuilderTests method testCombineDisjunctionInterval.
public void testCombineDisjunctionInterval() throws IOException {
String json = "{ \"intervals\" : " + "{ \"" + TEXT_FIELD_NAME + "\": { " + " \"all_of\" : {" + " \"mode\" : \"ordered\"," + " \"intervals\" : [" + " { \"match\" : { \"query\" : \"atmosphere\" } }," + " { \"any_of\" : {" + " \"intervals\" : [" + " { \"match\" : { \"query\" : \"cold\" } }," + " { \"match\" : { \"query\" : \"outside\" } } ] } } ]," + " \"max_gaps\" : 30," + " \"filter\" : { " + " \"not_contained_by\" : { " + " \"match\" : { \"query\" : \"freeze\" } } } } } } }";
IntervalQueryBuilder builder = (IntervalQueryBuilder) parseQuery(json);
Query expected = new IntervalQuery(TEXT_FIELD_NAME, Intervals.notContainedBy(Intervals.maxgaps(30, Intervals.ordered(Intervals.term("atmosphere"), Intervals.or(Intervals.term("cold"), Intervals.term("outside")))), Intervals.term("freeze")));
assertEquals(expected, builder.toQuery(createShardContext()));
}
Aggregations