Java Code Examples for org.elasticsearch.index.query.QueryShardContext

The following examples show how to use org.elasticsearch.index.query.QueryShardContext. These examples are extracted from open source projects. 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
@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 Project: crate   Source File: ToMatchQuery.java    License: 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 3
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 4
@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 5
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 6
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 7
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 8
Source Project: pyramid   Source File: PhraseCountQueryBuilder.java    License: 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 9
Source Project: crate   Source File: IpFieldMapper.java    License: 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 10
Source Project: crate   Source File: SeqNoFieldMapper.java    License: 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 11
Source Project: crate   Source File: MatchQueries.java    License: 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 12
@Override
public void testMustRewrite() throws IOException {
    QueryShardContext context = createShardContext();
    context.setAllowUnmappedFields(true);
    ExplorerQueryBuilder queryBuilder = createTestQueryBuilder();
    queryBuilder.boost(AbstractQueryBuilder.DEFAULT_BOOST);
    QueryBuilder rewritten = queryBuilder.rewrite(context);

    // though the query may be rewritten, we assert that we
    // always rewrite to an ExplorerQueryBuilder (same goes for ExplorerQuery...)
    assertThat(rewritten, instanceOf(ExplorerQueryBuilder.class));
    Query q = rewritten.toQuery(context);
    assertThat(q, instanceOf(ExplorerQuery.class));
}
 
Example 13
Source Project: crate   Source File: IpFieldMapper.java    License: 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();
    InetAddress lower;
    if (lowerTerm == null) {
        lower = InetAddressPoint.MIN_VALUE;
    } else {
        lower = parse(lowerTerm);
        if (includeLower == false) {
            if (lower.equals(InetAddressPoint.MAX_VALUE)) {
                return new MatchNoDocsQuery();
            }
            lower = InetAddressPoint.nextUp(lower);
        }
    }

    InetAddress upper;
    if (upperTerm == null) {
        upper = InetAddressPoint.MAX_VALUE;
    } else {
        upper = parse(upperTerm);
        if (includeUpper == false) {
            if (upper.equals(InetAddressPoint.MIN_VALUE)) {
                return new MatchNoDocsQuery();
            }
            upper = InetAddressPoint.nextDown(upper);
        }
    }

    return InetAddressPoint.newRangeQuery(name(), lower, upper);
}
 
Example 14
@Override
protected ValuesSourceAggregatorFactory<ValuesSource.Numeric> innerBuild(
        QueryShardContext context,
        ValuesSourceConfig<ValuesSource.Numeric> config,
        AggregatorFactory parent,
        Builder subFactoriesBuilder) throws IOException {

    final List<RoundingInfo> roundingsInfo = buildRoundings();

    return new DateHierarchyAggregatorFactory(
            name, config, order, roundingsInfo, minDocCount, bucketCountThresholds,
            context, parent, subFactoriesBuilder, metaData);
}
 
Example 15
Source Project: elasticsearch-plugin-geoshape   Source File: GeoShapeBuilder.java    License: MIT License 5 votes vote down vote up
@Override
protected ValuesSourceAggregatorFactory<ValuesSource> innerBuild(
        QueryShardContext queryShardContext,
        ValuesSourceConfig<ValuesSource> config,
        AggregatorFactory parent,
        AggregatorFactories.Builder subFactoriesBuilder) throws IOException {
    return new GeoShapeAggregatorFactory(
            name, config, output_format, must_simplify, simplify_zoom, simplify_algorithm,
            bucketCountThresholds, queryShardContext, parent, subFactoriesBuilder, metaData);
}
 
Example 16
GeoPointClusteringAggregatorFactory(
        String name, ValuesSourceConfig<GeoPoint> config, int precision, double radius, double ratio,
        int requiredSize, int shardSize, QueryShardContext context,
        AggregatorFactory parent, AggregatorFactories.Builder subFactoriesBuilder, Map<String, Object> metaData
) throws IOException {
    super(name, config, context, parent, subFactoriesBuilder, metaData);
    this.precision = precision;
    this.radius = radius;
    this.ratio = ratio;
    this.requiredSize = requiredSize;
    this.shardSize = shardSize;
}
 
Example 17
Source Project: elasticsearch-minhash   Source File: MinHashFieldMapper.java    License: 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 18
@Override
public Query existsQuery(QueryShardContext context) {
    if (hasDocValues()) {
        return new DocValuesFieldExistsQuery(name());
    } else {
        return new TermQuery(new Term(FieldNamesFieldMapper.NAME, name()));
    }
}
 
Example 19
Source Project: crate   Source File: OptimizeQueryForSearchAfter.java    License: 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);
    }
}
 
Example 20
@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 21
Source Project: crate   Source File: QueryParserHelper.java    License: 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 22
Source Project: crate   Source File: StringFieldType.java    License: 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 23
Source Project: crate   Source File: StringFieldType.java    License: 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 24
Source Project: crate   Source File: StringFieldType.java    License: 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 25
Source Project: crate   Source File: IgnoredFieldMapper.java    License: 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 Project: crate   Source File: DateFieldMapper.java    License: 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 27
Source Project: crate   Source File: LuceneOrderedDocCollectorTest.java    License: 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 28
Source Project: crate   Source File: DocumentMapperParser.java    License: 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 29
Source Project: crate   Source File: KeywordFieldMapper.java    License: 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 30
Source Project: crate   Source File: MapperService.java    License: 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;
}