use of org.apache.lucene.search.RegexpQuery in project crate by crate.
the class AnyNotLikeOperator method literalMatchesAnyArrayRef.
@Override
protected Query literalMatchesAnyArrayRef(Function any, Literal<?> probe, Reference candidates, Context context) {
String pattern = (String) probe.value();
String regexString = LikeOperators.patternToRegex(pattern, LikeOperators.DEFAULT_ESCAPE, false);
regexString = regexString.substring(1, regexString.length() - 1);
String notLike = negateWildcard(regexString);
return new RegexpQuery(new Term(candidates.column().fqn(), notLike), RegexpFlag.COMPLEMENT.value());
}
use of org.apache.lucene.search.RegexpQuery in project stargate-core by tuplejump.
the class RegexpCondition method query.
/**
* {@inheritDoc}
*/
@Override
public Query query(Options schema) {
if (field == null || field.trim().isEmpty()) {
throw new IllegalArgumentException("Field name required");
}
if (value == null || value.trim().isEmpty()) {
throw new IllegalArgumentException("Field value required");
}
Query query;
Properties properties = schema.getProperties(field);
Type fieldType = properties != null ? properties.getType() : Type.text;
if (fieldType.isCharSeq()) {
Term term = new Term(field, value);
query = new RegexpQuery(term);
} else {
String message = String.format("Regexp queries are not supported by %s mapper", fieldType);
throw new UnsupportedOperationException(message);
}
return query;
}
use of org.apache.lucene.search.RegexpQuery in project janusgraph by JanusGraph.
the class LuceneIndex method convertQuery.
private SearchParams convertQuery(Condition<?> condition, final KeyInformation.StoreRetriever information, final LuceneCustomAnalyzer delegatingAnalyzer) {
final SearchParams params = new SearchParams();
if (condition instanceof PredicateCondition) {
final PredicateCondition<String, ?> atom = (PredicateCondition) condition;
Object value = atom.getValue();
final String key = atom.getKey();
KeyInformation ki = information.get(key);
final JanusGraphPredicate janusgraphPredicate = atom.getPredicate();
if (value == null && janusgraphPredicate == Cmp.NOT_EQUAL) {
// some fields like Integer omit norms but have docValues
params.addQuery(new DocValuesFieldExistsQuery(key), BooleanClause.Occur.SHOULD);
// some fields like Text have no docValue but have norms
params.addQuery(new NormsFieldExistsQuery(key), BooleanClause.Occur.SHOULD);
} else if (value instanceof Number) {
Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on numeric types: %s", janusgraphPredicate);
params.addQuery(numericQuery(key, (Cmp) janusgraphPredicate, (Number) value));
} else if (value instanceof String) {
if (janusgraphPredicate == Cmp.LESS_THAN) {
params.addQuery(TermRangeQuery.newStringRange(key, null, value.toString(), false, false));
} else if (janusgraphPredicate == Cmp.LESS_THAN_EQUAL) {
params.addQuery(TermRangeQuery.newStringRange(key, null, value.toString(), false, true));
} else if (janusgraphPredicate == Cmp.GREATER_THAN) {
params.addQuery(TermRangeQuery.newStringRange(key, value.toString(), null, false, false));
} else if (janusgraphPredicate == Cmp.GREATER_THAN_EQUAL) {
params.addQuery(TermRangeQuery.newStringRange(key, value.toString(), null, true, false));
} else {
final Mapping map = Mapping.getMapping(ki);
final String stringFieldKey;
if (Mapping.getMapping(ki) == Mapping.TEXTSTRING) {
stringFieldKey = getDualFieldName(key, ki).orElse(key);
} else {
stringFieldKey = key;
}
if ((map == Mapping.DEFAULT || map == Mapping.TEXT) && !Text.HAS_CONTAINS.contains(janusgraphPredicate))
throw new IllegalArgumentException("Text mapped string values only support CONTAINS queries and not: " + janusgraphPredicate);
if (map == Mapping.STRING && Text.HAS_CONTAINS.contains(janusgraphPredicate))
throw new IllegalArgumentException("String mapped string values do not support CONTAINS queries: " + janusgraphPredicate);
if (janusgraphPredicate == Text.CONTAINS) {
tokenize(params, map, delegatingAnalyzer, ((String) value).toLowerCase(), key, janusgraphPredicate);
} else if (janusgraphPredicate == Text.CONTAINS_PREFIX) {
tokenize(params, map, delegatingAnalyzer, (String) value, key, janusgraphPredicate);
} else if (janusgraphPredicate == Text.PREFIX) {
params.addQuery(new PrefixQuery(new Term(stringFieldKey, (String) value)));
} else if (janusgraphPredicate == Text.REGEX) {
final RegexpQuery rq = new RegexpQuery(new Term(stringFieldKey, (String) value));
params.addQuery(rq);
} else if (janusgraphPredicate == Text.CONTAINS_REGEX) {
// This is terrible -- there is probably a better way
// putting this to lowercase because Text search is supposed to be case insensitive
final RegexpQuery rq = new RegexpQuery(new Term(key, ".*" + (((String) value).toLowerCase()) + ".*"));
params.addQuery(rq);
} else if (janusgraphPredicate == Cmp.EQUAL || janusgraphPredicate == Cmp.NOT_EQUAL) {
tokenize(params, map, delegatingAnalyzer, (String) value, stringFieldKey, janusgraphPredicate);
} else if (janusgraphPredicate == Text.FUZZY) {
params.addQuery(new FuzzyQuery(new Term(stringFieldKey, (String) value), Text.getMaxEditDistance((String) value)));
} else if (janusgraphPredicate == Text.CONTAINS_FUZZY) {
value = ((String) value).toLowerCase();
final Builder b = new BooleanQuery.Builder();
for (final String term : Text.tokenize((String) value)) {
b.add(new FuzzyQuery(new Term(key, term), Text.getMaxEditDistance(term)), BooleanClause.Occur.MUST);
}
params.addQuery(b.build());
} else
throw new IllegalArgumentException("Relation is not supported for string value: " + janusgraphPredicate);
}
} else if (value instanceof Geoshape) {
Preconditions.checkArgument(janusgraphPredicate instanceof Geo, "Relation not supported on geo types: %s", janusgraphPredicate);
final Shape shape = ((Geoshape) value).getShape();
final SpatialOperation spatialOp = SPATIAL_PREDICATES.get(janusgraphPredicate);
final SpatialArgs args = new SpatialArgs(spatialOp, shape);
params.addQuery(getSpatialStrategy(key, information.get(key)).makeQuery(args));
} else if (value instanceof Date) {
Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on date types: %s", janusgraphPredicate);
params.addQuery(numericQuery(key, (Cmp) janusgraphPredicate, ((Date) value).getTime()));
} else if (value instanceof Instant) {
Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on instant types: %s", janusgraphPredicate);
params.addQuery(numericQuery(key, (Cmp) janusgraphPredicate, ((Instant) value).toEpochMilli()));
} else if (value instanceof Boolean) {
Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on boolean types: %s", janusgraphPredicate);
final int intValue;
switch((Cmp) janusgraphPredicate) {
case EQUAL:
intValue = ((Boolean) value) ? 1 : 0;
params.addQuery(IntPoint.newRangeQuery(key, intValue, intValue));
break;
case NOT_EQUAL:
intValue = ((Boolean) value) ? 0 : 1;
params.addQuery(IntPoint.newRangeQuery(key, intValue, intValue));
break;
default:
throw new IllegalArgumentException("Boolean types only support EQUAL or NOT_EQUAL");
}
} else if (value instanceof UUID) {
Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on UUID types: %s", janusgraphPredicate);
if (janusgraphPredicate == Cmp.EQUAL) {
params.addQuery(new TermQuery(new Term(key, value.toString())));
} else if (janusgraphPredicate == Cmp.NOT_EQUAL) {
final BooleanQuery.Builder q = new BooleanQuery.Builder();
q.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
q.add(new TermQuery(new Term(key, value.toString())), BooleanClause.Occur.MUST_NOT);
params.addQuery(q.build());
} else {
throw new IllegalArgumentException("Relation is not supported for UUID type: " + janusgraphPredicate);
}
} else {
throw new IllegalArgumentException("Unsupported type: " + value);
}
} else if (condition instanceof Not) {
final SearchParams childParams = convertQuery(((Not) condition).getChild(), information, delegatingAnalyzer);
params.addQuery(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
params.addParams(childParams, BooleanClause.Occur.MUST_NOT);
} else if (condition instanceof And) {
for (final Condition c : condition.getChildren()) {
final SearchParams childParams = convertQuery(c, information, delegatingAnalyzer);
params.addParams(childParams, BooleanClause.Occur.MUST);
}
} else if (condition instanceof Or) {
for (final Condition c : condition.getChildren()) {
final SearchParams childParams = convertQuery(c, information, delegatingAnalyzer);
params.addParams(childParams, BooleanClause.Occur.SHOULD);
}
} else
throw new IllegalArgumentException("Invalid condition: " + condition);
return params;
}
use of org.apache.lucene.search.RegexpQuery in project elasticsearch by elastic.
the class RegexpQueryBuilder method doToQuery.
@Override
protected Query doToQuery(QueryShardContext context) throws QueryShardException, IOException {
MultiTermQuery.RewriteMethod method = QueryParsers.parseRewriteMethod(rewrite, null);
Query query = null;
MappedFieldType fieldType = context.fieldMapper(fieldName);
if (fieldType != null) {
query = fieldType.regexpQuery(value, flagsValue, maxDeterminizedStates, method, context);
}
if (query == null) {
RegexpQuery regexpQuery = new RegexpQuery(new Term(fieldName, BytesRefs.toBytesRef(value)), flagsValue, maxDeterminizedStates);
if (method != null) {
regexpQuery.setRewriteMethod(method);
}
query = regexpQuery;
}
return query;
}
use of org.apache.lucene.search.RegexpQuery in project elasticsearch by elastic.
the class QueryStringQueryBuilderTests method testToQueryRegExpQuery.
public void testToQueryRegExpQuery() throws Exception {
assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
Query query = queryStringQuery("/foo*bar/").defaultField(STRING_FIELD_NAME).maxDeterminizedStates(5000).toQuery(createShardContext());
assertThat(query, instanceOf(RegexpQuery.class));
RegexpQuery regexpQuery = (RegexpQuery) query;
assertTrue(regexpQuery.toString().contains("/foo*bar/"));
}
Aggregations