Java Code Examples for org.apache.lucene.util.BytesRef#bytesEquals()

The following examples show how to use org.apache.lucene.util.BytesRef#bytesEquals() . 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: XAnalyzingSuggester.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private boolean sameSurfaceForm(BytesRef key, BytesRef output2) {
  if (hasPayloads) {
    // output2 has at least PAYLOAD_SEP byte:
    if (key.length >= output2.length) {
      return false;
    }
    for(int i=0;i<key.length;i++) {
      if (key.bytes[key.offset+i] != output2.bytes[output2.offset+i]) {
        return false;
      }
    }
    return output2.bytes[output2.offset + key.length] == payloadSep;
  } else {
    return key.bytesEquals(output2);
  }
}
 
Example 2
Source File: AnalyzingSuggester.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private boolean sameSurfaceForm(BytesRef key, BytesRef output2) {
  if (hasPayloads) {
    // output2 has at least PAYLOAD_SEP byte:
    if (key.length >= output2.length) {
      return false;
    }
    for(int i=0;i<key.length;i++) {
      if (key.bytes[key.offset+i] != output2.bytes[output2.offset+i]) {
        return false;
      }
    }
    return output2.bytes[output2.offset + key.length] == PAYLOAD_SEP;
  } else {
    return key.bytesEquals(output2);
  }
}
 
Example 3
Source File: IndexFieldMapper.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private boolean isSameIndex(Object value, String indexName) {
    if (value instanceof BytesRef) {
        BytesRef indexNameRef = new BytesRef(indexName);
        return (indexNameRef.bytesEquals((BytesRef) value));
    } else {
        return indexName.equals(value.toString());
    }
}
 
Example 4
Source File: BytesRefHash.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Get the id associated with <code>key</code>
 */
public long find(BytesRef key, int code) {
    final long slot = slot(rehash(code), mask);
    for (long index = slot; ; index = nextSlot(index, mask)) {
        final long id = id(index);
        if (id == -1L || key.bytesEquals(get(id, spare))) {
            return id;
        }
    }
}
 
Example 5
Source File: DirectSpellChecker.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Provide spelling corrections based on several parameters.
 *
 * @param term The term to suggest spelling corrections for
 * @param numSug The maximum number of spelling corrections
 * @param ir The index reader to fetch the candidate spelling corrections from
 * @param docfreq The minimum document frequency a potential suggestion need to have in order to be included
 * @param editDistance The maximum edit distance candidates are allowed to have
 * @param accuracy The minimum accuracy a suggested spelling correction needs to have in order to be included
 * @param spare a chars scratch
 * @return a collection of spelling corrections sorted by <code>ScoreTerm</code>'s natural order.
 * @throws IOException If I/O related errors occur
 */
protected Collection<ScoreTerm> suggestSimilar(Term term, int numSug, IndexReader ir, int docfreq, int editDistance,
                                               float accuracy, final CharsRefBuilder spare) throws IOException {

  Terms terms = MultiTerms.getTerms(ir, term.field());
  if (terms == null) {
    return Collections.emptyList();
  }
  FuzzyTermsEnum e = new FuzzyTermsEnum(terms, term, editDistance, Math.max(minPrefix, editDistance - 1), true);
  final PriorityQueue<ScoreTerm> stQueue = new PriorityQueue<>();
  
  BytesRef queryTerm = new BytesRef(term.text());
  BytesRef candidateTerm;
  ScoreTerm st = new ScoreTerm();
  while ((candidateTerm = e.next()) != null) {
    // For FuzzyQuery, boost is the score:
    float score = e.getBoost();
    // ignore uncompetitive hits
    if (stQueue.size() >= numSug && score <= stQueue.peek().boost) {
      continue;
    }
    
    // ignore exact match of the same term
    if (queryTerm.bytesEquals(candidateTerm)) {
      continue;
    }
    
    int df = e.docFreq();
    
    // check docFreq if required
    if (df <= docfreq) {
      continue;
    }
    
    final String termAsString;
    if (distance == INTERNAL_LEVENSHTEIN) {
      // delay creating strings until the end
      termAsString = null;
    } else {
      spare.copyUTF8Bytes(candidateTerm);
      termAsString = spare.toString();
      score = distance.getDistance(term.text(), termAsString);
    }
    
    if (score < accuracy) {
      continue;
    }
    
    // add new entry in PQ
    st.term = BytesRef.deepCopyOf(candidateTerm);
    st.boost = score;
    st.docfreq = df;
    st.termAsString = termAsString;
    st.score = score;
    stQueue.offer(st);
    // possibly drop entries from queue
    st = (stQueue.size() > numSug) ? stQueue.poll() : new ScoreTerm();
    e.setMaxNonCompetitiveBoost((stQueue.size() >= numSug) ? stQueue.peek().boost : Float.NEGATIVE_INFINITY);
  }
    
  return stQueue;
}