use of com.yahoo.prelude.query.Item in project vespa by vespa-engine.
the class QueryCombinator method addAndItems.
private void addAndItems(Query query, Iterable<QueryPart> pieces, Execution.Context context) {
IndexFacts indexFacts = context.getIndexFacts();
IndexFacts.Session session = indexFacts.newSession(query);
Set<String> usedSources = new HashSet<>(session.documentTypes());
Language language = query.getModel().getParsingLanguage();
for (QueryPart part : pieces) {
String defaultIndex;
Item item = null;
Index index = session.getIndex(part.defaultIndex);
if (index == Index.nullIndex) {
defaultIndex = null;
} else {
defaultIndex = part.defaultIndex;
}
try {
CustomParser parser = (CustomParser) ParserFactory.newInstance(query.getModel().getType(), ParserEnvironment.fromExecutionContext(context));
item = parser.parse(part.query, null, language, usedSources, indexFacts, defaultIndex);
} catch (RuntimeException e) {
String err = Exceptions.toMessageString(e);
query.trace("Query parser threw an exception: " + err, true, 1);
getLogger().log(LogLevel.WARNING, "Query parser threw exception searcher QueryCombinator for " + query.getHttpRequest().toString() + ", query part " + part.query + ": " + err);
}
if (item == null) {
continue;
}
if (defaultIndex == null) {
assignDefaultIndex(item, part.defaultIndex);
}
addAndItem(query.getModel().getQueryTree(), item);
}
}
use of com.yahoo.prelude.query.Item in project vespa by vespa-engine.
the class RangeQueryOptimizer method optimizeAnd.
private boolean optimizeAnd(AndItem and, IndexFacts.Session indexFacts) {
// Find consolidated ranges by collecting a list of compatible ranges
List<FieldRange> fieldRanges = null;
for (Iterator<Item> i = and.getItemIterator(); i.hasNext(); ) {
Item item = i.next();
if (!(item instanceof IntItem))
continue;
IntItem intItem = (IntItem) item;
// each such op gets a different partial set: Cannot be optimized
if (intItem.getHitLimit() != 0)
continue;
// don't optimize searches for single numbers
if (intItem.getFromLimit().equals(intItem.getToLimit()))
continue;
// May match different values in each range
if (indexFacts.getIndex(intItem.getIndexName()).isMultivalue())
continue;
if (fieldRanges == null)
fieldRanges = new ArrayList<>();
Optional<FieldRange> compatibleRange = findCompatibleRange(intItem, fieldRanges);
if (compatibleRange.isPresent())
compatibleRange.get().addRange(intItem);
else
fieldRanges.add(new FieldRange(intItem));
i.remove();
}
// Add consolidated ranges
if (fieldRanges == null)
return false;
boolean optimized = false;
for (FieldRange fieldRange : fieldRanges) {
and.addItem(fieldRange.toItem());
optimized |= fieldRange.isOptimization();
}
return optimized;
}
use of com.yahoo.prelude.query.Item in project vespa by vespa-engine.
the class CompositeConverter method itemToForm.
@Override
public DispatchForm itemToForm(Item item, ItemIdMapper itemIdMapper) {
CompositeItem compositeItem = (CompositeItem) item;
DispatchForm form = new DispatchForm(getFormName(item));
for (ListIterator<Item> i = compositeItem.getItemIterator(); i.hasNext(); ) {
form.addChild(i.next());
}
ItemInitializer.initializeForm(form, item, itemIdMapper);
return form;
}
use of com.yahoo.prelude.query.Item in project vespa by vespa-engine.
the class YqlParser method leafStyleSettings.
@NonNull
private <T extends TaggableItem> T leafStyleSettings(OperatorNode<?> ast, @NonNull T out) {
{
Map<?, ?> connectivity = getAnnotation(ast, CONNECTIVITY, Map.class, null, "connectivity settings");
if (connectivity != null) {
connectedItems.add(new ConnectedItem(out, getMapValue(CONNECTIVITY, connectivity, CONNECTION_ID, Integer.class), getMapValue(CONNECTIVITY, connectivity, CONNECTION_WEIGHT, Number.class).doubleValue()));
}
Number significance = getAnnotation(ast, SIGNIFICANCE, Number.class, null, "term significance");
if (significance != null) {
out.setSignificance(significance.doubleValue());
}
Integer uniqueId = getAnnotation(ast, UNIQUE_ID, Integer.class, null, "term ID", false);
if (uniqueId != null) {
out.setUniqueID(uniqueId);
identifiedItems.put(uniqueId, out);
}
}
{
Item leaf = (Item) out;
Map<?, ?> itemAnnotations = getAnnotation(ast, ANNOTATIONS, Map.class, Collections.emptyMap(), "item annotation map");
for (Map.Entry<?, ?> entry : itemAnnotations.entrySet()) {
Preconditions.checkArgument(entry.getKey() instanceof String, "Expected String annotation key, got %s.", entry.getKey().getClass());
Preconditions.checkArgument(entry.getValue() instanceof String, "Expected String annotation value, got %s.", entry.getValue().getClass());
leaf.addAnnotation((String) entry.getKey(), entry.getValue());
}
Boolean filter = getAnnotation(ast, FILTER, Boolean.class, null, FILTER_DESCRIPTION);
if (filter != null) {
leaf.setFilter(filter);
}
Boolean isRanked = getAnnotation(ast, RANKED, Boolean.class, null, RANKED_DESCRIPTION);
if (isRanked != null) {
leaf.setRanked(isRanked);
}
String label = getAnnotation(ast, LABEL, String.class, null, "item label");
if (label != null) {
leaf.setLabel(label);
}
Integer weight = getAnnotation(ast, WEIGHT, Integer.class, null, "term weight for ranking");
if (weight != null) {
leaf.setWeight(weight);
}
}
if (out instanceof IntItem) {
IntItem number = (IntItem) out;
Integer hitLimit = getCappedRangeSearchParameter(ast);
if (hitLimit != null) {
number.setHitLimit(hitLimit);
}
}
return out;
}
use of com.yahoo.prelude.query.Item in project vespa by vespa-engine.
the class YqlParser method parseUserInput.
@NonNull
private Item parseUserInput(String grammar, String defaultIndex, String wordData, Language language, boolean allowNullItem) {
Query.Type parseAs = Query.Type.getType(grammar);
Parser parser = ParserFactory.newInstance(parseAs, environment);
// perhaps not use already resolved doctypes, but respect source and restrict
Item item = parser.parse(new Parsable().setQuery(wordData).addSources(docTypes).setLanguage(language).setDefaultIndexName(defaultIndex)).getRoot();
// the null check should be unnecessary, but is there to avoid having to suppress null warnings
if (!allowNullItem && (item == null || item instanceof NullItem))
throw new IllegalArgumentException("Parsing '" + wordData + "' only resulted in NullItem.");
if (// mark the language used, unless it's the default
language != Language.ENGLISH)
item.setLanguage(language);
return item;
}
Aggregations