Java Code Examples for org.apache.lucene.util.AttributeImpl

The following examples show how to use org.apache.lucene.util.AttributeImpl. 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: IndexTimeSynonymTest.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public TokenStreamComponents createComponents(String fieldName) {
  Tokenizer ts = new Tokenizer(Token.TOKEN_ATTRIBUTE_FACTORY) {
    final AttributeImpl reusableToken = (AttributeImpl) addAttribute(CharTermAttribute.class);
    int p = 0;
    
    @Override
    public boolean incrementToken() {
      if( p >= tokens.length ) return false;
      clearAttributes();
      tokens[p++].copyTo(reusableToken);
      return true;
    }

    @Override
    public void reset() throws IOException {
      super.reset();
      this.p = 0;
    }
  };
  return new TokenStreamComponents(ts);
}
 
Example 2
Source Project: lucene-solr   Source File: PackedTokenAttributeImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void copyTo(AttributeImpl target) {
  if (target instanceof PackedTokenAttributeImpl) {
    final PackedTokenAttributeImpl to = (PackedTokenAttributeImpl) target;
    to.copyBuffer(buffer(), 0, length());
    to.positionIncrement = positionIncrement;
    to.positionLength = positionLength;
    to.startOffset = startOffset;
    to.endOffset = endOffset;
    to.type = type;
    to.termFrequency = termFrequency;
  } else {
    super.copyTo(target);
    ((OffsetAttribute) target).setOffset(startOffset, endOffset);
    ((PositionIncrementAttribute) target).setPositionIncrement(positionIncrement);
    ((PositionLengthAttribute) target).setPositionLength(positionLength);
    ((TypeAttribute) target).setType(type);
    ((TermFrequencyAttribute) target).setTermFrequency(termFrequency);
  }
}
 
Example 3
Source Project: lucene4ir   Source File: Retriever.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns the list of tokens extracted from the query string using the specified analyzer.
 *
 * @param field document field.
 *
 * @param queryTerms query string.
 *
 * @param distinctTokens if true, return the distinct tokens in the query string.
 *
 * @return the list of tokens extracted from the given query.
 *
 * @throws IOException
 */
List<String> getTokens(String field, String queryTerms, boolean distinctTokens) throws IOException {

    List<String> tokens = new ArrayList<String>();

    StringReader topicTitleReader = new StringReader(queryTerms);

    Set<String> seenTokens = new TreeSet<String>();

    TokenStream tok;
    tok = analyzer.tokenStream(field, topicTitleReader);
    tok.reset();
    while (tok.incrementToken()) {
        Iterator<AttributeImpl> atts = tok.getAttributeImplsIterator();
        AttributeImpl token = atts.next();
        String text = "" + token;
        if (seenTokens.contains(text) && distinctTokens) {
            continue;
        }
        seenTokens.add(text);
        tokens.add(text);
    }
    tok.close();

    return tokens;
}
 
Example 4
Source Project: Elasticsearch   Source File: NumericTokenizer.java    License: Apache License 2.0 5 votes vote down vote up
/** Make this tokenizer get attributes from the delegate token stream. */
private static final AttributeFactory delegatingAttributeFactory(final AttributeSource source) {
    return new AttributeFactory() {
        @Override
        public AttributeImpl createAttributeInstance(Class<? extends Attribute> attClass) {
            return (AttributeImpl) source.addAttribute(attClass);
        }
    };
}
 
Example 5
Source Project: lucene-solr   Source File: AnalysisImpl.java    License: Apache License 2.0 5 votes vote down vote up
private List<TokenAttribute> copyAttributes(TokenStream tokenStream, CharTermAttribute charAtt) {
  List<TokenAttribute> attributes = new ArrayList<>();
  Iterator<AttributeImpl> itr = tokenStream.getAttributeImplsIterator();
  while(itr.hasNext()) {
    AttributeImpl att = itr.next();
    Map<String, String> attValues = new LinkedHashMap<>();
    att.reflectWith((attClass, key, value) -> {
      if (value != null)
        attValues.put(key, value.toString());
    });
    attributes.add(new TokenAttribute(att.getClass().getSimpleName(), attValues));
  }
  return attributes;
}
 
Example 6
@Override
public void copyTo(AttributeImpl target) {
  List<StringBuilder> cloned = null;
  if (tags != null) {
    cloned = new ArrayList<>(tags.size());
    for (StringBuilder b : tags) {
      cloned.add(new StringBuilder(b));
    }
  }
  ((MorphosyntacticTagsAttribute) target).setTags(cloned);
}
 
Example 7
Source Project: lucene-solr   Source File: UniqueFieldAttributeImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void copyTo(AttributeImpl target) {

  if (!(target instanceof UniqueFieldAttributeImpl)) {
    throw new IllegalArgumentException(
        "cannot copy the values from attribute UniqueFieldAttribute to an instance of "
            + target.getClass().getName());
  }

  UniqueFieldAttributeImpl uniqueFieldAttr = (UniqueFieldAttributeImpl) target;
  uniqueFieldAttr.uniqueField = uniqueField.toString();

}
 
Example 8
Source Project: lucene-solr   Source File: Test2BTerms.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public AttributeImpl createAttributeInstance(Class<? extends Attribute> attClass) {
  if (attClass == TermToBytesRefAttribute.class)
    return new MyTermAttributeImpl();
  if (CharTermAttribute.class.isAssignableFrom(attClass))
    throw new IllegalArgumentException("no");
  return delegate.createAttributeInstance(attClass);
}
 
Example 9
Source Project: lucene-solr   Source File: TestCharTermAttributeImpl.java    License: Apache License 2.0 5 votes vote down vote up
public static <T extends AttributeImpl> T assertCloneIsEqual(T att) {
  @SuppressWarnings("unchecked")
  T clone = (T) att.clone();
  assertEquals("Clone must be equal", att, clone);
  assertEquals("Clone's hashcode must be equal", att.hashCode(), clone.hashCode());
  return clone;
}
 
Example 10
Source Project: lucene-solr   Source File: TestCharTermAttributeImpl.java    License: Apache License 2.0 5 votes vote down vote up
public static <T extends AttributeImpl> T assertCopyIsEqual(T att) throws Exception {
  @SuppressWarnings("unchecked")
  T copy = (T) att.getClass().getConstructor().newInstance();
  att.copyTo(copy);
  assertEquals("Copied instance must be equal", att, copy);
  assertEquals("Copied instance's hashcode must be equal", att.hashCode(), copy.hashCode());
  return copy;
}
 
Example 11
Source Project: lucene-solr   Source File: TestBytesRefAttImpl.java    License: Apache License 2.0 5 votes vote down vote up
public static <T extends AttributeImpl> T assertCopyIsEqual(T att) throws Exception {
  @SuppressWarnings("unchecked")
  T copy = (T) att.getClass().getConstructor().newInstance();
  att.copyTo(copy);
  assertEquals("Copied instance must be equal", att, copy);
  assertEquals("Copied instance's hashcode must be equal", att.hashCode(), copy.hashCode());
  return copy;
}
 
Example 12
Source Project: lucene-solr   Source File: AnalysisRequestHandlerBase.java    License: Apache License 2.0 5 votes vote down vote up
protected void addAttributes(AttributeSource attributeSource) {
  // note: ideally we wouldn't call addAttributeImpl which is marked internal. But nonetheless it's possible
  //  this method is used by some custom attributes, especially since Solr doesn't provide a way to customize the
  //  AttributeFactory which is the recommended way to choose which classes implement which attributes.
  Iterator<AttributeImpl> atts = attributeSource.getAttributeImplsIterator();
  while (atts.hasNext()) {
    addAttributeImpl(atts.next()); // adds both impl & interfaces
  }
}
 
Example 13
Source Project: lucene-solr   Source File: TestNumericTokenStream.java    License: Apache License 2.0 5 votes vote down vote up
public static <T extends AttributeImpl> T assertCloneIsEqual(T att) {
  @SuppressWarnings("unchecked")
  T clone = (T) att.clone();
  assertEquals("Clone must be equal", att, clone);
  assertEquals("Clone's hashcode must be equal", att.hashCode(), clone.hashCode());
  return clone;
}
 
Example 14
Source Project: lucene-solr   Source File: TestNumericTokenStream.java    License: Apache License 2.0 5 votes vote down vote up
public static <T extends AttributeImpl> T assertCopyIsEqual(T att) throws Exception {
  @SuppressWarnings("unchecked")
  T copy = (T) att.getClass().getConstructor().newInstance();
  att.copyTo(copy);
  assertEquals("Copied instance must be equal", att, copy);
  assertEquals("Copied instance's hashcode must be equal", att.hashCode(), copy.hashCode());
  return copy;
}
 
Example 15
private CharTermAttribute getTermAttribute() {
    Iterator<AttributeImpl> attrIt = getAttributeImplsIterator();
    while (attrIt != null && attrIt.hasNext()) {
        AttributeImpl attrImp = attrIt.next();
        if (attrImp instanceof CharTermAttribute) {
            return (CharTermAttribute) attrImp;
        }
    }
    return null;
}
 
Example 16
private OffsetAttribute getOffsetAttribute() {
    Iterator<AttributeImpl> attrIt = getAttributeImplsIterator();
    while (attrIt != null && attrIt.hasNext()) {
        AttributeImpl attrImp = attrIt.next();
        if (attrImp instanceof OffsetAttribute) {
            return (OffsetAttribute) attrImp;
        }
    }
    return null;
}
 
Example 17
private PositionIncrementAttribute getPositionIncrementAttribute() {
    Iterator<AttributeImpl> attrIt = getAttributeImplsIterator();
    while (attrIt != null && attrIt.hasNext()) {
        AttributeImpl attrImp = attrIt.next();
        if (attrImp instanceof PositionIncrementAttribute) {
            return (PositionIncrementAttribute) attrImp;
        }
    }
    return null;
}
 
Example 18
Source Project: datawave   Source File: TruncateAttributeImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void copyTo(AttributeImpl arg0) {
    TruncateAttribute a = (TruncateAttribute) arg0;
    a.setTruncated(this.isTruncated());
}
 
Example 19
Source Project: datawave   Source File: TokenizedFieldsAttributeImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void copyTo(AttributeImpl target) {
    throw new UnsupportedOperationException();
}
 
Example 20
Source Project: lucene-solr   Source File: AnalysisImpl.java    License: Apache License 2.0 4 votes vote down vote up
void addAttributes(AttributeSource attributeSource) {
  Iterator<AttributeImpl> atts = attributeSource.getAttributeImplsIterator();
  while (atts.hasNext()) {
    addAttributeImpl(atts.next()); // adds both impl & interfaces
  }
}
 
Example 21
Source Project: lucene-solr   Source File: ReadingAttributeImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void copyTo(AttributeImpl target) {
  ReadingAttribute t = (ReadingAttribute) target;
  t.setToken(token);
}
 
Example 22
Source Project: lucene-solr   Source File: PartOfSpeechAttributeImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void copyTo(AttributeImpl target) {
  PartOfSpeechAttribute t = (PartOfSpeechAttribute) target;
  t.setToken(token);
}
 
Example 23
Source Project: lucene-solr   Source File: BaseFormAttributeImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void copyTo(AttributeImpl target) {
  BaseFormAttribute t = (BaseFormAttribute) target;
  t.setToken(token);
}
 
Example 24
Source Project: lucene-solr   Source File: InflectionAttributeImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void copyTo(AttributeImpl target) {
  InflectionAttribute t = (InflectionAttribute) target;
  t.setToken(token);
}
 
Example 25
Source Project: lucene-solr   Source File: ScriptAttributeImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void copyTo(AttributeImpl target) {
  ScriptAttribute t = (ScriptAttribute) target;
  t.setCode(code);
}
 
Example 26
Source Project: lucene-solr   Source File: ReadingAttributeImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void copyTo(AttributeImpl target) {
  ReadingAttribute t = (ReadingAttribute) target;
  t.setToken(token);
}
 
Example 27
Source Project: lucene-solr   Source File: PartOfSpeechAttributeImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void copyTo(AttributeImpl target) {
  PartOfSpeechAttribute t = (PartOfSpeechAttribute) target;
  t.setToken(token);
}
 
Example 28
Source Project: lucene-solr   Source File: ConcatenateGraphFilter.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void copyTo(AttributeImpl target) {
  BytesRefBuilderTermAttributeImpl other = (BytesRefBuilderTermAttributeImpl) target;
  other.bytes.copyBytes(bytes);
}
 
Example 29
Source Project: lucene-solr   Source File: ConcatenateGraphFilter.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public AttributeImpl clone() {
  BytesRefBuilderTermAttributeImpl other = new BytesRefBuilderTermAttributeImpl();
  copyTo(other);
  return other;
}
 
Example 30
Source Project: lucene-solr   Source File: TestCompoundWordTokenFilter.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void copyTo(AttributeImpl target) {
  MockRetainAttribute t = (MockRetainAttribute) target;
  t.setRetain(retain);
}