org.elasticsearch.index.query.QueryShardContext Java Examples

The following examples show how to use org.elasticsearch.index.query.QueryShardContext. 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: LtrQueryBuilder.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    List<PrebuiltFeature> features = new ArrayList<>(_features.size());
    for (QueryBuilder builder : _features) {
        features.add(new PrebuiltFeature(builder.queryName(), builder.toQuery(context)));
    }
    features = Collections.unmodifiableList(features);

    RankLibScriptEngine.RankLibModelContainer.Factory factory = context.compile(
            _rankLibScript,
            RankLibScriptEngine.CONTEXT);
    RankLibScriptEngine.RankLibModelContainer executableScript = factory.newInstance();
    LtrRanker ranker = (LtrRanker) executableScript.run();

    PrebuiltFeatureSet featureSet = new PrebuiltFeatureSet(queryName(), features);
    PrebuiltLtrModel model = new PrebuiltLtrModel(ranker.name(), ranker, featureSet);
    return RankerQuery.build(model);
}
 
Example #2
Source File: IpFieldMapper.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public Query termQuery(Object value, @Nullable QueryShardContext context) {
    failIfNotIndexed();
    if (value instanceof InetAddress) {
        return InetAddressPoint.newExactQuery(name(), (InetAddress) value);
    } else {
        if (value instanceof BytesRef) {
            value = ((BytesRef) value).utf8ToString();
        }
        String term = value.toString();
        if (term.contains("/")) {
            final Tuple<InetAddress, Integer> cidr = InetAddresses.parseCidr(term);
            return InetAddressPoint.newPrefixQuery(name(), cidr.v1(), cidr.v2());
        }
        InetAddress address = InetAddresses.forString(term);
        return InetAddressPoint.newExactQuery(name(), address);
    }
}
 
Example #3
Source File: SeqNoFieldMapper.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower,
                        boolean includeUpper, QueryShardContext context) {
    long l = Long.MIN_VALUE;
    long u = Long.MAX_VALUE;
    if (lowerTerm != null) {
        l = parse(lowerTerm);
        if (includeLower == false) {
            if (l == Long.MAX_VALUE) {
                return new MatchNoDocsQuery();
            }
            ++l;
        }
    }
    if (upperTerm != null) {
        u = parse(upperTerm);
        if (includeUpper == false) {
            if (u == Long.MIN_VALUE) {
                return new MatchNoDocsQuery();
            }
            --u;
        }
    }
    return LongPoint.newRangeQuery(name(), l, u);
}
 
Example #4
Source File: ToMatchQuery.java    From crate with Apache License 2.0 6 votes vote down vote up
private static Query singleMatchQuery(QueryShardContext queryShardContext,
                                      Map.Entry<String, Object> entry,
                                      String queryString,
                                      String matchType,
                                      Map<String, Object> options) throws IOException {
    Query query = MatchQueries.singleMatch(
        queryShardContext,
        entry.getKey(),
        queryString,
        matchType,
        options
    );
    Object boost = entry.getValue();
    if (boost instanceof Number) {
        return new BoostQuery(query, ((Number) boost).floatValue());
    }
    return query;
}
 
Example #5
Source File: PhraseCountQueryBuilder.java    From pyramid with Apache License 2.0 6 votes vote down vote up
protected Query doToQuery(QueryShardContext context) throws IOException {
//        Analyzer analyzer = context.getMapperService().searchAnalyzer();
        Analyzer analyzer = new WhitespaceAnalyzer();
        try (TokenStream source = analyzer.tokenStream(fieldName, value.toString())) {
            CachingTokenFilter stream = new CachingTokenFilter(new LowerCaseFilter(source));
            TermToBytesRefAttribute termAtt = stream.getAttribute(TermToBytesRefAttribute.class);
            if (termAtt == null) {
                return null;
            }
            List<CustomSpanTermQuery> clauses = new ArrayList<>();
            stream.reset();
            while (stream.incrementToken()) {
                Term term = new Term(fieldName, termAtt.getBytesRef());
                    clauses.add(new CustomSpanTermQuery(term));
            }
            return new PhraseCountQuery(clauses.toArray(new CustomSpanTermQuery[clauses.size()]), slop, inOrder, weightedCount);
        } catch (IOException e) {
            throw new RuntimeException("Error analyzing query text", e);
        }


    }
 
Example #6
Source File: LtrQueryBuilderTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
public void testUnnamedFeatures() throws IOException {
    String scriptSpec = "{\"source\": \"" + simpleModel + "\"}";

    String ltrQuery =       "{  " +
            "   \"ltr\": {" +
            "      \"model\": " + scriptSpec + ",        " +
            "      \"features\": [        " +
            "         {\"match\": {         " +
            "            \"foo\": {     " +
            "              \"query\": \"bar\" " +
            "         }}},                   " +
            "         {\"match\": {         " +
            "            \"baz\": {" +
            "            \"query\": \"sham\"," +
            "            \"_name\": \"\" " +
            "         }}}                   " +
            "      ]                      " +
            "   } " +
            "}";
    LtrQueryBuilder queryBuilder = (LtrQueryBuilder)parseQuery(ltrQuery);
    QueryShardContext context = createShardContext();
    RankerQuery query = (RankerQuery)queryBuilder.toQuery(context);
    assertNull(query.getFeature(0).name());
    assertEquals(query.getFeature(1).name(), "");

}
 
Example #7
Source File: MatchQueries.java    From crate with Apache License 2.0 6 votes vote down vote up
public static Query singleMatch(QueryShardContext queryShardContext,
                                String fieldName,
                                String queryString,
                                @Nullable String matchType,
                                @Nullable Map<String, Object> options) throws IOException {
    MultiMatchQueryType type = getType(matchType);
    ParsedOptions parsedOptions = OptionParser.parse(type, options);

    MatchQuery matchQuery = new MatchQuery(queryShardContext);

    if (parsedOptions.analyzer() != null) {
        matchQuery.setAnalyzer(parsedOptions.analyzer());
    }
    matchQuery.setCommonTermsCutoff(parsedOptions.commonTermsCutoff());
    matchQuery.setFuzziness(parsedOptions.fuzziness());
    matchQuery.setFuzzyPrefixLength(parsedOptions.prefixLength());
    matchQuery.setFuzzyRewriteMethod(parsedOptions.rewriteMethod());
    matchQuery.setMaxExpansions(parsedOptions.maxExpansions());
    matchQuery.setPhraseSlop(parsedOptions.phraseSlop());
    matchQuery.setTranspositions(parsedOptions.transpositions());
    matchQuery.setZeroTermsQuery(parsedOptions.zeroTermsQuery());
    matchQuery.setOccur(parsedOptions.operator());

    MatchQuery.Type matchQueryType = type.matchQueryType();
    return matchQuery.parse(matchQueryType, fieldName, queryString);
}
 
Example #8
Source File: PathHierarchyAggregationBuilder.java    From elasticsearch-aggregation-pathhierarchy with MIT License 6 votes vote down vote up
@Override
protected ValuesSourceAggregatorFactory<ValuesSource> innerBuild(
        QueryShardContext context,
        ValuesSourceConfig<ValuesSource> config,
        AggregatorFactory parent,
        AggregatorFactories.Builder subFactoriesBuilder) throws IOException {

    if (minDepth > maxDepth)
        throw new IllegalArgumentException("[minDepth] (" + minDepth + ") must not be greater than [maxDepth] (" +
                maxDepth + ")");

    if (depth >= 0) {
        if (minDepth > depth)
            throw new IllegalArgumentException("[minDepth] (" + minDepth + ") must not be greater than [depth] (" +
                    depth + ")");
        minDepth = depth;
        maxDepth = depth;
    }

    return new PathHierarchyAggregatorFactory(
            name, config, separator, minDepth, maxDepth, keepBlankPath, order, minDocCount, bucketCountThresholds,
            context, parent, subFactoriesBuilder, metaData);
}
 
Example #9
Source File: GeoShapeAggregatorFactory.java    From elasticsearch-plugin-geoshape with MIT License 6 votes vote down vote up
GeoShapeAggregatorFactory(String name,
                               ValuesSourceConfig<ValuesSource> config,
                               InternalGeoShape.OutputFormat output_format,
                               boolean must_simplify,
                               int zoom,
                               GeoShape.Algorithm algorithm,
                               GeoShapeAggregator.BucketCountThresholds bucketCountThresholds,
                               QueryShardContext context,
                               AggregatorFactory parent,
                               AggregatorFactories.Builder subFactoriesBuilder,
                               Map<String, Object> metaData
) throws IOException {
    super(name, config, context, parent, subFactoriesBuilder, metaData);
    this.output_format = output_format;
    this.must_simplify = must_simplify;
    this.zoom = zoom;
    this.algorithm = algorithm;
    this.bucketCountThresholds = bucketCountThresholds;
}
 
Example #10
Source File: PathHierarchyAggregatorFactory.java    From elasticsearch-aggregation-pathhierarchy with MIT License 6 votes vote down vote up
PathHierarchyAggregatorFactory(String name,
                               ValuesSourceConfig<ValuesSource> config,
                               String separator,
                               int minDepth,
                               int maxDepth,
                               boolean keepBlankPath,
                               BucketOrder order,
                               long minDocCount,
                               PathHierarchyAggregator.BucketCountThresholds bucketCountThresholds,
                               QueryShardContext context,
                               AggregatorFactory parent,
                               AggregatorFactories.Builder subFactoriesBuilder,
                               Map<String, Object> metaData
) throws IOException {
    super(name, config, context, parent, subFactoriesBuilder, metaData);
    this.separator = new BytesRef(separator);
    this.minDepth = minDepth;
    this.maxDepth = maxDepth;
    this.keepBlankPath = keepBlankPath;
    this.order = order;
    this.minDocCount = minDocCount;
    this.bucketCountThresholds = bucketCountThresholds;
}
 
Example #11
Source File: DateHierarchyAggregatorFactory.java    From elasticsearch-aggregation-pathhierarchy with MIT License 6 votes vote down vote up
DateHierarchyAggregatorFactory(String name,
                               ValuesSourceConfig<ValuesSource.Numeric> config,
                               BucketOrder order,
                               List<DateHierarchyAggregationBuilder.RoundingInfo> roundingsInfo,
                               long minDocCount,
                               DateHierarchyAggregator.BucketCountThresholds bucketCountThresholds,
                               QueryShardContext context,
                               AggregatorFactory parent,
                               AggregatorFactories.Builder subFactoriesBuilder,
                               Map<String, Object> metaData
) throws IOException {
    super(name, config, context, parent, subFactoriesBuilder, metaData);
    this.order = order;
    this.roundingsInfo = roundingsInfo;
    this.minDocCount = minDocCount;
    this.bucketCountThresholds = bucketCountThresholds;
}
 
Example #12
Source File: DocumentMapperParser.java    From crate with Apache License 2.0 5 votes vote down vote up
public DocumentMapperParser(IndexSettings indexSettings, MapperService mapperService, IndexAnalyzers indexAnalyzers,
                            NamedXContentRegistry xContentRegistry, MapperRegistry mapperRegistry,
                            Supplier<QueryShardContext> queryShardContextSupplier) {
    this.mapperService = mapperService;
    this.indexAnalyzers = indexAnalyzers;
    this.xContentRegistry = xContentRegistry;
    this.queryShardContextSupplier = queryShardContextSupplier;
    this.typeParsers = mapperRegistry.getMapperParsers();
    this.rootTypeParsers = mapperRegistry.getMetadataMapperParsers();
    indexVersionCreated = indexSettings.getIndexVersionCreated();
}
 
Example #13
Source File: SimpleMappedFieldType.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public final Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper,
                              ShapeRelation relation, DateTimeZone timeZone, QueryShardContext context) {
    if (relation == ShapeRelation.DISJOINT) {
        throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() +
                "] does not support DISJOINT ranges");
    }
    // We do not fail on non-null time zones and date parsers
    // The reasoning is that on query parsers, you might want to set a time zone or format for date fields
    // but then the API has no way to know which fields are dates and which fields are not dates
    return rangeQuery(lowerTerm, upperTerm, includeLower, includeUpper, context);
}
 
Example #14
Source File: GeoPointFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query existsQuery(QueryShardContext context) {
    if (hasDocValues()) {
        return new DocValuesFieldExistsQuery(name());
    } else {
        return new TermQuery(new Term(FieldNamesFieldMapper.NAME, name()));
    }
}
 
Example #15
Source File: BooleanFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, QueryShardContext context) {
    failIfNotIndexed();
    return new TermRangeQuery(name(),
        lowerTerm == null ? null : indexedValueForSearch(lowerTerm),
        upperTerm == null ? null : indexedValueForSearch(upperTerm),
        includeLower, includeUpper);
}
 
Example #16
Source File: BooleanFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query existsQuery(QueryShardContext context) {
    if (hasDocValues()) {
        return new DocValuesFieldExistsQuery(name());
    } else {
        return new TermQuery(new Term(FieldNamesFieldMapper.NAME, name()));
    }
}
 
Example #17
Source File: KeywordFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query existsQuery(QueryShardContext context) {
    if (hasDocValues()) {
        return new DocValuesFieldExistsQuery(name());
    } else if (omitNorms()) {
        return new TermQuery(new Term(FieldNamesFieldMapper.NAME, name()));
    } else {
        return new NormsFieldExistsQuery(name());
    }
}
 
Example #18
Source File: QueryParserHelper.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Resolve all the field names and patterns present in the provided map with the
 * {@link QueryShardContext} and returns a new map containing all the expanded fields with their original boost.
 * @param context The context of the query.
 * @param fieldsAndWeights The map of fields and weights to expand.
 * @param fieldSuffix The suffix name to add to the expanded field names if a mapping exists for that name.
 *                    The original name of the field is kept if adding the suffix to the field name does not point to a valid field
 *                    in the mapping.
 */
public static Map<String, Float> resolveMappingFields(QueryShardContext context,
                                                      Map<String, Float> fieldsAndWeights,
                                                      String fieldSuffix) {
    Map<String, Float> resolvedFields = new HashMap<>();
    for (Map.Entry<String, Float> fieldEntry : fieldsAndWeights.entrySet()) {
        boolean allField = Regex.isMatchAllPattern(fieldEntry.getKey());
        boolean multiField = Regex.isSimpleMatchPattern(fieldEntry.getKey());
        float weight = fieldEntry.getValue() == null ? 1.0f : fieldEntry.getValue();
        Map<String, Float> fieldMap = resolveMappingField(context, fieldEntry.getKey(), weight,
            !multiField, !allField, fieldSuffix);
        resolvedFields.putAll(fieldMap);
    }
    return resolvedFields;
}
 
Example #19
Source File: MapperService.java    From crate with Apache License 2.0 5 votes vote down vote up
public MapperService(IndexSettings indexSettings, IndexAnalyzers indexAnalyzers, NamedXContentRegistry xContentRegistry,
                     MapperRegistry mapperRegistry,
                     Supplier<QueryShardContext> queryShardContextSupplier) {
    super(indexSettings);
    this.indexAnalyzers = indexAnalyzers;
    this.fieldTypes = new FieldTypeLookup();
    this.documentParser = new DocumentMapperParser(indexSettings, this, indexAnalyzers, xContentRegistry,
            mapperRegistry, queryShardContextSupplier);
    this.indexAnalyzer = new MapperAnalyzerWrapper(indexAnalyzers.getDefaultIndexAnalyzer(), MappedFieldType::indexAnalyzer);
    this.searchAnalyzer = new MapperAnalyzerWrapper(indexAnalyzers.getDefaultSearchAnalyzer(), MappedFieldType::searchAnalyzer);
    this.searchQuoteAnalyzer = new MapperAnalyzerWrapper(indexAnalyzers.getDefaultSearchQuoteAnalyzer(), MappedFieldType::searchQuoteAnalyzer);
    this.mapperRegistry = mapperRegistry;
}
 
Example #20
Source File: DateFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query rangeQuery(Object lowerTerm,
                        Object upperTerm,
                        boolean includeLower,
                        boolean includeUpper,
                        ShapeRelation relation,
                        @Nullable DateTimeZone timeZone,
                        QueryShardContext context) {
    failIfNotIndexed();
    if (relation == ShapeRelation.DISJOINT) {
        throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() +
                "] does not support DISJOINT ranges");
    }
    long l, u;
    if (lowerTerm == null) {
        l = Long.MIN_VALUE;
    } else {
        l = (Long) lowerTerm;
        if (includeLower == false) {
            ++l;
        }
    }
    if (upperTerm == null) {
        u = Long.MAX_VALUE;
    } else {
        u = (Long) upperTerm;
        if (includeUpper == false) {
            --u;
        }
    }
    Query query = LongPoint.newRangeQuery(name(), l, u);
    if (hasDocValues()) {
        Query dvQuery = SortedNumericDocValuesField.newSlowRangeQuery(name(), l, u);
        query = new IndexOrDocValuesQuery(query, dvQuery);
    }
    return query;
}
 
Example #21
Source File: LuceneOrderedDocCollectorTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testNextPageQueryWithLastCollectedNullValue() {
    FieldDoc fieldDoc = new FieldDoc(1, 0, new Object[]{null});
    OrderBy orderBy = new OrderBy(Collections.singletonList(REFERENCE), new boolean[]{false}, new boolean[]{false});

    OptimizeQueryForSearchAfter queryForSearchAfter = new OptimizeQueryForSearchAfter(
        orderBy, mock(QueryShardContext.class), name -> valueFieldType);

    queryForSearchAfter.apply(fieldDoc);
}
 
Example #22
Source File: IpFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query existsQuery(QueryShardContext context) {
    if (hasDocValues()) {
        return new DocValuesFieldExistsQuery(name());
    } else {
        return new TermQuery(new Term(FieldNamesFieldMapper.NAME, name()));
    }
}
 
Example #23
Source File: TextFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query prefixQuery(String value, MultiTermQuery.RewriteMethod method, QueryShardContext context) {
    if (prefixFieldType == null || prefixFieldType.accept(value.length()) == false) {
        return super.prefixQuery(value, method, context);
    }
    Query tq = prefixFieldType.termQuery(value, context);
    if (method == null || method == MultiTermQuery.CONSTANT_SCORE_REWRITE
        || method == MultiTermQuery.CONSTANT_SCORE_BOOLEAN_REWRITE) {
        return new ConstantScoreQuery(tq);
    }
    return tq;
}
 
Example #24
Source File: SeqNoFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query termsQuery(List<?> values, @Nullable QueryShardContext context) {
    long[] v = new long[values.size()];
    for (int i = 0; i < values.size(); ++i) {
        v[i] = parse(values.get(i));
    }
    return LongPoint.newSetQuery(name(), v);
}
 
Example #25
Source File: IgnoredFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query existsQuery(QueryShardContext context) {
    // This query is not performance sensitive, it only helps assess
    // quality of the data, so we may use a slow query. It shouldn't
    // be too slow in practice since the number of unique terms in this
    // field is bounded by the number of fields in the mappings.
    return new TermRangeQuery(name(), null, null, true, true);
}
 
Example #26
Source File: StringFieldType.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, QueryShardContext context) {
    failIfNotIndexed();
    return new TermRangeQuery(name(),
        lowerTerm == null ? null : indexedValueForSearch(lowerTerm),
        upperTerm == null ? null : indexedValueForSearch(upperTerm),
        includeLower, includeUpper);
}
 
Example #27
Source File: StringFieldType.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query prefixQuery(String value, MultiTermQuery.RewriteMethod method, QueryShardContext context) {
    failIfNotIndexed();
    PrefixQuery query = new PrefixQuery(new Term(name(), indexedValueForSearch(value)));
    if (method != null) {
        query.setRewriteMethod(method);
    }
    return query;
}
 
Example #28
Source File: StringFieldType.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query termsQuery(List<?> values, QueryShardContext context) {
    failIfNotIndexed();
    BytesRef[] bytesRefs = new BytesRef[values.size()];
    for (int i = 0; i < bytesRefs.length; i++) {
        bytesRefs[i] = indexedValueForSearch(values.get(i));
    }
    return new TermInSetQuery(name(), bytesRefs);
}
 
Example #29
Source File: ReferenceMapper.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Query termsQuery(List<?> values, QueryShardContext context) {
    failIfNotIndexed();
    BytesRef[] bytesRefs = new BytesRef[values.size()];
    for (int i = 0; i < bytesRefs.length; i++) {
        bytesRefs[i] = indexedValueForSearch(values.get(i));
    }
    return new TermInSetQuery(name(), bytesRefs);
}
 
Example #30
Source File: OptimizeQueryForSearchAfter.java    From crate with Apache License 2.0 5 votes vote down vote up
public OptimizeQueryForSearchAfter(OrderBy orderBy,
                                   QueryShardContext queryShardContext,
                                   FieldTypeLookup fieldTypeLookup) {
    this.orderBy = orderBy;
    missingValues = new Object[orderBy.orderBySymbols().size()];
    this.queryShardContext = queryShardContext;
    this.fieldTypeLookup = fieldTypeLookup;
    for (int i = 0; i < orderBy.orderBySymbols().size(); i++) {
        missingValues[i] = NullSentinelValues.nullSentinelForScoreDoc(orderBy, i);
    }
}