Skip to content

Commit

Permalink
Make _exists_/_missing_ behave consistently with exists/missing.
Browse files Browse the repository at this point in the history
`_exists_` and `_missing_` miss field name expansion that `exists` and
`missing` have, which allows these filters to work on `object` fields.

Close #5142
  • Loading branch information
jpountz committed Feb 20, 2014
1 parent d5f1e6f commit f5b3c08
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,11 @@

package org.apache.lucene.queryparser.classic;

import org.apache.lucene.search.Filter;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermRangeFilter;
import org.elasticsearch.common.lucene.search.XConstantScoreQuery;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.query.ExistsFilterParser;
import org.elasticsearch.index.query.QueryParseContext;

import static org.elasticsearch.index.query.support.QueryParsers.wrapSmartNameFilter;

/**
*
*/
Expand All @@ -37,23 +33,6 @@ public class ExistsFieldQueryExtension implements FieldQueryExtension {

@Override
public Query query(QueryParseContext parseContext, String queryText) {
String fieldName = queryText;
Filter filter = null;
MapperService.SmartNameFieldMappers smartNameFieldMappers = parseContext.smartFieldMappers(fieldName);
if (smartNameFieldMappers != null) {
if (smartNameFieldMappers.hasMapper()) {
filter = smartNameFieldMappers.mapper().rangeFilter(null, null, true, true, parseContext);
}
}
if (filter == null) {
filter = new TermRangeFilter(fieldName, null, null, true, true);
}

// we always cache this one, really does not change...
filter = parseContext.cacheFilter(filter, null);

filter = wrapSmartNameFilter(filter, smartNameFieldMappers, parseContext);

return new XConstantScoreQuery(filter);
return new XConstantScoreQuery(ExistsFilterParser.newFilter(parseContext, queryText, null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,11 @@

package org.apache.lucene.queryparser.classic;

import org.apache.lucene.search.Filter;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermRangeFilter;
import org.elasticsearch.common.lucene.search.NotFilter;
import org.elasticsearch.common.lucene.search.XConstantScoreQuery;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.query.MissingFilterParser;
import org.elasticsearch.index.query.QueryParseContext;

import static org.elasticsearch.index.query.support.QueryParsers.wrapSmartNameFilter;

/**
*
*/
Expand All @@ -38,27 +33,7 @@ public class MissingFieldQueryExtension implements FieldQueryExtension {

@Override
public Query query(QueryParseContext parseContext, String queryText) {
String fieldName = queryText;

Filter filter = null;
MapperService.SmartNameFieldMappers smartNameFieldMappers = parseContext.smartFieldMappers(fieldName);
if (smartNameFieldMappers != null) {
if (smartNameFieldMappers.hasMapper()) {
filter = smartNameFieldMappers.mapper().rangeFilter(null, null, true, true, parseContext);
}
}
if (filter == null) {
filter = new TermRangeFilter(fieldName, null, null, true, true);
}

// we always cache this one, really does not change... (exists)
filter = parseContext.cacheFilter(filter, null);
filter = new NotFilter(filter);
// cache the not filter as well, so it will be faster
filter = parseContext.cacheFilter(filter, null);

filter = wrapSmartNameFilter(filter, smartNameFieldMappers, parseContext);

return new XConstantScoreQuery(filter);
return new XConstantScoreQuery(MissingFilterParser.newFilter(parseContext, queryText,
MissingFilterParser.DEFAULT_EXISTENCE_VALUE, MissingFilterParser.DEFAULT_NULL_VALUE, null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public Filter parse(QueryParseContext parseContext) throws IOException, QueryPar
throw new QueryParsingException(parseContext.index(), "exists must be provided with a [field]");
}

return newFilter(parseContext, fieldPattern, filterName);
}

public static Filter newFilter(QueryParseContext parseContext, String fieldPattern, String filterName) {
MapperService.SmartNameObjectMapper smartNameObjectMapper = parseContext.smartObjectMapper(fieldPattern);
if (smartNameObjectMapper != null && smartNameObjectMapper.hasMapper()) {
// automatic make the object mapper pattern
Expand Down Expand Up @@ -116,4 +120,5 @@ public Filter parse(QueryParseContext parseContext) throws IOException, QueryPar
}
return filter;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
public class MissingFilterParser implements FilterParser {

public static final String NAME = "missing";
public static final boolean DEFAULT_NULL_VALUE = false;
public static final boolean DEFAULT_EXISTENCE_VALUE = true;

@Inject
public MissingFilterParser() {
Expand All @@ -57,8 +59,8 @@ public Filter parse(QueryParseContext parseContext) throws IOException, QueryPar

String fieldPattern = null;
String filterName = null;
boolean nullValue = false;
boolean existence = true;
boolean nullValue = DEFAULT_NULL_VALUE;
boolean existence = DEFAULT_EXISTENCE_VALUE;

XContentParser.Token token;
String currentFieldName = null;
Expand All @@ -84,6 +86,10 @@ public Filter parse(QueryParseContext parseContext) throws IOException, QueryPar
throw new QueryParsingException(parseContext.index(), "missing must be provided with a [field]");
}

return newFilter(parseContext, fieldPattern, existence, nullValue, filterName);
}

public static Filter newFilter(QueryParseContext parseContext, String fieldPattern, boolean existence, boolean nullValue, String filterName) {
if (!existence && !nullValue) {
throw new QueryParsingException(parseContext.index(), "missing must have either existence, or null_value, or both set to true");
}
Expand Down

0 comments on commit f5b3c08

Please sign in to comment.