下面列出了org.apache.lucene.search.DocValuesFieldExistsQuery#org.elasticsearch.index.query.QueryShardContext 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
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;
}
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(), "");
}
@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);
}
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;
}
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;
}
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;
}
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);
}
}
@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);
}
}
@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);
}
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);
}
@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));
}
@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);
}
@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);
}
@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);
}
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;
}
@Override
public Query existsQuery(QueryShardContext context) {
if (hasDocValues()) {
return new DocValuesFieldExistsQuery(name());
} else {
return new TermQuery(new Term(FieldNamesFieldMapper.NAME, name()));
}
}
@Override
public Query existsQuery(QueryShardContext context) {
if (hasDocValues()) {
return new DocValuesFieldExistsQuery(name());
} else {
return new TermQuery(new Term(FieldNamesFieldMapper.NAME, name()));
}
}
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);
}
}
@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);
}
/**
* 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;
}
@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);
}
@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;
}
@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);
}
@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);
}
@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;
}
@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);
}
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();
}
@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());
}
}
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;
}