When searching against an index/type, use the type information to derive different search aspects, closes #1391.

This commit is contained in:
Shay Banon 2011-10-18 01:26:53 +02:00
parent 6a146e7ad0
commit 673655cc7b
6 changed files with 42 additions and 3 deletions

View File

@ -446,6 +446,36 @@ public class MapperService extends AbstractIndexComponent implements Iterable<Do
return fields; return fields;
} }
public SmartNameObjectMapper smartNameObjectMapper(String smartName, @Nullable String[] types) {
if (types == null || types.length == 0) {
return smartNameObjectMapper(smartName);
}
for (String type : types) {
DocumentMapper possibleDocMapper = mappers.get(type);
if (possibleDocMapper != null) {
ObjectMapper mapper = possibleDocMapper.objectMappers().get(smartName);
if (mapper != null) {
return new SmartNameObjectMapper(mapper, possibleDocMapper);
}
}
}
// did not find one, see if its prefixed by type
int dotIndex = smartName.indexOf('.');
if (dotIndex != -1) {
String possibleType = smartName.substring(0, dotIndex);
DocumentMapper possibleDocMapper = mappers.get(possibleType);
if (possibleDocMapper != null) {
String possiblePath = smartName.substring(dotIndex + 1);
ObjectMapper mapper = possibleDocMapper.objectMappers().get(possiblePath);
if (mapper != null) {
return new SmartNameObjectMapper(mapper, possibleDocMapper);
}
}
}
// did not explicitly find one under the types provided, or prefixed by type...
return null;
}
public SmartNameObjectMapper smartNameObjectMapper(String smartName) { public SmartNameObjectMapper smartNameObjectMapper(String smartName) {
int dotIndex = smartName.indexOf('.'); int dotIndex = smartName.indexOf('.');
if (dotIndex != -1) { if (dotIndex != -1) {

View File

@ -105,7 +105,7 @@ public class NestedFilterParser implements FilterParser {
query.setBoost(boost); query.setBoost(boost);
MapperService.SmartNameObjectMapper mapper = parseContext.mapperService().smartNameObjectMapper(path); MapperService.SmartNameObjectMapper mapper = parseContext.smartObjectMapper(path);
if (mapper == null) { if (mapper == null) {
throw new QueryParsingException(parseContext.index(), "[nested] failed to find nested object under path [" + path + "]"); throw new QueryParsingException(parseContext.index(), "[nested] failed to find nested object under path [" + path + "]");
} }

View File

@ -110,7 +110,7 @@ public class NestedQueryParser implements QueryParser {
query.setBoost(boost); query.setBoost(boost);
MapperService.SmartNameObjectMapper mapper = parseContext.mapperService().smartNameObjectMapper(path); MapperService.SmartNameObjectMapper mapper = parseContext.smartObjectMapper(path);
if (mapper == null) { if (mapper == null) {
throw new QueryParsingException(parseContext.index(), "[nested] failed to find nested object under path [" + path + "]"); throw new QueryParsingException(parseContext.index(), "[nested] failed to find nested object under path [" + path + "]");
} }

View File

@ -223,4 +223,9 @@ public class QueryParseContext {
SearchContext searchContext = SearchContext.current(); SearchContext searchContext = SearchContext.current();
return indexQueryParser.mapperService.smartName(name, searchContext == null ? null : searchContext.types()); return indexQueryParser.mapperService.smartName(name, searchContext == null ? null : searchContext.types());
} }
public MapperService.SmartNameObjectMapper smartObjectMapper(String name) {
SearchContext searchContext = SearchContext.current();
return indexQueryParser.mapperService.smartNameObjectMapper(name, searchContext == null ? null : searchContext.types());
}
} }

View File

@ -113,7 +113,7 @@ public class FacetParseElement implements SearchParseElement {
if (nestedPath != null) { if (nestedPath != null) {
// its a nested facet, wrap the collector with a facet one... // its a nested facet, wrap the collector with a facet one...
MapperService.SmartNameObjectMapper mapper = context.mapperService().smartNameObjectMapper(nestedPath); MapperService.SmartNameObjectMapper mapper = context.smartNameObjectMapper(nestedPath);
if (mapper == null) { if (mapper == null) {
throw new SearchParseException(context, "facet nested path [" + nestedPath + "] not found"); throw new SearchParseException(context, "facet nested path [" + nestedPath + "] not found");
} }

View File

@ -546,4 +546,8 @@ public class SearchContext implements Releasable {
public FieldMapper smartNameFieldMapper(String name) { public FieldMapper smartNameFieldMapper(String name) {
return mapperService().smartNameFieldMapper(name, types); return mapperService().smartNameFieldMapper(name, types);
} }
public MapperService.SmartNameObjectMapper smartNameObjectMapper(String name) {
return mapperService().smartNameObjectMapper(name, types);
}
} }