org.elasticsearch.common.lucene.search.Queries Java Examples
The following examples show how to use
org.elasticsearch.common.lucene.search.Queries.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NumberFieldMapper.java From crate with Apache License 2.0 | 6 votes |
@Override public Query termsQuery(String field, List<Object> values) { int[] v = new int[values.size()]; int upTo = 0; for (int i = 0; i < values.size(); i++) { Object value = values.get(i); if (!hasDecimalPart(value)) { v[upTo++] = parse(value, true); } } if (upTo == 0) { return Queries.newMatchNoDocsQuery("All values have a decimal part"); } if (upTo != v.length) { v = Arrays.copyOf(v, upTo); } return IntPoint.newSetQuery(field, v); }
Example #2
Source File: LuceneQueryBuilder.java From crate with Apache License 2.0 | 6 votes |
@Override public Query visitLiteral(Literal literal, Context context) { Object value = literal.value(); if (value == null) { return Queries.newMatchNoDocsQuery("WHERE null -> no match"); } try { return (boolean) value ? Queries.newMatchAllQuery() : Queries.newMatchNoDocsQuery("WHERE false -> no match"); } catch (ClassCastException e) { // Throw a nice error if the top-level literal doesn't have a boolean type // (This is currently caught earlier, so this code is just a safe-guard) return visitSymbol(literal, context); } }
Example #3
Source File: AnyNeqQuery.java From crate with Apache License 2.0 | 6 votes |
@Override protected Query literalMatchesAnyArrayRef(Literal candidate, Reference array, LuceneQueryBuilder.Context context) throws IOException { // 1 != any ( col ) --> gt 1 or lt 1 String columnName = array.column().fqn(); Object value = candidate.value(); MappedFieldType fieldType = context.getFieldTypeOrNull(columnName); if (fieldType == null) { return Queries.newMatchNoDocsQuery("column does not exist in this index"); } BooleanQuery.Builder query = new BooleanQuery.Builder(); query.setMinimumNumberShouldMatch(1); query.add( fieldType.rangeQuery(value, null, false, false, null, null, context.queryShardContext), BooleanClause.Occur.SHOULD ); query.add( fieldType.rangeQuery(null, value, false, false, null, null, context.queryShardContext), BooleanClause.Occur.SHOULD ); return query.build(); }
Example #4
Source File: AnyEqQuery.java From crate with Apache License 2.0 | 6 votes |
private static Query literalMatchesAnyArrayRef(Function any, Literal candidate, Reference array, LuceneQueryBuilder.Context context) { MappedFieldType fieldType = context.getFieldTypeOrNull(array.column().fqn()); if (fieldType == null) { if (ArrayType.unnest(array.valueType()).id() == ObjectType.ID) { return genericFunctionFilter(any, context); // {x=10} = any(objects) } return Queries.newMatchNoDocsQuery("column doesn't exist in this index"); } if (DataTypes.isArray(candidate.valueType())) { return arrayLiteralEqAnyArray(any, fieldType, candidate.value(), context); } return fieldType.termQuery(candidate.value(), context.queryShardContext()); }
Example #5
Source File: PercolatorService.java From Elasticsearch with Apache License 2.0 | 6 votes |
private void queryBasedPercolating(Engine.Searcher percolatorSearcher, PercolateContext context, QueryCollector percolateCollector) throws IOException { Query percolatorTypeFilter = context.indexService().mapperService().documentMapper(TYPE_NAME).typeFilter(); final Query filter; if (context.aliasFilter() != null) { BooleanQuery.Builder booleanFilter = new BooleanQuery.Builder(); booleanFilter.add(context.aliasFilter(), BooleanClause.Occur.MUST); booleanFilter.add(percolatorTypeFilter, BooleanClause.Occur.MUST); filter = booleanFilter.build(); } else { filter = percolatorTypeFilter; } Query query = Queries.filtered(context.percolateQuery(), filter); percolatorSearcher.searcher().search(query, percolateCollector); percolateCollector.aggregatorCollector.postCollection(); if (context.aggregations() != null) { aggregationPhase.execute(context); } }
Example #6
Source File: QueryCollector.java From Elasticsearch with Apache License 2.0 | 6 votes |
@Override public void collect(int doc) throws IOException { final Query query = getQuery(doc); if (query == null) { // log??? return; } Query existsQuery = query; if (isNestedDoc) { existsQuery = new BooleanQuery.Builder() .add(existsQuery, Occur.MUST) .add(Queries.newNonNestedFilter(), Occur.FILTER) .build(); } // run the query try { if (Lucene.exists(searcher, existsQuery)) { counter++; postMatch(doc); } } catch (IOException e) { logger.warn("[" + current.utf8ToString() + "] failed to execute query", e); } }
Example #7
Source File: QueryCollector.java From Elasticsearch with Apache License 2.0 | 6 votes |
@Override public void collect(int doc) throws IOException { final Query query = getQuery(doc); if (query == null) { // log??? return; } Query existsQuery = query; if (isNestedDoc) { existsQuery = new BooleanQuery.Builder() .add(existsQuery, Occur.MUST) .add(Queries.newNonNestedFilter(), Occur.FILTER) .build(); } // run the query try { if (Lucene.exists(searcher, existsQuery)) { topDocsLeafCollector.collect(doc); postMatch(doc); } } catch (IOException e) { logger.warn("[" + current.utf8ToString() + "] failed to execute query", e); } }
Example #8
Source File: NumberFieldMapper.java From crate with Apache License 2.0 | 6 votes |
@Override public Query termsQuery(String field, List<Object> values) { long[] v = new long[values.size()]; int upTo = 0; for (int i = 0; i < values.size(); i++) { Object value = values.get(i); if (!hasDecimalPart(value)) { v[upTo++] = parse(value, true); } } if (upTo == 0) { return Queries.newMatchNoDocsQuery("All values have a decimal part"); } if (upTo != v.length) { v = Arrays.copyOf(v, upTo); } return LongPoint.newSetQuery(field, v); }
Example #9
Source File: ImageHashLimitQuery.java From elasticsearch-image with Apache License 2.0 | 6 votes |
@Override public Weight createWeight(IndexSearcher searcher) throws IOException { IndexSearcher indexSearcher = new IndexSearcher(searcher.getIndexReader()); indexSearcher.setSimilarity(new SimpleSimilarity()); BooleanQuery booleanQuery = new BooleanQuery(); for (int h : hashes) { booleanQuery.add(new BooleanClause(new TermQuery(new Term(hashFieldName, Integer.toString(h))), BooleanClause.Occur.SHOULD)); } TopDocs topDocs = indexSearcher.search(booleanQuery, maxResult); if (topDocs.scoreDocs.length == 0) { // no result find return Queries.newMatchNoDocsQuery().createWeight(searcher); } BitSet bitSet = new BitSet(topDocs.scoreDocs.length); for (ScoreDoc scoreDoc : topDocs.scoreDocs) { bitSet.set(scoreDoc.doc); } return new ImageHashLimitWeight(searcher, bitSet); }
Example #10
Source File: LuceneQueryBuilder.java From Elasticsearch with Apache License 2.0 | 6 votes |
public Context convert(WhereClause whereClause, MapperService mapperService, IndexFieldDataService indexFieldDataService, IndexCache indexCache) throws UnsupportedFeatureException { Context ctx = new Context(inputSymbolVisitor, mapperService, indexFieldDataService, indexCache); if (whereClause.noMatch()) { ctx.query = Queries.newMatchNoDocsQuery(); } else if (!whereClause.hasQuery()) { ctx.query = Queries.newMatchAllQuery(); } else { ctx.query = VISITOR.process(whereClause.query(), ctx); } if (LOGGER.isTraceEnabled()) { if (whereClause.hasQuery()) { LOGGER.trace("WHERE CLAUSE [{}] -> LUCENE QUERY [{}] ", SymbolPrinter.INSTANCE.printSimple(whereClause.query()), ctx.query); } } return ctx; }
Example #11
Source File: IndexQueryParserService.java From Elasticsearch with Apache License 2.0 | 5 votes |
@Nullable public Query parseInnerQuery(QueryParseContext parseContext) throws IOException { parseContext.parseFieldMatcher(parseFieldMatcher); Query query = parseContext.parseInnerQuery(); if (query == null) { query = Queries.newMatchNoDocsQuery(); } return query; }
Example #12
Source File: QueryBuilderHelper.java From Elasticsearch with Apache License 2.0 | 5 votes |
@Override public Query eq(String columnName, Object value) { if (value == null) { return Queries.newMatchNoDocsQuery(); } return new TermQuery(new Term(columnName, (boolean)value ? "T" : "F")); }
Example #13
Source File: QueryBuilderHelper.java From Elasticsearch with Apache License 2.0 | 5 votes |
@Override public Query eq(String columnName, Object value) { if (value == null) { return Queries.newMatchNoDocsQuery(); } return new TermQuery(new Term(columnName, (BytesRef)value)); }
Example #14
Source File: QueryBuilderHelper.java From Elasticsearch with Apache License 2.0 | 5 votes |
@Override public Query eq(String columnName, Object value) { if (value == null) { return Queries.newMatchNoDocsQuery(); } return new TermQuery(new Term(columnName, valueForSearch(value))); }
Example #15
Source File: AnyNotLikeQuery.java From crate with Apache License 2.0 | 5 votes |
@Override protected Query refMatchesAnyArrayLiteral(Reference candidate, Literal array, LuceneQueryBuilder.Context context) { // col not like ANY (['a', 'b']) --> not(and(like(col, 'a'), like(col, 'b'))) String columnName = candidate.column().fqn(); MappedFieldType fieldType = context.getFieldTypeOrNull(columnName); BooleanQuery.Builder andLikeQueries = new BooleanQuery.Builder(); for (Object value : toIterable(array.value())) { andLikeQueries.add( LikeQuery.like(candidate.valueType(), fieldType, value, ignoreCase), BooleanClause.Occur.MUST); } return Queries.not(andLikeQueries.build()); }
Example #16
Source File: LikeQuery.java From crate with Apache License 2.0 | 5 votes |
public static Query like(DataType dataType, @Nullable MappedFieldType fieldType, Object value, boolean ignoreCase) { if (fieldType == null) { // column doesn't exist on this index -> no match return Queries.newMatchNoDocsQuery("column does not exist in this index"); } if (dataType.equals(DataTypes.STRING)) { return createCaseAwareQuery( fieldType.name(), BytesRefs.toString(value), ignoreCase); } return fieldType.termQuery(value, null); }
Example #17
Source File: NestedInnerQueryParseSupport.java From Elasticsearch with Apache License 2.0 | 5 votes |
private void setPathLevel() { ObjectMapper objectMapper = parseContext.nestedScope().getObjectMapper(); if (objectMapper == null) { parentFilter = parseContext.bitsetFilter(Queries.newNonNestedFilter()); } else { parentFilter = parseContext.bitsetFilter(objectMapper.nestedTypeFilter()); } childFilter = nestedObjectMapper.nestedTypeFilter(); parentObjectMapper = parseContext.nestedScope().nextLevel(nestedObjectMapper); }
Example #18
Source File: LuceneQueryBuilder.java From crate with Apache License 2.0 | 5 votes |
@Override public Query visitReference(Reference symbol, Context context) { // called for queries like: where boolColumn if (symbol.valueType() == DataTypes.BOOLEAN) { MappedFieldType fieldType = context.getFieldTypeOrNull(symbol.column().fqn()); if (fieldType == null) { return Queries.newMatchNoDocsQuery("column does not exist in this index"); } return fieldType.termQuery(true, context.queryShardContext()); } return super.visitReference(symbol, context); }
Example #19
Source File: MatchQueryBuilder.java From Elasticsearch with Apache License 2.0 | 5 votes |
protected Query singleQueryAndApply(MatchQuery.Type type, String fieldName, BytesRef queryString, Float boost) { Query query = singleQuery(type, fieldName, queryString); if (query instanceof BooleanQuery) { Queries.applyMinimumShouldMatch((BooleanQuery) query, options.minimumShouldMatch()); } if (boost != null && query != null) { query.setBoost(boost); } return query; }
Example #20
Source File: LimitQueryParser.java From Elasticsearch with Apache License 2.0 | 5 votes |
@Override public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException { deprecationLogger.deprecated("The [limit] query is deprecated, please use the [terminate_after] parameter of the search API instead."); XContentParser parser = parseContext.parser(); int limit = -1; String currentFieldName = null; XContentParser.Token token; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token.isValue()) { if ("value".equals(currentFieldName)) { limit = parser.intValue(); } else { throw new QueryParsingException(parseContext, "[limit] query does not support [" + currentFieldName + "]"); } } } if (limit == -1) { throw new QueryParsingException(parseContext, "No value specified for limit query"); } // this filter is deprecated and parses to a filter that matches everything return Queries.newMatchAllQuery(); }
Example #21
Source File: IndexFieldMapper.java From Elasticsearch with Apache License 2.0 | 5 votes |
@Override public Query termsQuery(List values, QueryParseContext context) { if (context == null) { return super.termsQuery(values, context); } for (Object value : values) { if (isSameIndex(value, context.index().getName())) { // No need to OR these clauses - we can only logically be // running in the context of just one of these index names. return Queries.newMatchAllQuery(); } } // None of the listed index names are this one return Queries.newMatchNoDocsQuery(); }
Example #22
Source File: NumberFieldMapper.java From crate with Apache License 2.0 | 5 votes |
@Override public Query termQuery(String field, Object value) { if (hasDecimalPart(value)) { return Queries.newMatchNoDocsQuery("Value [" + value + "] has a decimal part"); } int v = parse(value, true); return IntPoint.newExactQuery(field, v); }
Example #23
Source File: IndexQueryParserService.java From Elasticsearch with Apache License 2.0 | 5 votes |
private ParsedQuery innerParse(QueryParseContext parseContext, XContentParser parser) throws IOException, QueryParsingException { parseContext.reset(parser); try { parseContext.parseFieldMatcher(parseFieldMatcher); Query query = parseContext.parseInnerQuery(); if (query == null) { query = Queries.newMatchNoDocsQuery(); } return new ParsedQuery(query, parseContext.copyNamedQueries()); } finally { parseContext.reset(null); } }
Example #24
Source File: MatchQuery.java From crate with Apache License 2.0 | 5 votes |
protected Query zeroTermsQuery() { switch (zeroTermsQuery) { case NULL: return null; case NONE: return Queries.newMatchNoDocsQuery("Matching no documents because no terms present"); case ALL: return Queries.newMatchAllQuery(); default: throw new IllegalStateException("unknown zeroTermsQuery " + zeroTermsQuery); } }
Example #25
Source File: ExistsQueryBuilder.java From crate with Apache License 2.0 | 5 votes |
private static Query newFieldExistsQuery(QueryShardContext context, String field) { MappedFieldType fieldType = context.getMapperService().fullName(field); if (fieldType == null) { // The field does not exist as a leaf but could be an object so // check for an object mapper if (context.getObjectMapper(field) != null) { return newObjectFieldExistsQuery(context, field); } return Queries.newMatchNoDocsQuery("No field \"" + field + "\" exists in mappings."); } Query filter = fieldType.existsQuery(context); return new ConstantScoreQuery(filter); }
Example #26
Source File: EqQuery.java From crate with Apache License 2.0 | 5 votes |
@Override public Query apply(Function input, LuceneQueryBuilder.Context context) { RefAndLiteral refAndLiteral = RefAndLiteral.of(input); if (refAndLiteral == null) { return null; } Reference reference = refAndLiteral.reference(); Literal literal = refAndLiteral.literal(); String columnName = reference.column().fqn(); MappedFieldType fieldType = context.getFieldTypeOrNull(columnName); if (reference.valueType().id() == ObjectType.ID) { //noinspection unchecked return refEqObject(input, reference, (Map<String, Object>) literal.value(), context); } if (fieldType == null) { // field doesn't exist, can't match return Queries.newMatchNoDocsQuery("column does not exist in this index"); } if (DataTypes.isArray(reference.valueType()) && DataTypes.isArray(literal.valueType())) { List values = LuceneQueryBuilder.asList(literal); if (values.isEmpty()) { return genericFunctionFilter(input, context); } Query termsQuery = LuceneQueryBuilder.termsQuery(fieldType, values, context.queryShardContext); // wrap boolTermsFilter and genericFunction filter in an additional BooleanFilter to control the ordering of the filters // termsFilter is applied first // afterwards the more expensive genericFunctionFilter BooleanQuery.Builder filterClauses = new BooleanQuery.Builder(); filterClauses.add(termsQuery, BooleanClause.Occur.MUST); filterClauses.add(genericFunctionFilter(input, context), BooleanClause.Occur.MUST); return filterClauses.build(); } return fieldType.termQuery(literal.value(), context.queryShardContext); }
Example #27
Source File: NumberFieldMapper.java From crate with Apache License 2.0 | 5 votes |
@Override public Query termQuery(String field, Object value) { if (hasDecimalPart(value)) { return Queries.newMatchNoDocsQuery("Value [" + value + "] has a decimal part"); } long v = parse(value, true); return LongPoint.newExactQuery(field, v); }
Example #28
Source File: MultiMatchQuery.java From Elasticsearch with Apache License 2.0 | 5 votes |
private Query parseAndApply(Type type, String fieldName, Object value, String minimumShouldMatch, Float boostValue) throws IOException { Query query = parse(type, fieldName, value); // If the coordination factor is disabled on a boolean query we don't apply the minimum should match. // This is done to make sure that the minimum_should_match doesn't get applied when there is only one word // and multiple variations of the same word in the query (synonyms for instance). if (query instanceof BooleanQuery && !((BooleanQuery) query).isCoordDisabled()) { query = Queries.applyMinimumShouldMatch((BooleanQuery) query, minimumShouldMatch); } if (boostValue != null && query != null) { query.setBoost(boostValue); } return query; }
Example #29
Source File: TranslogRecoveryPerformer.java From Elasticsearch with Apache License 2.0 | 5 votes |
private static Engine.DeleteByQuery prepareDeleteByQuery(IndexQueryParserService queryParserService, MapperService mapperService, IndexAliasesService indexAliasesService, IndexCache indexCache, BytesReference source, @Nullable String[] filteringAliases, Engine.Operation.Origin origin, String... types) { long startTime = System.nanoTime(); if (types == null) { types = Strings.EMPTY_ARRAY; } Query query; try { query = queryParserService.parseQuery(source).query(); } catch (QueryParsingException ex) { // for BWC we try to parse directly the query since pre 1.0.0.Beta2 we didn't require a top level query field if (queryParserService.getIndexCreatedVersion().onOrBefore(Version.V_1_0_0_Beta2)) { try { XContentParser parser = XContentHelper.createParser(source); ParsedQuery parse = queryParserService.parse(parser); query = parse.query(); } catch (Throwable t) { ex.addSuppressed(t); throw ex; } } else { throw ex; } } Query searchFilter = mapperService.searchFilter(types); if (searchFilter != null) { query = Queries.filtered(query, searchFilter); } Query aliasFilter = indexAliasesService.aliasFilter(filteringAliases); BitSetProducer parentFilter = mapperService.hasNested() ? indexCache.bitsetFilterCache().getBitSetProducer(Queries.newNonNestedFilter()) : null; return new Engine.DeleteByQuery(query, source, filteringAliases, aliasFilter, parentFilter, origin, startTime, types); }
Example #30
Source File: IndexFieldMapper.java From Elasticsearch with Apache License 2.0 | 5 votes |
/** * This termQuery impl looks at the context to determine the index that * is being queried and then returns a MATCH_ALL_QUERY or MATCH_NO_QUERY * if the value matches this index. This can be useful if aliases or * wildcards are used but the aim is to restrict the query to specific * indices */ @Override public Query termQuery(Object value, @Nullable QueryParseContext context) { if (context == null) { return super.termQuery(value, context); } if (isSameIndex(value, context.index().getName())) { return Queries.newMatchAllQuery(); } else { return Queries.newMatchNoDocsQuery(); } }