Example usage for org.apache.lucene.codecs FieldsConsumer close

List of usage examples for org.apache.lucene.codecs FieldsConsumer close

Introduction

In this page you can find the example usage for org.apache.lucene.codecs FieldsConsumer close.

Prototype

@Override
    public abstract void close() throws IOException;

Source Link

Usage

From source file:com.rocana.lucene.codec.v1.RocanaBasePostingsFormatTestCase.java

License:Apache License

@Override
public void testInvertedWrite() throws Exception {
    Directory dir = newDirectory();//w w w . ja  v a2  s.  c om
    MockAnalyzer analyzer = new MockAnalyzer(random());
    analyzer.setMaxTokenLength(TestUtil.nextInt(random(), 1, IndexWriter.MAX_TERM_LENGTH));
    IndexWriterConfig iwc = newIndexWriterConfig(analyzer);

    // Must be concurrent because thread(s) can be merging
    // while up to one thread flushes, and each of those
    // threads iterates over the map while the flushing
    // thread might be adding to it:
    final Map<String, TermFreqs> termFreqs = new ConcurrentHashMap<>();

    final AtomicLong sumDocFreq = new AtomicLong();
    final AtomicLong sumTotalTermFreq = new AtomicLong();

    // TODO: would be better to use / delegate to the current
    // Codec returned by getCodec()

    iwc.setCodec(new AssertingCodec() {
        @Override
        public PostingsFormat getPostingsFormatForField(String field) {

            PostingsFormat p = getCodec().postingsFormat();
            if (p instanceof PerFieldPostingsFormat) {
                p = ((PerFieldPostingsFormat) p).getPostingsFormatForField(field);
            }
            if (p instanceof RocanaPerFieldPostingsFormat) {
                p = ((RocanaPerFieldPostingsFormat) p).getPostingsFormatForField(field);
            }
            final PostingsFormat defaultPostingsFormat = p;

            final Thread mainThread = Thread.currentThread();

            if (field.equals("body")) {

                // A PF that counts up some stats and then in
                // the end we verify the stats match what the
                // final IndexReader says, just to exercise the
                // new freedom of iterating the postings more
                // than once at flush/merge:

                return new PostingsFormat(defaultPostingsFormat.getName()) {

                    @Override
                    public FieldsConsumer fieldsConsumer(final SegmentWriteState state) throws IOException {

                        final FieldsConsumer fieldsConsumer = defaultPostingsFormat.fieldsConsumer(state);

                        return new FieldsConsumer() {
                            @Override
                            public void write(Fields fields) throws IOException {
                                fieldsConsumer.write(fields);

                                boolean isMerge = state.context.context == IOContext.Context.MERGE;

                                // We only use one thread for flushing
                                // in this test:
                                assert isMerge || Thread.currentThread() == mainThread;

                                // We iterate the provided TermsEnum
                                // twice, so we excercise this new freedom
                                // with the inverted API; if
                                // addOnSecondPass is true, we add up
                                // term stats on the 2nd iteration:
                                boolean addOnSecondPass = random().nextBoolean();

                                //System.out.println("write isMerge=" + isMerge + " 2ndPass=" + addOnSecondPass);

                                // Gather our own stats:
                                Terms terms = fields.terms("body");
                                assert terms != null;

                                TermsEnum termsEnum = terms.iterator();
                                PostingsEnum docs = null;
                                while (termsEnum.next() != null) {
                                    BytesRef term = termsEnum.term();
                                    // TODO: also sometimes ask for payloads/offsets?
                                    boolean noPositions = random().nextBoolean();
                                    if (noPositions) {
                                        docs = termsEnum.postings(docs, PostingsEnum.FREQS);
                                    } else {
                                        docs = termsEnum.postings(null, PostingsEnum.POSITIONS);
                                    }
                                    int docFreq = 0;
                                    long totalTermFreq = 0;
                                    while (docs.nextDoc() != PostingsEnum.NO_MORE_DOCS) {
                                        docFreq++;
                                        totalTermFreq += docs.freq();
                                        int limit = TestUtil.nextInt(random(), 1, docs.freq());
                                        if (!noPositions) {
                                            for (int i = 0; i < limit; i++) {
                                                docs.nextPosition();
                                            }
                                        }
                                    }

                                    String termString = term.utf8ToString();

                                    // During merge we should only see terms
                                    // we had already seen during a
                                    // previous flush:
                                    assertTrue(isMerge == false || termFreqs.containsKey(termString));

                                    if (isMerge == false) {
                                        if (addOnSecondPass == false) {
                                            TermFreqs tf = termFreqs.get(termString);
                                            if (tf == null) {
                                                tf = new TermFreqs();
                                                termFreqs.put(termString, tf);
                                            }
                                            tf.docFreq += docFreq;
                                            tf.totalTermFreq += totalTermFreq;
                                            sumDocFreq.addAndGet(docFreq);
                                            sumTotalTermFreq.addAndGet(totalTermFreq);
                                        } else if (termFreqs.containsKey(termString) == false) {
                                            // Add placeholder (2nd pass will
                                            // set its counts):
                                            termFreqs.put(termString, new TermFreqs());
                                        }
                                    }
                                }

                                // Also test seeking the TermsEnum:
                                for (String term : termFreqs.keySet()) {
                                    if (termsEnum.seekExact(new BytesRef(term))) {
                                        // TODO: also sometimes ask for payloads/offsets?
                                        boolean noPositions = random().nextBoolean();
                                        if (noPositions) {
                                            docs = termsEnum.postings(docs, PostingsEnum.FREQS);
                                        } else {
                                            docs = termsEnum.postings(null, PostingsEnum.POSITIONS);
                                        }

                                        int docFreq = 0;
                                        long totalTermFreq = 0;
                                        while (docs.nextDoc() != PostingsEnum.NO_MORE_DOCS) {
                                            docFreq++;
                                            totalTermFreq += docs.freq();
                                            int limit = TestUtil.nextInt(random(), 1, docs.freq());
                                            if (!noPositions) {
                                                for (int i = 0; i < limit; i++) {
                                                    docs.nextPosition();
                                                }
                                            }
                                        }

                                        if (isMerge == false && addOnSecondPass) {
                                            TermFreqs tf = termFreqs.get(term);
                                            assert tf != null;
                                            tf.docFreq += docFreq;
                                            tf.totalTermFreq += totalTermFreq;
                                            sumDocFreq.addAndGet(docFreq);
                                            sumTotalTermFreq.addAndGet(totalTermFreq);
                                        }

                                        //System.out.println("  term=" + term + " docFreq=" + docFreq + " ttDF=" + termToDocFreq.get(term));
                                        assertTrue(docFreq <= termFreqs.get(term).docFreq);
                                        assertTrue(totalTermFreq <= termFreqs.get(term).totalTermFreq);
                                    }
                                }

                                // Also test seekCeil
                                for (int iter = 0; iter < 10; iter++) {
                                    BytesRef term = new BytesRef(
                                            TestUtil.randomRealisticUnicodeString(random()));
                                    SeekStatus status = termsEnum.seekCeil(term);
                                    if (status == SeekStatus.NOT_FOUND) {
                                        assertTrue(term.compareTo(termsEnum.term()) < 0);
                                    }
                                }
                            }

                            @Override
                            public void close() throws IOException {
                                fieldsConsumer.close();
                            }
                        };
                    }

                    @Override
                    public FieldsProducer fieldsProducer(SegmentReadState state) throws IOException {
                        return defaultPostingsFormat.fieldsProducer(state);
                    }
                };
            } else {
                return defaultPostingsFormat;
            }
        }
    });

    RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc);

    LineFileDocs docs = new LineFileDocs(random());
    int bytesToIndex = atLeast(100) * 1024;
    int bytesIndexed = 0;
    while (bytesIndexed < bytesToIndex) {
        Document doc = docs.nextDoc();
        w.addDocument(doc);
        bytesIndexed += RamUsageTester.sizeOf(doc);
    }

    IndexReader r = w.getReader();
    w.close();

    Terms terms = MultiFields.getTerms(r, "body");
    assertEquals(sumDocFreq.get(), terms.getSumDocFreq());
    assertEquals(sumTotalTermFreq.get(), terms.getSumTotalTermFreq());

    TermsEnum termsEnum = terms.iterator();
    long termCount = 0;
    boolean supportsOrds = true;
    while (termsEnum.next() != null) {
        BytesRef term = termsEnum.term();
        assertEquals(termFreqs.get(term.utf8ToString()).docFreq, termsEnum.docFreq());
        assertEquals(termFreqs.get(term.utf8ToString()).totalTermFreq, termsEnum.totalTermFreq());
        if (supportsOrds) {
            long ord;
            try {
                ord = termsEnum.ord();
            } catch (UnsupportedOperationException uoe) {
                supportsOrds = false;
                ord = -1;
            }
            if (ord != -1) {
                assertEquals(termCount, ord);
            }
        }
        termCount++;
    }
    assertEquals(termFreqs.size(), termCount);

    r.close();
    dir.close();
}

From source file:org.elasticsearch.search.suggest.completion.CompletionPostingsFormatTest.java

License:Apache License

@Test
public void testNoDocs() throws IOException {
    AnalyzingCompletionLookupProvider provider = new AnalyzingCompletionLookupProvider(true, false, true, true);
    RAMDirectory dir = new RAMDirectory();
    IndexOutput output = dir.createOutput("foo.txt", IOContext.DEFAULT);
    FieldsConsumer consumer = provider.consumer(output);
    FieldInfo fieldInfo = new FieldInfo("foo", true, 1, false, true, true,
            IndexOptions.DOCS_AND_FREQS_AND_POSITIONS, DocValuesType.SORTED, DocValuesType.BINARY,
            new HashMap<String, String>());
    TermsConsumer addField = consumer.addField(fieldInfo);
    addField.finish(0, 0, 0);/* w w  w  . j  av  a  2  s  . co m*/
    consumer.close();
    output.close();

    IndexInput input = dir.openInput("foo.txt", IOContext.DEFAULT);
    LookupFactory load = provider.load(input);
    PostingsFormatProvider format = new PreBuiltPostingsFormatProvider(new Elasticsearch090PostingsFormat());
    NamedAnalyzer analyzer = new NamedAnalyzer("foo", new StandardAnalyzer(TEST_VERSION_CURRENT));
    assertNull(load.getLookup(
            new CompletionFieldMapper(new Names("foo"), analyzer, analyzer, format, null, true, true, true,
                    Integer.MAX_VALUE, AbstractFieldMapper.MultiFields.empty(), null),
            new CompletionSuggestionContext(null)));
    dir.close();
}

From source file:org.elasticsearch.search.suggest.completion.CompletionPostingsFormatTest.java

License:Apache License

private void writeData(Directory dir, Completion090PostingsFormat.CompletionLookupProvider provider)
        throws IOException {
    IndexOutput output = dir.createOutput("foo.txt", IOContext.DEFAULT);
    FieldsConsumer consumer = provider.consumer(output);
    FieldInfo fieldInfo = new FieldInfo("foo", true, 1, false, true, true,
            IndexOptions.DOCS_AND_FREQS_AND_POSITIONS, DocValuesType.SORTED, DocValuesType.BINARY,
            new HashMap<String, String>());
    TermsConsumer addField = consumer.addField(fieldInfo);

    PostingsConsumer postingsConsumer = addField.startTerm(new BytesRef("foofightersgenerator"));
    postingsConsumer.startDoc(0, 1);//  www .j a v  a  2  s .c  o m
    postingsConsumer.addPosition(256 - 2,
            provider.buildPayload(new BytesRef("Generator - Foo Fighters"), 9, new BytesRef("id:10")), 0, 1);
    postingsConsumer.finishDoc();
    addField.finishTerm(new BytesRef("foofightersgenerator"), new TermStats(1, 1));
    addField.startTerm(new BytesRef("generator"));
    postingsConsumer.startDoc(0, 1);
    postingsConsumer.addPosition(256 - 1,
            provider.buildPayload(new BytesRef("Generator - Foo Fighters"), 9, new BytesRef("id:10")), 0, 1);
    postingsConsumer.finishDoc();
    addField.finishTerm(new BytesRef("generator"), new TermStats(1, 1));
    addField.finish(1, 1, 1);
    consumer.close();
    output.close();

}

From source file:org.elasticsearch.search.suggest.completion.CompletionPostingsFormatTests.java

License:Apache License

@Test
public void testNoDocs() throws IOException {
    AnalyzingCompletionLookupProvider provider = new AnalyzingCompletionLookupProvider(true, false, true, true);
    RAMDirectory dir = new RAMDirectory();
    IndexOutput output = dir.createOutput("foo.txt", IOContext.DEFAULT);
    FieldsConsumer consumer = provider.consumer(output);
    consumer.write(new Fields() {
        @Override//from  w w  w . j  a  v  a 2 s.  c  om
        public Iterator<String> iterator() {
            return Arrays.asList("foo").iterator();
        }

        @Override
        public Terms terms(String field) throws IOException {
            return null;
        }

        @Override
        public int size() {
            return 1;
        }
    });
    consumer.close();
    output.close();

    IndexInput input = dir.openInput("foo.txt", IOContext.DEFAULT);
    LookupFactory load = provider.load(input);
    CompletionFieldMapper.CompletionFieldType fieldType = FIELD_TYPE.clone();
    fieldType.setProvider(provider);
    assertNull(load.getLookup(fieldType, new CompletionSuggestionContext(null)));
    dir.close();
}

From source file:org.elasticsearch.search.suggest.completion.CompletionPostingsFormatTests.java

License:Apache License

private void writeData(Directory dir, Completion090PostingsFormat.CompletionLookupProvider provider)
        throws IOException {
    IndexOutput output = dir.createOutput("foo.txt", IOContext.DEFAULT);
    FieldsConsumer consumer = provider.consumer(output);
    final List<TermPosAndPayload> terms = new ArrayList<>();
    terms.add(new TermPosAndPayload("foofightersgenerator", 256 - 2,
            provider.buildPayload(new BytesRef("Generator - Foo Fighters"), 9, new BytesRef("id:10"))));
    terms.add(new TermPosAndPayload("generator", 256 - 1,
            provider.buildPayload(new BytesRef("Generator - Foo Fighters"), 9, new BytesRef("id:10"))));
    Fields fields = new Fields() {
        @Override/*w w  w . j a  v a 2 s . com*/
        public Iterator<String> iterator() {
            return Arrays.asList("foo").iterator();
        }

        @Override
        public Terms terms(String field) throws IOException {
            if (field.equals("foo")) {
                return new Terms() {
                    @Override
                    public TermsEnum iterator() throws IOException {
                        final Iterator<TermPosAndPayload> iterator = terms.iterator();
                        return new TermsEnum() {
                            private TermPosAndPayload current = null;

                            @Override
                            public SeekStatus seekCeil(BytesRef text) throws IOException {
                                throw new UnsupportedOperationException();
                            }

                            @Override
                            public void seekExact(long ord) throws IOException {
                                throw new UnsupportedOperationException();
                            }

                            @Override
                            public BytesRef term() throws IOException {
                                return current == null ? null : current.term;
                            }

                            @Override
                            public long ord() throws IOException {
                                throw new UnsupportedOperationException();
                            }

                            @Override
                            public int docFreq() throws IOException {
                                return current == null ? 0 : 1;
                            }

                            @Override
                            public long totalTermFreq() throws IOException {
                                throw new UnsupportedOperationException();
                            }

                            @Override
                            public PostingsEnum postings(PostingsEnum reuse, int flags) throws IOException {
                                final TermPosAndPayload data = current;
                                return new PostingsEnum() {
                                    boolean done = false;

                                    @Override
                                    public int nextPosition() throws IOException {
                                        return data.pos;
                                    }

                                    @Override
                                    public int startOffset() throws IOException {
                                        return 0;
                                    }

                                    @Override
                                    public int endOffset() throws IOException {
                                        return 0;
                                    }

                                    @Override
                                    public BytesRef getPayload() throws IOException {
                                        return data.payload;
                                    }

                                    @Override
                                    public int freq() throws IOException {
                                        return 1;
                                    }

                                    @Override
                                    public int docID() {
                                        if (done) {
                                            return NO_MORE_DOCS;
                                        }
                                        return 0;
                                    }

                                    @Override
                                    public int nextDoc() throws IOException {
                                        if (done) {
                                            return NO_MORE_DOCS;
                                        }
                                        done = true;
                                        return 0;
                                    }

                                    @Override
                                    public int advance(int target) throws IOException {
                                        if (done) {
                                            return NO_MORE_DOCS;
                                        }
                                        done = true;
                                        return 0;
                                    }

                                    @Override
                                    public long cost() {
                                        return 0;
                                    }
                                };
                            }

                            @Override
                            public BytesRef next() throws IOException {
                                if (iterator.hasNext()) {
                                    current = iterator.next();
                                    return current.term;
                                }
                                current = null;
                                return null;
                            }
                        };
                    }

                    @Override
                    public long size() throws IOException {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public long getSumTotalTermFreq() throws IOException {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public long getSumDocFreq() throws IOException {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public int getDocCount() throws IOException {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public boolean hasFreqs() {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public boolean hasOffsets() {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public boolean hasPositions() {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public boolean hasPayloads() {
                        throw new UnsupportedOperationException();
                    }
                };
            }
            return null;
        }

        @Override
        public int size() {
            return 0;
        }
    };
    consumer.write(fields);
    consumer.close();
    output.close();

}

From source file:org.elasticsearch.search.suggest.completion.old.CompletionPostingsFormatTest.java

License:Apache License

@Test
public void testNoDocs() throws IOException {
    AnalyzingCompletionLookupProvider provider = new AnalyzingCompletionLookupProvider(true, false, true, true);
    RAMDirectory dir = new RAMDirectory();
    IndexOutput output = dir.createOutput("foo.txt", IOContext.DEFAULT);
    FieldsConsumer consumer = provider.consumer(output);
    consumer.write(new Fields() {
        @Override/*  w  w w.  j a  va  2  s .  c  o m*/
        public Iterator<String> iterator() {
            return Arrays.asList("foo").iterator();
        }

        @Override
        public Terms terms(String field) throws IOException {
            return null;
        }

        @Override
        public int size() {
            return 1;
        }
    });
    consumer.close();
    output.close();

    IndexInput input = dir.openInput("foo.txt", IOContext.DEFAULT);
    LookupFactory load = provider.load(input);
    OldCompletionFieldMapper.CompletionFieldType fieldType = FIELD_TYPE.clone();
    fieldType.setProvider(provider);
    assertNull(load.getLookup(fieldType, new CompletionSuggestionContext(null)));
    dir.close();
}

From source file:org.elasticsearch.search.suggest.completion.old.CompletionPostingsFormatTest.java

License:Apache License

private void writeData(Directory dir, Completion090PostingsFormat.CompletionLookupProvider provider)
        throws IOException {
    IndexOutput output = dir.createOutput("foo.txt", IOContext.DEFAULT);
    FieldsConsumer consumer = provider.consumer(output);
    final List<TermPosAndPayload> terms = new ArrayList<>();
    terms.add(new TermPosAndPayload("foofightersgenerator", 256 - 2,
            provider.buildPayload(new BytesRef("Generator - Foo Fighters"), 9, new BytesRef("id:10"))));
    terms.add(new TermPosAndPayload("generator", 256 - 1,
            provider.buildPayload(new BytesRef("Generator - Foo Fighters"), 9, new BytesRef("id:10"))));
    Fields fields = new Fields() {
        @Override/*from   w w w.  j av a 2s .  co m*/
        public Iterator<String> iterator() {
            return Arrays.asList("foo").iterator();
        }

        @Override
        public Terms terms(String field) throws IOException {
            if (field.equals("foo")) {
                return new Terms() {
                    @Override
                    public TermsEnum iterator() throws IOException {
                        final Iterator<TermPosAndPayload> iterator = terms.iterator();
                        return new TermsEnum() {
                            private TermPosAndPayload current = null;

                            @Override
                            public SeekStatus seekCeil(BytesRef text) throws IOException {
                                throw new UnsupportedOperationException();
                            }

                            @Override
                            public void seekExact(long ord) throws IOException {
                                throw new UnsupportedOperationException();
                            }

                            @Override
                            public BytesRef term() throws IOException {
                                return current == null ? null : current.term;
                            }

                            @Override
                            public long ord() throws IOException {
                                throw new UnsupportedOperationException();
                            }

                            @Override
                            public int docFreq() throws IOException {
                                return current == null ? 0 : 1;
                            }

                            @Override
                            public long totalTermFreq() throws IOException {
                                throw new UnsupportedOperationException();
                            }

                            @Override
                            public PostingsEnum postings(Bits liveDocs, PostingsEnum reuse, int flags)
                                    throws IOException {
                                final TermPosAndPayload data = current;
                                return new PostingsEnum() {
                                    boolean done = false;

                                    @Override
                                    public int nextPosition() throws IOException {
                                        return current.pos;
                                    }

                                    @Override
                                    public int startOffset() throws IOException {
                                        return 0;
                                    }

                                    @Override
                                    public int endOffset() throws IOException {
                                        return 0;
                                    }

                                    @Override
                                    public BytesRef getPayload() throws IOException {
                                        return current.payload;
                                    }

                                    @Override
                                    public int freq() throws IOException {
                                        return 1;
                                    }

                                    @Override
                                    public int docID() {
                                        if (done) {
                                            return NO_MORE_DOCS;
                                        }
                                        return 0;
                                    }

                                    @Override
                                    public int nextDoc() throws IOException {
                                        if (done) {
                                            return NO_MORE_DOCS;
                                        }
                                        done = true;
                                        return 0;
                                    }

                                    @Override
                                    public int advance(int target) throws IOException {
                                        if (done) {
                                            return NO_MORE_DOCS;
                                        }
                                        done = true;
                                        return 0;
                                    }

                                    @Override
                                    public long cost() {
                                        return 0;
                                    }
                                };
                            }

                            @Override
                            public BytesRef next() throws IOException {
                                if (iterator.hasNext()) {
                                    current = iterator.next();
                                    return current.term;
                                }
                                current = null;
                                return null;
                            }
                        };
                    }

                    @Override
                    public long size() throws IOException {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public long getSumTotalTermFreq() throws IOException {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public long getSumDocFreq() throws IOException {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public int getDocCount() throws IOException {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public boolean hasFreqs() {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public boolean hasOffsets() {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public boolean hasPositions() {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public boolean hasPayloads() {
                        throw new UnsupportedOperationException();
                    }
                };
            }
            return null;
        }

        @Override
        public int size() {
            return 0;
        }
    };
    consumer.write(fields);
    consumer.close();
    output.close();

}

From source file:org.elasticsearch.search.suggest.CompletionPostingsFormatTest.java

License:Apache License

@Test
public void testCompletionPostingsFormat() throws IOException {
    AnalyzingCompletionLookupProvider provider = new AnalyzingCompletionLookupProvider(true, false, true, true);
    RAMDirectory dir = new RAMDirectory();
    IndexOutput output = dir.createOutput("foo.txt", IOContext.DEFAULT);
    FieldsConsumer consumer = provider.consumer(output);
    FieldInfo fieldInfo = new FieldInfo("foo", true, 1, false, true, true,
            IndexOptions.DOCS_AND_FREQS_AND_POSITIONS, DocValuesType.SORTED, DocValuesType.BINARY,
            new HashMap<String, String>());
    TermsConsumer addField = consumer.addField(fieldInfo);

    PostingsConsumer postingsConsumer = addField.startTerm(new BytesRef("foofightersgenerator"));
    postingsConsumer.startDoc(0, 1);/* ww  w . j a  v  a 2s.  c o  m*/
    postingsConsumer.addPosition(256 - 2,
            provider.buildPayload(new BytesRef("Generator - Foo Fighters"), 9, new BytesRef("id:10")), 0, 1);
    postingsConsumer.finishDoc();
    addField.finishTerm(new BytesRef("foofightersgenerator"), new TermStats(1, 1));
    addField.startTerm(new BytesRef("generator"));
    postingsConsumer.startDoc(0, 1);
    postingsConsumer.addPosition(256 - 1,
            provider.buildPayload(new BytesRef("Generator - Foo Fighters"), 9, new BytesRef("id:10")), 0, 1);
    postingsConsumer.finishDoc();
    addField.finishTerm(new BytesRef("generator"), new TermStats(1, 1));
    addField.finish(1, 1, 1);
    consumer.close();
    output.close();

    IndexInput input = dir.openInput("foo.txt", IOContext.DEFAULT);
    LookupFactory load = provider.load(input);
    PostingsFormatProvider format = new PreBuiltPostingsFormatProvider(new ElasticSearch090PostingsFormat());
    NamedAnalyzer analyzer = new NamedAnalyzer("foo", new StandardAnalyzer(TEST_VERSION_CURRENT));
    Lookup lookup = load.getLookup(new CompletionFieldMapper(new Names("foo"), analyzer, analyzer, format, null,
            true, true, true, Integer.MAX_VALUE), new CompletionSuggestionContext(null));
    List<LookupResult> result = lookup.lookup("ge", false, 10);
    assertThat(result.get(0).key.toString(), equalTo("Generator - Foo Fighters"));
    assertThat(result.get(0).payload.utf8ToString(), equalTo("id:10"));
    dir.close();
}

From source file:org.elasticsearch.search.suggest.CompletionPostingsFormatTest.java

License:Apache License

@Test
public void testNoDocs() throws IOException {
    AnalyzingCompletionLookupProvider provider = new AnalyzingCompletionLookupProvider(true, false, true, true);
    RAMDirectory dir = new RAMDirectory();
    IndexOutput output = dir.createOutput("foo.txt", IOContext.DEFAULT);
    FieldsConsumer consumer = provider.consumer(output);
    FieldInfo fieldInfo = new FieldInfo("foo", true, 1, false, true, true,
            IndexOptions.DOCS_AND_FREQS_AND_POSITIONS, DocValuesType.SORTED, DocValuesType.BINARY,
            new HashMap<String, String>());
    TermsConsumer addField = consumer.addField(fieldInfo);
    addField.finish(0, 0, 0);//w ww  .j  a  v  a 2s . co  m
    consumer.close();
    output.close();

    IndexInput input = dir.openInput("foo.txt", IOContext.DEFAULT);
    LookupFactory load = provider.load(input);
    PostingsFormatProvider format = new PreBuiltPostingsFormatProvider(new ElasticSearch090PostingsFormat());
    NamedAnalyzer analyzer = new NamedAnalyzer("foo", new StandardAnalyzer(TEST_VERSION_CURRENT));
    assertNull(load.getLookup(new CompletionFieldMapper(new Names("foo"), analyzer, analyzer, format, null,
            true, true, true, Integer.MAX_VALUE), new CompletionSuggestionContext(null)));
    dir.close();
}

From source file:org.elasticsearch.test.integration.search.suggest.CompletionPostingsFormatTest.java

License:Apache License

@Test
public void testCompletionPostingsFormat() throws IOException {
    AnalyzingCompletionLookupProvider provider = new AnalyzingCompletionLookupProvider(true, false, true, true);
    RAMDirectory dir = new RAMDirectory();
    IndexOutput output = dir.createOutput("foo.txt", IOContext.DEFAULT);
    FieldsConsumer consumer = provider.consumer(output);
    FieldInfo fieldInfo = new FieldInfo("foo", true, 1, false, true, true,
            IndexOptions.DOCS_AND_FREQS_AND_POSITIONS, DocValuesType.SORTED, DocValuesType.BINARY,
            new HashMap<String, String>());
    TermsConsumer addField = consumer.addField(fieldInfo);

    PostingsConsumer postingsConsumer = addField.startTerm(new BytesRef("foofightersgenerator"));
    postingsConsumer.startDoc(0, 1);/*from   w ww.j  a  va 2 s .c  o  m*/
    postingsConsumer.addPosition(256 - 2,
            provider.buildPayload(new BytesRef("Generator - Foo Fighters"), 9, new BytesRef("id:10")), 0, 1);
    postingsConsumer.finishDoc();
    addField.finishTerm(new BytesRef("foofightersgenerator"), new TermStats(1, 1));
    addField.startTerm(new BytesRef("generator"));
    postingsConsumer.startDoc(0, 1);
    postingsConsumer.addPosition(256 - 1,
            provider.buildPayload(new BytesRef("Generator - Foo Fighters"), 9, new BytesRef("id:10")), 0, 1);
    postingsConsumer.finishDoc();
    addField.finishTerm(new BytesRef("generator"), new TermStats(1, 1));
    addField.finish(1, 1, 1);
    consumer.close();
    output.close();

    IndexInput input = dir.openInput("foo.txt", IOContext.DEFAULT);
    LookupFactory load = provider.load(input);
    PostingsFormatProvider format = new PreBuiltPostingsFormatProvider(new ElasticSearch090PostingsFormat());
    NamedAnalyzer analyzer = new NamedAnalyzer("foo", new StandardAnalyzer(TEST_VERSION_CURRENT));
    Lookup lookup = load.getLookup(
            new CompletionFieldMapper(new Names("foo"), analyzer, analyzer, format, null, true, true, true),
            false);
    List<LookupResult> result = lookup.lookup("ge", false, 10);
    assertThat(result.get(0).key.toString(), equalTo("Generator - Foo Fighters"));
    assertThat(result.get(0).payload.utf8ToString(), equalTo("id:10"));
    dir.close();
}