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

The following examples show how to use org.apache.lucene.queries.function.ValueSource. 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 File: CommandHandler.java    From lucene-solr with 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 #2
Source File: GroupingSearchTest.java    From lucene-solr with 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 #3
Source File: DirectUpdateHandler2.java    From lucene-solr with 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 #4
Source File: GeoDistValueSourceParser.java    From lucene-solr with 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 #5
Source File: ValueSourceParser.java    From lucene-solr with 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 #6
Source File: ValueSourceParser.java    From lucene-solr with 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 #7
Source File: ValueSourceParser.java    From lucene-solr with 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 #8
Source File: PointType.java    From lucene-solr with 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 #9
Source File: MultiFunction.java    From lucene-solr with 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 #10
Source File: FunctionQParser.java    From lucene-solr with 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 #11
Source File: CountUsageValueSourceParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public ValueSource parse(FunctionQParser fp) throws SyntaxError {
  String key = fp.parseArg();
  double val = fp.parseDouble();
  
  AtomicInteger counter = new AtomicInteger();
  if (null != counters.putIfAbsent(key, counter)) {
    throw new IllegalArgumentException("Key has already been used: " + key);
  } 
  return new CountDocsValueSource(counter, val);
}
 
Example #12
Source File: ValueSourceParser.java    From lucene-solr with 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 #13
Source File: FunctionQParser.java    From lucene-solr with 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 #14
Source File: ValueSourceParser.java    From lucene-solr with 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 #15
Source File: VarianceAgg.java    From lucene-solr with 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 #16
Source File: ValueSourceParser.java    From lucene-solr with 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 #17
Source File: SumAgg.java    From lucene-solr with 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 SumSortedNumericAcc(fcontext, sf, numSlots);
        }
        return new SumSortedSetAcc(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 SumUnInvertedFieldAcc(fcontext, sf, numSlots);
    }
    vs = sf.getType().getValueSource(sf, null);
  }
  return new SlotAcc.SumSlotAcc(vs, fcontext, numSlots);
}
 
Example #18
Source File: NestedQParserPlugin.java    From lucene-solr with 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 #19
Source File: FunctionRangeQuery.java    From lucene-solr with 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 #20
Source File: Grouping.java    From lucene-solr with 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 #21
Source File: TaggerRequestHandler.java    From lucene-solr with 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()];
}
 
Example #22
Source File: DocBasedVersionConstraintsProcessor.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@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 #23
Source File: ValueSourceParser.java    From lucene-solr with 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 #24
Source File: ValueSourceAugmenter.java    From lucene-solr with 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 #25
Source File: ValueSourceParser.java    From lucene-solr with 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 #26
Source File: AllGroupsCollectorTest.java    From lucene-solr with 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 #27
Source File: ValueSourceParser.java    From lucene-solr with 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 #28
Source File: TestGrouping.java    From lucene-solr with 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 #29
Source File: TestGrouping.java    From lucene-solr with 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 #30
Source File: PointField.java    From lucene-solr with 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);
}