Java Code Examples for org.apache.lucene.queries.function.ValueSource

The following examples show how to use org.apache.lucene.queries.function.ValueSource. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: lucene-solr   Source File: GroupingSearchTest.java    License: Apache License 2.0 6 votes vote down vote up
private GroupingSearch createRandomGroupingSearch(String groupField, Sort groupSort, int docsInGroup, boolean canUseIDV) {
  GroupingSearch groupingSearch;
  if (random().nextBoolean()) {
    ValueSource vs = new BytesRefFieldSource(groupField);
    groupingSearch = new GroupingSearch(vs, new HashMap<>());
  } else {
    groupingSearch = new GroupingSearch(groupField);
  }

  groupingSearch.setGroupSort(groupSort);
  groupingSearch.setGroupDocsLimit(docsInGroup);

  if (random().nextBoolean()) {
    groupingSearch.setCachingInMB(4.0, true);
  }

  return groupingSearch;
}
 
Example 2
Source Project: lucene-solr   Source File: DirectUpdateHandler2.java    License: Apache License 2.0 6 votes vote down vote up
private Query getQuery(DeleteUpdateCommand cmd) {
  Query q;
  try {
    // move this higher in the stack?
    QParser parser = QParser.getParser(cmd.getQuery(), cmd.req);
    q = parser.getQuery();
    q = QueryUtils.makeQueryable(q);

    // Make sure not to delete newer versions
    if (ulog != null && cmd.getVersion() != 0 && cmd.getVersion() != -Long.MAX_VALUE) {
      BooleanQuery.Builder bq = new BooleanQuery.Builder();
      bq.add(q, Occur.MUST);
      SchemaField sf = ulog.getVersionInfo().getVersionField();
      ValueSource vs = sf.getType().getValueSource(sf, null);
      ValueSourceRangeFilter filt = new ValueSourceRangeFilter(vs, Long.toString(Math.abs(cmd.getVersion())), null, true, true);
      FunctionRangeQuery range = new FunctionRangeQuery(filt);
      bq.add(range, Occur.MUST_NOT);  // formulated in the "MUST_NOT" sense so we can delete docs w/o a version (some tests depend on this...)
      q = bq.build();
    }

    return q;

  } catch (SyntaxError e) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
  }
}
 
Example 3
Source Project: lucene-solr   Source File: GeoDistValueSourceParser.java    License: Apache License 2.0 6 votes vote down vote up
private MultiValueSource parseSfield(FunctionQParser fp) throws SyntaxError {
  String sfield = fp.getParam(SpatialParams.FIELD);
  if (sfield == null) return null;
  SchemaField sf = fp.getReq().getSchema().getField(sfield);
  FieldType type = sf.getType();
  if (type instanceof AbstractSpatialFieldType) {
    @SuppressWarnings({"rawtypes"})
    AbstractSpatialFieldType asft = (AbstractSpatialFieldType) type;
    return new SpatialStrategyMultiValueSource(asft.getStrategy(sfield), asft.getDistanceUnits());
  }
  ValueSource vs = type.getValueSource(sf, fp);
  if (vs instanceof MultiValueSource) {
    return (MultiValueSource)vs;
  }
  throw new SyntaxError("Spatial field must implement MultiValueSource or extend AbstractSpatialFieldType:" + sf);
}
 
Example 4
Source Project: lucene-solr   Source File: ValueSourceParser.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public ValueSource parse(FunctionQParser fp) throws SyntaxError {
  List<ValueSource> sources = fp.parseValueSourceList();
  return new MultiBoolFunction(sources) {
    @Override
    protected String name() {
      return "xor";
    }
    @Override
    protected boolean func(int doc, FunctionValues[] vals) throws IOException {
      int nTrue=0, nFalse=0;
      for (FunctionValues dv : vals) {
        if (dv.boolVal(doc)) nTrue++;
        else nFalse++;
      }
      return nTrue != 0 && nFalse != 0;
    }
  };
}
 
Example 5
Source Project: lucene-solr   Source File: CommandHandler.java    License: Apache License 2.0 6 votes vote down vote up
private DocSet computeGroupedDocSet(Query query, ProcessedFilter filter, List<Collector> collectors) throws IOException {
  @SuppressWarnings({"rawtypes"})
  Command firstCommand = commands.get(0);
  String field = firstCommand.getKey();
  SchemaField sf = searcher.getSchema().getField(field);
  FieldType fieldType = sf.getType();
  
  @SuppressWarnings({"rawtypes"})
  final AllGroupHeadsCollector allGroupHeadsCollector;
  if (fieldType.getNumberType() != null) {
    ValueSource vs = fieldType.getValueSource(sf, null);
    allGroupHeadsCollector = AllGroupHeadsCollector.newCollector(new ValueSourceGroupSelector(vs, new HashMap<>()),
        firstCommand.getWithinGroupSort());
  } else {
    allGroupHeadsCollector
        = AllGroupHeadsCollector.newCollector(new TermGroupSelector(firstCommand.getKey()), firstCommand.getWithinGroupSort());
  }
  if (collectors.isEmpty()) {
    searchWithTimeLimiter(query, filter, allGroupHeadsCollector);
  } else {
    collectors.add(allGroupHeadsCollector);
    searchWithTimeLimiter(query, filter, MultiCollector.wrap(collectors.toArray(new Collector[collectors.size()])));
  }

  return new BitDocSet(allGroupHeadsCollector.retrieveGroupHeads(searcher.maxDoc()));
}
 
Example 6
Source Project: lucene-solr   Source File: ValueSourceParser.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ValueSource parse(FunctionQParser fp) throws SyntaxError {
  ValueSource source = fp.parseValueSource();
  float slope = fp.parseFloat();
  float intercept = fp.parseFloat();
  return new LinearFloatFunction(source, slope, intercept);
}
 
Example 7
Source Project: lucene-solr   Source File: NestedQParserPlugin.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
  if (localParams == null) { // avoid an NPE later
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
        "the 'query' QParser must be invoked with local-params, e.g. {!query defType=...}");
  }
  return new QParser(qstr, localParams, params, req) {
    QParser baseParser;
    ValueSource vs;
    String b;

    @Override
    public Query parse() throws SyntaxError {
      baseParser = subQuery(localParams.get(QueryParsing.V), null);
      return baseParser.getQuery();
    }

    @Override
    public String[] getDefaultHighlightFields() {
      return baseParser.getDefaultHighlightFields();
    }

    @Override
    public Query getHighlightQuery() throws SyntaxError {
      return baseParser.getHighlightQuery();
    }

    @Override
    public void addDebugInfo(NamedList<Object> debugInfo) {
      // encapsulate base debug info in a sub-list?
      baseParser.addDebugInfo(debugInfo);
    }
  };
}
 
Example 8
Source Project: lucene-solr   Source File: ValueSourceParser.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ValueSource parse(FunctionQParser fp) throws SyntaxError {
  ValueSource source = fp.parseValueSource();
  float min = fp.parseFloat();
  float max = fp.parseFloat();
  ValueSource target = fp.parseValueSource();
  ValueSource def = fp.hasMoreArguments() ? fp.parseValueSource() : null;
  return new RangeMapFloatFunction(source, min, max, target, def);
}
 
Example 9
Source Project: lucene-solr   Source File: ValueSourceParser.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ValueSource parse(FunctionQParser fp) throws SyntaxError {
  ValueSource lhsValSource = fp.parseValueSource();
  ValueSource rhsValSource = fp.parseValueSource();

  return new EqualFunction(lhsValSource, rhsValSource, "eq");
}
 
Example 10
Source Project: lucene-solr   Source File: VarianceAgg.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public SlotAcc createSlotAcc(FacetContext fcontext, long numDocs, int numSlots) throws IOException {
  ValueSource vs = getArg();

  if (vs instanceof FieldNameValueSource) {
    String field = ((FieldNameValueSource) vs).getFieldName();
    SchemaField sf = fcontext.qcontext.searcher().getSchema().getField(field);
    if (sf.getType().getNumberType() == null) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
          name() + " aggregation not supported for " + sf.getType().getTypeName());
    }
    if (sf.multiValued() || sf.getType().multiValuedFieldCache()) {
      if (sf.hasDocValues()) {
        if (sf.getType().isPointField()) {
          return new VarianceSortedNumericAcc(fcontext, sf, numSlots);
        }
        return new VarianceSortedSetAcc(fcontext, sf, numSlots);
      }
      if (sf.getType().isPointField()) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
            name() + " aggregation not supported for PointField w/o docValues");
      }
      return new VarianceUnInvertedFieldAcc(fcontext, sf, numSlots);
    }
    vs = sf.getType().getValueSource(sf, null);
  }
  return new SlotAcc.VarianceSlotAcc(vs, fcontext, numSlots);
}
 
Example 11
Source Project: lucene-solr   Source File: ValueSourceParser.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ValueSource parse(FunctionQParser fp) throws SyntaxError {
  ValueSource source = fp.parseValueSource();
  float min = fp.parseFloat();
  float max = fp.parseFloat();
  return new ScaleFloatFunction(source, min, max);
}
 
Example 12
Source Project: lucene-solr   Source File: FunctionQParser.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Parse a list of ValueSource.  Must be the final set of arguments
 * to a ValueSource.
 * 
 * @return List&lt;ValueSource&gt;
 */
public List<ValueSource> parseValueSourceList() throws SyntaxError {
  List<ValueSource> sources = new ArrayList<>(3);
  while (hasMoreArguments()) {
    sources.add(parseValueSource(FLAG_DEFAULT | FLAG_CONSUME_DELIMITER));
  }
  return sources;
}
 
Example 13
Source Project: lucene-solr   Source File: TrieField.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public final ValueSource getSingleValueSource(MultiValueSelector choice, SchemaField field, QParser parser) {
  // trivial base case
  if (!field.multiValued()) {
    // single value matches any selector
    return getValueSource(field, parser);
  }

  // See LUCENE-6709
  if (! field.hasDocValues()) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                            "docValues='true' is required to select '" + choice.toString() +
                            "' value from multivalued field ("+ field.getName() +") at query time");
  }
  
  // multivalued Trie fields all use SortedSetDocValues, so we give a clean error if that's
  // not supported by the specified choice, else we delegate to a helper
  SortedSetSelector.Type selectorType = choice.getSortedSetSelectorType();
  if (null == selectorType) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                            choice.toString() + " is not a supported option for picking a single value"
                            + " from the multivalued field: " + field.getName() +
                            " (type: " + this.getTypeName() + ")");
  }
  
  return getSingleValueSource(selectorType, field);
}
 
Example 14
Source Project: lucene-solr   Source File: FunctionQParser.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Query parse() throws SyntaxError {
  ValueSource vs = null;
  List<ValueSource> lst = null;

  for(;;) {
    ValueSource valsource = parseValueSource(FLAG_DEFAULT & ~FLAG_CONSUME_DELIMITER);
    sp.eatws();
    if (!parseMultipleSources) {
      vs = valsource; 
      break;
    } else {
      if (lst != null) {
        lst.add(valsource);
      } else {
        vs = valsource;
      }
    }

    // check if there is a "," separator
    if (sp.peek() != ',') break;

    consumeArgumentDelimiter();

    if (lst == null) {
      lst = new ArrayList<>(2);
      lst.add(valsource);
    }
  }

  if (parseToEnd && sp.pos < sp.end) {
    throw new SyntaxError("Unexpected text after function: " + sp.val.substring(sp.pos, sp.end));
  }

  if (lst != null) {
    vs = new VectorValueSource(lst);
  }

  return new FunctionQuery(vs);
}
 
Example 15
Source Project: lucene-solr   Source File: MultiFunction.java    License: Apache License 2.0 5 votes vote down vote up
public static String description(String name, List<ValueSource> sources) {
  StringBuilder sb = new StringBuilder();
  sb.append(name).append('(');
  boolean firstTime=true;
  for (ValueSource source : sources) {
    if (firstTime) {
      firstTime=false;
    } else {
      sb.append(',');
    }
    sb.append(source);
  }
  sb.append(')');
  return sb.toString();
}
 
Example 16
Source Project: lucene-solr   Source File: PointType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ValueSource getValueSource(SchemaField field, QParser parser) {
  ArrayList<ValueSource> vs = new ArrayList<>(dimension);
  for (int i=0; i<dimension; i++) {
    SchemaField sub = subField(field, i, schema);
    vs.add(sub.getType().getValueSource(sub, parser));
  }
  return new PointTypeValueSource(field, vs);
}
 
Example 17
Source Project: lucene-solr   Source File: ValueSourceParser.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ValueSource parse(FunctionQParser fp) throws SyntaxError {
  ValueSource lhsValSource = fp.parseValueSource();
  ValueSource rhsValSource = fp.parseValueSource();

  return new SolrComparisonBoolFunction(lhsValSource, rhsValSource, "gte", (cmp) -> cmp >= 0);

}
 
Example 18
Source Project: lucene-solr   Source File: ValueSourceParser.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ValueSource parse(FunctionQParser fp) throws SyntaxError {
  int ms = fp.parseInt();
  ValueSource source = fp.parseValueSource();
  try {
    Thread.sleep(ms);
  } catch (InterruptedException e) {
    throw new RuntimeException(e);
  }
  return source;
}
 
Example 19
Source Project: lucene-solr   Source File: ValueSourceParser.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ValueSource parse(FunctionQParser fp) throws SyntaxError {
  ValueSource vs = fp.parseValueSource();
  return new SimpleBoolFunction(vs) {
    @Override
    protected boolean func(int doc, FunctionValues vals) throws IOException {
      return !vals.boolVal(doc);
    }
    @Override
    protected String name() {
      return "not";
    }
  };
}
 
Example 20
Source Project: lucene-solr   Source File: ValueSourceParser.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ValueSource parse(FunctionQParser fp) throws SyntaxError {
  ValueSource a = fp.parseValueSource();
  ValueSource b = fp.parseValueSource();
  return new DualFloatFunction(a, b) {
    @Override
    protected String name() {
      return "mod";
    }
    @Override
    protected float func(int doc, FunctionValues aVals, FunctionValues bVals) throws IOException {
      return aVals.floatVal(doc) % bVals.floatVal(doc);
    }
  };
}
 
Example 21
Source Project: lucene-solr   Source File: TestGrouping.java    License: Apache License 2.0 5 votes vote down vote up
private FirstPassGroupingCollector<?> createRandomFirstPassCollector(String groupField, Sort groupSort, int topDocs) throws IOException {
  if (random().nextBoolean()) {
    ValueSource vs = new BytesRefFieldSource(groupField);
    return new FirstPassGroupingCollector<>(new ValueSourceGroupSelector(vs, new HashMap<>()), groupSort, topDocs);
  } else {
    return new FirstPassGroupingCollector<>(new TermGroupSelector(groupField), groupSort, topDocs);
  }
}
 
Example 22
Source Project: lucene-solr   Source File: TestGrouping.java    License: Apache License 2.0 5 votes vote down vote up
private FirstPassGroupingCollector<?> createFirstPassCollector(String groupField, Sort groupSort, int topDocs, FirstPassGroupingCollector<?> firstPassGroupingCollector) throws IOException {
  GroupSelector<?> selector = firstPassGroupingCollector.getGroupSelector();
  if (TermGroupSelector.class.isAssignableFrom(selector.getClass())) {
    ValueSource vs = new BytesRefFieldSource(groupField);
    return new FirstPassGroupingCollector<>(new ValueSourceGroupSelector(vs, new HashMap<>()), groupSort, topDocs);
  } else {
    return new FirstPassGroupingCollector<>(new TermGroupSelector(groupField), groupSort, topDocs);
  }
}
 
Example 23
Source Project: lucene-solr   Source File: PointField.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public final ValueSource getSingleValueSource(MultiValueSelector choice, SchemaField field, QParser parser) {
  // trivial base case
  if (!field.multiValued()) {
    // single value matches any selector
    return getValueSource(field, parser);
  }

  // Point fields don't support UninvertingReader. See SOLR-9202
  if (!field.hasDocValues()) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                            "docValues='true' is required to select '" + choice.toString() +
                            "' value from multivalued field ("+ field.getName() +") at query time");
  }
  
  // multivalued Point fields all use SortedSetDocValues, so we give a clean error if that's
  // not supported by the specified choice, else we delegate to a helper
  SortedNumericSelector.Type selectorType = choice.getSortedNumericSelectorType();
  if (null == selectorType) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                            choice.toString() + " is not a supported option for picking a single value"
                            + " from the multivalued field: " + field.getName() +
                            " (type: " + this.getTypeName() + ")");
  }
  
  return getSingleValueSource(selectorType, field);
}
 
Example 24
Source Project: lucene-solr   Source File: AllGroupsCollectorTest.java    License: Apache License 2.0 5 votes vote down vote up
private AllGroupsCollector<?> createRandomCollector(String groupField) {
  if (random().nextBoolean()) {
    return new AllGroupsCollector<>(new TermGroupSelector(groupField));
  }
  else {
    ValueSource vs = new BytesRefFieldSource(groupField);
    return new AllGroupsCollector<>(new ValueSourceGroupSelector(vs, new HashMap<>()));
  }
}
 
Example 25
Source Project: lucene-solr   Source File: ValueSourceAugmenter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings({"unchecked"})
public void setContext( ResultContext context ) {
  super.setContext(context);
  try {
    searcher = context.getSearcher();
    readerContexts = searcher.getIndexReader().leaves();
    fcontext = ValueSource.newContext(searcher);
    this.valueSource.createWeight(fcontext, searcher);
  } catch (IOException e) {
    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);
  }
}
 
Example 26
Source Project: lucene-solr   Source File: ValueSourceParser.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ValueSource parse(FunctionQParser fp) throws SyntaxError {

  String fieldName = fp.parseArg();
  SchemaField f = fp.getReq().getSchema().getField(fieldName);
  if (! (f.getType() instanceof CurrencyFieldType)) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                            "Currency function input must be the name of a CurrencyFieldType: " + fieldName);
  }
  CurrencyFieldType ft = (CurrencyFieldType) f.getType();
  String code = fp.hasMoreArguments() ? fp.parseArg() : null;
  return ft.getConvertedValueSource(code, ft.getValueSource(f, fp));
}
 
Example 27
@SuppressWarnings({"unchecked"})
private static FunctionValues getFunctionValues(LeafReaderContext segmentContext,
                                        SchemaField field,
                                        SolrIndexSearcher searcher) throws IOException {
  ValueSource vs = field.getType().getValueSource(field, null);
  @SuppressWarnings({"rawtypes"})
  Map context = ValueSource.newContext(searcher);
  vs.createWeight(context, searcher);
  return vs.getValues(context, segmentContext);
}
 
Example 28
Source Project: lucene-solr   Source File: Grouping.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings({"unchecked"})
protected void prepare() throws IOException {
  context = ValueSource.newContext(searcher);
  groupBy.createWeight(context, searcher);
  actualGroupsToFind = getMax(offset, numGroups, maxDoc);
}
 
Example 29
Source Project: lucene-solr   Source File: FunctionRangeQuery.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public DelegatingCollector getFilterCollector(IndexSearcher searcher) {
  @SuppressWarnings({"rawtypes"})
  Map fcontext = ValueSource.newContext(searcher);
  Weight weight = rangeFilt.createWeight(searcher, ScoreMode.COMPLETE, 1);
  return new FunctionRangeCollector(fcontext, weight);
}
 
Example 30
Source Project: lucene-solr   Source File: TaggerRequestHandler.java    License: Apache License 2.0 5 votes vote down vote up
ValueSourceAccessor(IndexSearcher searcher, ValueSource valueSource) {
  readerContexts = searcher.getIndexReader().leaves();
  this.valueSource = valueSource;
  fContext = ValueSource.newContext(searcher);
  functionValuesPerSeg = new FunctionValues[readerContexts.size()];
  functionValuesDocIdPerSeg = new int[readerContexts.size()];
}