Search in sources :

Example 1 with CannedBinaryTokenStream

use of org.apache.lucene.analysis.CannedBinaryTokenStream in project lucene-solr by apache.

the class TestTerms method testTermMinMaxRandom.

public void testTermMinMaxRandom() throws Exception {
    Directory dir = newDirectory();
    RandomIndexWriter w = new RandomIndexWriter(random(), dir);
    int numDocs = atLeast(100);
    BytesRef minTerm = null;
    BytesRef maxTerm = null;
    for (int i = 0; i < numDocs; i++) {
        Document doc = new Document();
        Field field = new TextField("field", "", Field.Store.NO);
        doc.add(field);
        //System.out.println("  doc " + i);
        CannedBinaryTokenStream.BinaryToken[] tokens = new CannedBinaryTokenStream.BinaryToken[atLeast(10)];
        for (int j = 0; j < tokens.length; j++) {
            byte[] bytes = new byte[TestUtil.nextInt(random(), 1, 20)];
            random().nextBytes(bytes);
            BytesRef tokenBytes = new BytesRef(bytes);
            //System.out.println("    token " + tokenBytes);
            if (minTerm == null || tokenBytes.compareTo(minTerm) < 0) {
                //System.out.println("      ** new min");
                minTerm = tokenBytes;
            }
            if (maxTerm == null || tokenBytes.compareTo(maxTerm) > 0) {
                //System.out.println("      ** new max");
                maxTerm = tokenBytes;
            }
            tokens[j] = new CannedBinaryTokenStream.BinaryToken(tokenBytes);
        }
        field.setTokenStream(new CannedBinaryTokenStream(tokens));
        w.addDocument(doc);
    }
    IndexReader r = w.getReader();
    Terms terms = MultiFields.getTerms(r, "field");
    assertEquals(minTerm, terms.getMin());
    assertEquals(maxTerm, terms.getMax());
    r.close();
    w.close();
    dir.close();
}
Also used : CannedBinaryTokenStream(org.apache.lucene.analysis.CannedBinaryTokenStream) Document(org.apache.lucene.document.Document) Field(org.apache.lucene.document.Field) TextField(org.apache.lucene.document.TextField) TextField(org.apache.lucene.document.TextField) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Example 2 with CannedBinaryTokenStream

use of org.apache.lucene.analysis.CannedBinaryTokenStream in project lucene-solr by apache.

the class AnalyzingSuggesterTest method test0ByteKeys.

public void test0ByteKeys() throws Exception {
    final Analyzer a = new Analyzer() {

        @Override
        protected TokenStreamComponents createComponents(String fieldName) {
            Tokenizer tokenizer = new MockTokenizer(MockTokenizer.SIMPLE, true);
            return new TokenStreamComponents(tokenizer) {

                int tokenStreamCounter = 0;

                final TokenStream[] tokenStreams = new TokenStream[] { new CannedBinaryTokenStream(new BinaryToken[] { token(new BytesRef(new byte[] { 0x0, 0x0, 0x0 })) }), new CannedBinaryTokenStream(new BinaryToken[] { token(new BytesRef(new byte[] { 0x0, 0x0 })) }), new CannedBinaryTokenStream(new BinaryToken[] { token(new BytesRef(new byte[] { 0x0, 0x0, 0x0 })) }), new CannedBinaryTokenStream(new BinaryToken[] { token(new BytesRef(new byte[] { 0x0, 0x0 })) }) };

                @Override
                public TokenStream getTokenStream() {
                    TokenStream result = tokenStreams[tokenStreamCounter];
                    tokenStreamCounter++;
                    return result;
                }

                @Override
                protected void setReader(final Reader reader) {
                }
            };
        }
    };
    Directory tempDir = getDirectory();
    AnalyzingSuggester suggester = new AnalyzingSuggester(tempDir, "suggest", a, a, 0, 256, -1, true);
    suggester.build(new InputArrayIterator(new Input[] { new Input("a a", 50), new Input("a b", 50) }));
    IOUtils.close(a, tempDir);
}
Also used : CannedBinaryTokenStream(org.apache.lucene.analysis.CannedBinaryTokenStream) CannedTokenStream(org.apache.lucene.analysis.CannedTokenStream) CannedBinaryTokenStream(org.apache.lucene.analysis.CannedBinaryTokenStream) TokenStream(org.apache.lucene.analysis.TokenStream) Reader(java.io.Reader) Analyzer(org.apache.lucene.analysis.Analyzer) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) MockTokenizer(org.apache.lucene.analysis.MockTokenizer) Input(org.apache.lucene.search.suggest.Input) InputArrayIterator(org.apache.lucene.search.suggest.InputArrayIterator) BinaryToken(org.apache.lucene.analysis.CannedBinaryTokenStream.BinaryToken) Tokenizer(org.apache.lucene.analysis.Tokenizer) MockTokenizer(org.apache.lucene.analysis.MockTokenizer) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Aggregations

CannedBinaryTokenStream (org.apache.lucene.analysis.CannedBinaryTokenStream)2 Directory (org.apache.lucene.store.Directory)2 BytesRef (org.apache.lucene.util.BytesRef)2 Reader (java.io.Reader)1 Analyzer (org.apache.lucene.analysis.Analyzer)1 BinaryToken (org.apache.lucene.analysis.CannedBinaryTokenStream.BinaryToken)1 CannedTokenStream (org.apache.lucene.analysis.CannedTokenStream)1 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)1 MockTokenizer (org.apache.lucene.analysis.MockTokenizer)1 TokenStream (org.apache.lucene.analysis.TokenStream)1 Tokenizer (org.apache.lucene.analysis.Tokenizer)1 Document (org.apache.lucene.document.Document)1 Field (org.apache.lucene.document.Field)1 TextField (org.apache.lucene.document.TextField)1 Input (org.apache.lucene.search.suggest.Input)1 InputArrayIterator (org.apache.lucene.search.suggest.InputArrayIterator)1