Java Code Examples for org.apache.lucene.index.DocValues#getSortedSet()

The following examples show how to use org.apache.lucene.index.DocValues#getSortedSet() . 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: SortedSetFieldSource.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public FunctionValues getValues(Map<Object, Object> context, LeafReaderContext readerContext) throws IOException {
  SortedSetDocValues sortedSet = DocValues.getSortedSet(readerContext.reader(), field);
  SortedDocValues view = SortedSetSelector.wrap(sortedSet, selector);
  return new DocTermsIndexDocValues(this, view) {
    @Override
    protected String toTerm(String readableValue) {
      return readableValue;
    }

    @Override
    public Object objectVal(int doc) throws IOException {
      return strVal(doc);
    }
  };
}
 
Example 2
Source File: ToParentBlockJoinSortField.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private FieldComparator<?> getStringComparator(int numHits) {
  return new FieldComparator.TermOrdValComparator(numHits, getField(), missingValue == STRING_LAST) {

    @Override
    protected SortedDocValues getSortedDocValues(LeafReaderContext context, String field) throws IOException {
      SortedSetDocValues sortedSet = DocValues.getSortedSet(context.reader(), field);
      final BlockJoinSelector.Type type = order
          ? BlockJoinSelector.Type.MAX
          : BlockJoinSelector.Type.MIN;
      final BitSet parents = parentFilter.getBitSet(context);
      final BitSet children = childFilter.getBitSet(context);
      if (children == null) {
        return DocValues.emptySorted();
      }
      return BlockJoinSelector.wrap(sortedSet, type, parents, toIter(children));
    }

  };
}
 
Example 3
Source File: BooleanMultiField.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public void doSetNextReader(LeafReaderContext context) throws IOException {
  docValues = DocValues.getSortedSet(context.reader(), fieldName);

  // figure out what ord maps to true
  long numOrds = docValues.getValueCount();
  // if no values in the segment, default trueOrd to something other then -1 (missing)
  int trueOrd = -2;
  for (int i=0; i<numOrds; i++) {
    final BytesRef br = docValues.lookupOrd(i);
    if (br.length==1 && br.bytes[br.offset]=='T') {
      trueOrd = i;
      break;
    }
  }

  this.trueOrd = trueOrd;
}
 
Example 4
Source File: TopLevelJoinQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private SortedSetDocValues validateAndFetchDocValues(SolrIndexSearcher solrSearcher, String fieldName, String querySide) throws IOException {
  final IndexSchema schema = solrSearcher.getSchema();
  final SchemaField field = schema.getFieldOrNull(fieldName);
  if (field == null) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, querySide + " field '" + fieldName + "' does not exist");
  }

  if (!field.hasDocValues()) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
        "'top-level' join queries require both 'from' and 'to' fields to have docValues, but " + querySide +
            " field [" + fieldName +  "] does not.");
  }

  final LeafReader leafReader = solrSearcher.getSlowAtomicReader();
  if (field.multiValued()) {
    return DocValues.getSortedSet(leafReader, fieldName);
  }
  return DocValues.singleton(DocValues.getSorted(leafReader, fieldName));
}
 
Example 5
Source File: TermGroupFacetCollector.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
protected void doSetNextReader(LeafReaderContext context) throws IOException {
  if (segmentFacetCounts != null) {
    segmentResults.add(createSegmentResult());
  }

  groupFieldTermsIndex = DocValues.getSorted(context.reader(), groupField);
  facetFieldDocTermOrds = DocValues.getSortedSet(context.reader(), facetField);
  facetFieldNumTerms = (int) facetFieldDocTermOrds.getValueCount();
  if (facetFieldNumTerms == 0) {
    facetOrdTermsEnum = null;
  } else {
    facetOrdTermsEnum = facetFieldDocTermOrds.termsEnum();
  }
  // [facetFieldNumTerms() + 1] for all possible facet values and docs not containing facet field
  segmentFacetCounts = new int[facetFieldNumTerms + 1];
  segmentTotalCount = 0;

  segmentGroupedFacetHits.clear();
  for (GroupedFacetHit groupedFacetHit : groupedFacetHits) {
    int groupOrd = groupedFacetHit.groupValue == null ? -1 : groupFieldTermsIndex.lookupTerm(groupedFacetHit.groupValue);
    if (groupedFacetHit.groupValue != null && groupOrd < 0) {
      continue;
    }

    int facetOrd;
    if (groupedFacetHit.facetValue != null) {
      if (facetOrdTermsEnum == null || !facetOrdTermsEnum.seekExact(groupedFacetHit.facetValue)) {
        continue;
      }
      facetOrd = (int) facetOrdTermsEnum.ord();
    } else {
      facetOrd = facetFieldNumTerms;
    }

    // (facetFieldDocTermOrds.numTerms() + 1) for all possible facet values and docs not containing facet field
    int segmentGroupedFacetsIndex = groupOrd * (facetFieldNumTerms + 1) + facetOrd;
    segmentGroupedFacetHits.put(segmentGroupedFacetsIndex);
  }

  if (facetPrefix != null) {
    TermsEnum.SeekStatus seekStatus;
    if (facetOrdTermsEnum != null) {
      seekStatus = facetOrdTermsEnum.seekCeil(facetPrefix);
    } else {
      seekStatus = TermsEnum.SeekStatus.END;
    }

    if (seekStatus != TermsEnum.SeekStatus.END) {
      startFacetOrd = (int) facetOrdTermsEnum.ord();
    } else {
      startFacetOrd = 0;
      endFacetOrd = 0;
      return;
    }

    BytesRefBuilder facetEndPrefix = new BytesRefBuilder();
    facetEndPrefix.append(facetPrefix);
    facetEndPrefix.append(UnicodeUtil.BIG_TERM);
    seekStatus = facetOrdTermsEnum.seekCeil(facetEndPrefix.get());
    if (seekStatus != TermsEnum.SeekStatus.END) {
      endFacetOrd = (int) facetOrdTermsEnum.ord();
    } else {
      endFacetOrd = facetFieldNumTerms; // Don't include null...
    }
  } else {
    startFacetOrd = 0;
    endFacetOrd = facetFieldNumTerms + 1;
  }
}
 
Example 6
Source File: DocValuesTermsCollector.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
static Function<SortedSetDocValues> sortedSetDocValues(String field) {
  return (ctx) -> DocValues.getSortedSet(ctx, field);
}
 
Example 7
Source File: DocValuesTermsQuery.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
  return new ConstantScoreWeight(this, boost) {

    @Override
    public Scorer scorer(LeafReaderContext context) throws IOException {
      final SortedSetDocValues values = DocValues.getSortedSet(context.reader(), field);
      final LongBitSet bits = new LongBitSet(values.getValueCount());
      boolean matchesAtLeastOneTerm = false;
      TermIterator iterator = termData.iterator();
      for (BytesRef term = iterator.next(); term != null; term = iterator.next()) {
        final long ord = values.lookupTerm(term);
        if (ord >= 0) {
          matchesAtLeastOneTerm = true;
          bits.set(ord);
        }
      }
      if (matchesAtLeastOneTerm == false) {
        return null;
      }
      return new ConstantScoreScorer(this, score(), scoreMode, new TwoPhaseIterator(values) {

        @Override
        public boolean matches() throws IOException {
          for (long ord = values.nextOrd(); ord != SortedSetDocValues.NO_MORE_ORDS; ord = values.nextOrd()) {
            if (bits.get(ord)) {
              return true;
            }
          }
          return false;
        }

        @Override
        public float matchCost() {
          return 3; // lookup in a bitset
        }

      });
    }

    @Override
    public boolean isCacheable(LeafReaderContext ctx) {
      return DocValues.isCacheable(ctx, field);
    }

  };
}
 
Example 8
Source File: StringMultiField.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public void doSetNextReader(LeafReaderContext context) throws IOException {
  docValues = DocValues.getSortedSet(context.reader(), fieldName);
}
 
Example 9
Source File: IntMultiTrieField.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public void doSetNextReader(LeafReaderContext context) throws IOException {
  docValues = DocValues.getSortedSet(context.reader(), fieldName);
}
 
Example 10
Source File: FloatMultiTrieField.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public void doSetNextReader(LeafReaderContext context) throws IOException {
  docValues = DocValues.getSortedSet(context.reader(), fieldName);
}
 
Example 11
Source File: LongMultiTrieField.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public void doSetNextReader(LeafReaderContext context) throws IOException {
  docValues = DocValues.getSortedSet(context.reader(), fieldName);
}
 
Example 12
Source File: DoubleMultiTrieField.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public void doSetNextReader(LeafReaderContext context) throws IOException {
  docValues = DocValues.getSortedSet(context.reader(), fieldName);
}
 
Example 13
Source File: TermsQParserPlugin.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public Weight createWeight(IndexSearcher searcher, final ScoreMode scoreMode, float boost) throws IOException {
  if (! (searcher instanceof SolrIndexSearcher)) {
    log.debug("Falling back to DocValuesTermsQuery because searcher [{}] is not the required SolrIndexSearcher", searcher);
    return super.createWeight(searcher, scoreMode, boost);
  }

  topLevelDocValues = DocValues.getSortedSet(((SolrIndexSearcher)searcher).getSlowAtomicReader(), fieldName);
  topLevelTermOrdinals = new LongBitSet(topLevelDocValues.getValueCount());
  PrefixCodedTerms.TermIterator iterator = getTerms().iterator();

  long lastTermOrdFound = 0;
  for(BytesRef term = iterator.next(); term != null; term = iterator.next()) {
    long currentTermOrd = lookupTerm(topLevelDocValues, term, lastTermOrdFound);
    if (currentTermOrd >= 0L) {
      matchesAtLeastOneTerm = true;
      topLevelTermOrdinals.set(currentTermOrd);
      lastTermOrdFound = currentTermOrd;
    }
  }

  return new ConstantScoreWeight(this, boost) {
    public Scorer scorer(LeafReaderContext context) throws IOException {
      if (! matchesAtLeastOneTerm) {
        return null;
      }
      
      SortedSetDocValues segmentDocValues = context.reader().getSortedSetDocValues(fieldName);
      if (segmentDocValues == null) {
        return null;
      }

      final int docBase = context.docBase;
      return new ConstantScoreScorer(this, this.score(), scoreMode, new TwoPhaseIterator(segmentDocValues) {
        public boolean matches() throws IOException {
          topLevelDocValues.advanceExact(docBase + approximation.docID());
          for(long ord = topLevelDocValues.nextOrd(); ord != -1L; ord = topLevelDocValues.nextOrd()) {
            if (topLevelTermOrdinals.get(ord)) {
              return true;
            }
          }

          return false;
        }

        public float matchCost() {
          return 10.0F;
        }
      });

    }

    public boolean isCacheable(LeafReaderContext ctx) {
      return DocValues.isCacheable(ctx, new String[]{fieldName});
    }
  };
}
 
Example 14
Source File: DocValuesAcc.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public void setNextReader(LeafReaderContext readerContext) throws IOException {
  super.setNextReader(readerContext);
  values = DocValues.getSortedSet(readerContext.reader(), sf.getName());
}
 
Example 15
Source File: PercentileAgg.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public void setNextReader(LeafReaderContext readerContext) throws IOException {
  super.setNextReader(readerContext);
  values = DocValues.getSortedSet(readerContext.reader(),  sf.getName());
}
 
Example 16
Source File: GraphEdgeCollector.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public void doSetNextReader(LeafReaderContext context) throws IOException {
  super.doSetNextReader(context);
  // Grab the updated doc values.
  docTermOrds = DocValues.getSortedSet(context.reader(), collectField.getName());
}