use of org.elasticsearch.index.mapper.MappedFieldType in project elasticsearch by elastic.
the class SpanTermQueryBuilder method doToQuery.
@Override
protected SpanQuery doToQuery(QueryShardContext context) throws IOException {
MappedFieldType mapper = context.fieldMapper(fieldName);
Term term;
if (mapper == null) {
term = new Term(fieldName, BytesRefs.toBytesRef(value));
} else {
Query termQuery = mapper.termQuery(value, context);
term = MappedFieldType.extractTerm(termQuery);
}
return new SpanTermQuery(term);
}
use of org.elasticsearch.index.mapper.MappedFieldType in project elasticsearch by elastic.
the class IndicesService method getFieldStats.
/**
* Fetch {@linkplain FieldStats} for a field. These stats are cached until the shard changes.
* @param shard the shard to use with the cache key
* @param searcher searcher to use to lookup the field stats
* @param field the actual field
* @param useCache should this request use the cache?
*/
public FieldStats<?> getFieldStats(IndexShard shard, Engine.Searcher searcher, String field, boolean useCache) throws Exception {
MappedFieldType fieldType = shard.mapperService().fullName(field);
if (fieldType == null) {
return null;
}
if (useCache == false) {
return fieldType.stats(searcher.reader());
}
BytesReference cacheKey = new BytesArray("fieldstats:" + field);
BytesReference statsRef = cacheShardLevelResult(shard, searcher.getDirectoryReader(), cacheKey, out -> {
try {
out.writeOptionalWriteable(fieldType.stats(searcher.reader()));
} catch (IOException e) {
throw new IllegalStateException("Failed to write field stats output", e);
}
});
try (StreamInput in = statsRef.streamInput()) {
return in.readOptionalWriteable(FieldStats::readFrom);
}
}
use of org.elasticsearch.index.mapper.MappedFieldType in project crate by crate.
the class LuceneOrderedDocCollector method nextPageQuery.
@Nullable
@VisibleForTesting
static Query nextPageQuery(FieldDoc lastCollected, OrderBy orderBy, Object[] missingValues, FieldTypeLookup fieldTypeLookup) {
BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder();
for (int i = 0; i < orderBy.orderBySymbols().size(); i++) {
Symbol order = orderBy.orderBySymbols().get(i);
Object value = lastCollected.fields[i];
if (order instanceof Reference) {
boolean nullsFirst = orderBy.nullsFirst()[i] == null ? false : orderBy.nullsFirst()[i];
value = value == null || value.equals(missingValues[i]) ? null : value;
if (nullsFirst && value == null) {
// no filter needed
continue;
}
String columnName = ((Reference) order).ident().columnIdent().fqn();
MappedFieldType fieldType = requireNonNull(fieldTypeLookup.get(columnName), "Column must exist: " + columnName);
Query orderQuery;
// nulls already gone, so they should be excluded
if (nullsFirst) {
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
booleanQuery.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
if (orderBy.reverseFlags()[i]) {
booleanQuery.add(fieldType.rangeQuery(null, value, false, true), BooleanClause.Occur.MUST_NOT);
} else {
booleanQuery.add(fieldType.rangeQuery(value, null, true, false), BooleanClause.Occur.MUST_NOT);
}
orderQuery = booleanQuery.build();
} else {
if (orderBy.reverseFlags()[i]) {
orderQuery = fieldType.rangeQuery(value, null, false, false);
} else {
orderQuery = fieldType.rangeQuery(null, value, false, false);
}
}
queryBuilder.add(orderQuery, BooleanClause.Occur.MUST);
}
}
BooleanQuery query = queryBuilder.build();
if (query.clauses().size() > 0) {
return query;
} else {
return null;
}
}
use of org.elasticsearch.index.mapper.MappedFieldType in project crate by crate.
the class MatchQueryBuilder method singleQuery.
private Query singleQuery(MatchQuery.Type type, String fieldName, BytesRef queryString) {
String field;
MappedFieldType fieldType = mapperService.smartNameFieldType(fieldName);
if (fieldType == null) {
field = fieldName;
} else {
field = fieldType.names().indexName();
}
if (fieldType != null && fieldType.useTermQueryWithQueryString() && !forceAnalyzeQueryString()) {
try {
return fieldType.termQuery(queryString, null);
} catch (RuntimeException e) {
return null;
}
}
Analyzer analyzer = getAnalyzer(fieldType);
InnerQueryBuilder builder = new InnerQueryBuilder(analyzer, fieldType);
Query query;
switch(type) {
case BOOLEAN:
if (options.commonTermsCutoff() == null) {
query = builder.createBooleanQuery(field, BytesRefs.toString(queryString), options.operator());
} else {
query = builder.createCommonTermsQuery(field, BytesRefs.toString(queryString), options.operator(), options.operator(), options.commonTermsCutoff(), fieldType);
}
break;
case PHRASE:
query = builder.createPhraseQuery(field, BytesRefs.toString(queryString), options.phraseSlop());
break;
case PHRASE_PREFIX:
query = builder.createPhrasePrefixQuery(field, BytesRefs.toString(queryString), options.phraseSlop(), options.maxExpansions());
break;
default:
throw new IllegalArgumentException("invalid type: " + type.toString());
}
if (query == null) {
return zeroTermsQuery();
} else {
return query;
}
}
use of org.elasticsearch.index.mapper.MappedFieldType in project crate by crate.
the class BytesRefColumnReferenceTest method testFieldCacheExpression.
@Test
public void testFieldCacheExpression() throws Exception {
MappedFieldType fieldType = StringFieldMapper.Defaults.FIELD_TYPE.clone();
fieldType.setNames(new MappedFieldType.Names(column));
BytesRefColumnReference bytesRefColumn = new BytesRefColumnReference(column, fieldType);
bytesRefColumn.startCollect(ctx);
bytesRefColumn.setNextReader(readerContext);
IndexSearcher searcher = new IndexSearcher(readerContext.reader());
TopDocs topDocs = searcher.search(new MatchAllDocsQuery(), 20);
int i = 0;
StringBuilder builder = new StringBuilder();
for (ScoreDoc doc : topDocs.scoreDocs) {
builder.append(i);
bytesRefColumn.setNextDocId(doc.doc);
assertThat(bytesRefColumn.value().utf8ToString(), is(builder.toString()));
i++;
}
}
Aggregations