Example usage for org.apache.lucene.facet.sortedset SortedSetDocValuesFacetField SortedSetDocValuesFacetField

List of usage examples for org.apache.lucene.facet.sortedset SortedSetDocValuesFacetField SortedSetDocValuesFacetField

Introduction

In this page you can find the example usage for org.apache.lucene.facet.sortedset SortedSetDocValuesFacetField SortedSetDocValuesFacetField.

Prototype

public SortedSetDocValuesFacetField(String dim, String label) 

Source Link

Document

Sole constructor.

Usage

From source file:com.czw.search.lucene.example.facet.SimpleSortedSetFacetsExample.java

License:Apache License

/**
 * Build the example index./* ww w.  j av  a  2  s . c o  m*/
 */
private void index() throws IOException {
    IndexWriter indexWriter = new IndexWriter(indexDir,
            new IndexWriterConfig(new WhitespaceAnalyzer()).setOpenMode(OpenMode.CREATE));

    Document doc = new Document();
    doc.add(new SortedSetDocValuesFacetField("Author", "Bob"));
    doc.add(new SortedSetDocValuesFacetField("Publish Year", "2010"));
    indexWriter.addDocument(config.build(doc));

    doc = new Document();
    doc.add(new SortedSetDocValuesFacetField("Author", "Lisa"));
    doc.add(new SortedSetDocValuesFacetField("Publish Year", "2010"));
    indexWriter.addDocument(config.build(doc));

    doc = new Document();
    doc.add(new SortedSetDocValuesFacetField("Author", "Lisa"));
    doc.add(new SortedSetDocValuesFacetField("Publish Year", "2012"));
    indexWriter.addDocument(config.build(doc));

    doc = new Document();
    doc.add(new SortedSetDocValuesFacetField("Author", "Susan"));
    doc.add(new SortedSetDocValuesFacetField("Publish Year", "2012"));
    indexWriter.addDocument(config.build(doc));

    doc = new Document();
    doc.add(new SortedSetDocValuesFacetField("Author", "Frank"));
    doc.add(new SortedSetDocValuesFacetField("Publish Year", "1999"));
    indexWriter.addDocument(config.build(doc));

    indexWriter.close();
}

From source file:com.epam.catgenome.dao.index.FeatureIndexDao.java

License:Open Source License

private void addCommonDocumentFields(Document document, FeatureIndexEntry entry, final Long featureFileId) {
    document.add(new SortedStringField(FeatureIndexFields.FEATURE_ID.getFieldName(), entry.getFeatureId()));

    FieldType fieldType = new FieldType();
    fieldType.setOmitNorms(true);/*from  ww  w.ja va  2  s  .c om*/
    fieldType.setIndexOptions(IndexOptions.DOCS);
    fieldType.setStored(true);
    fieldType.setTokenized(false);
    fieldType.setDocValuesType(DocValuesType.SORTED);
    fieldType.freeze();
    Field field = new Field(FeatureIndexFields.CHROMOSOME_ID.getFieldName(),
            entry.getChromosome() != null ? new BytesRef(entry.getChromosome().getId().toString())
                    : new BytesRef(""),
            fieldType);
    document.add(field);
    document.add(new SortedStringField(FeatureIndexFields.CHROMOSOME_NAME.getFieldName(),
            entry.getChromosome().getName(), true));

    document.add(new SortedIntPoint(FeatureIndexFields.START_INDEX.getFieldName(), entry.getStartIndex()));
    document.add(new StoredField(FeatureIndexFields.START_INDEX.getFieldName(), entry.getStartIndex()));
    document.add(new SortedDocValuesField(FeatureIndexFields.START_INDEX.getGroupName(),
            new BytesRef(entry.getStartIndex().toString())));

    document.add(new SortedIntPoint(FeatureIndexFields.END_INDEX.getFieldName(), entry.getEndIndex()));
    document.add(new StoredField(FeatureIndexFields.END_INDEX.getFieldName(), entry.getEndIndex()));
    document.add(new SortedDocValuesField(FeatureIndexFields.END_INDEX.getGroupName(),
            new BytesRef(entry.getStartIndex().toString())));

    document.add(new StringField(FeatureIndexFields.FEATURE_TYPE.getFieldName(),
            entry.getFeatureType() != null ? entry.getFeatureType().getFileValue() : "", Field.Store.YES));
    document.add(new StringField(FeatureIndexFields.FILE_ID.getFieldName(), featureFileId.toString(),
            Field.Store.YES));

    document.add(new StringField(FeatureIndexFields.FEATURE_NAME.getFieldName(),
            entry.getFeatureName() != null ? entry.getFeatureName().toLowerCase() : "", Field.Store.YES));
    document.add(new SortedDocValuesField(FeatureIndexFields.FEATURE_NAME.getFieldName(),
            new BytesRef(entry.getFeatureName() != null ? entry.getFeatureName() : "")));

    document.add(new SortedSetDocValuesFacetField(FeatureIndexFields.CHR_ID.getFieldName(),
            entry.getChromosome().getId().toString()));

    document.add(new SortedStringField(FeatureIndexFields.UID.getFieldName(), entry.getUuid().toString()));
    document.add(new SortedSetDocValuesFacetField(FeatureIndexFields.F_UID.getFieldName(),
            entry.getUuid().toString()));
}

From source file:com.qwazr.search.bench.test.SortedSetFacet.SortedSetFacetLuceneTest.java

License:Apache License

@Override
final public void accept(final TtlLineReader lineReader, final LuceneRecord.Indexable record) {
    final BytesRef termBytesRef = new BytesRef(lineReader.subject);
    record.reset(new Term(URL, termBytesRef));
    record.document.add(new StringField(URL, termBytesRef, Field.Store.NO));
    record.document.add(new SortedSetDocValuesFacetField(PREDICATE, lineReader.predicate));
}

From source file:com.qwazr.search.field.SortedSetDocValuesFacetType.java

License:Apache License

@Override
final public void fillValue(final Object value, final FieldConsumer consumer) {
    consumer.accept(new SortedSetDocValuesFacetField(fieldName, value.toString()));
}

From source file:com.search.lucene.demo.facet.SimpleSortedSetFacetsExample.java

License:Apache License

/** Build the example index. */
private void index() throws IOException {
    IndexWriter indexWriter = new IndexWriter(indexDir,
            new IndexWriterConfig(new WhitespaceAnalyzer()).setOpenMode(OpenMode.CREATE));
    Document doc = new Document();
    doc.add(new SortedSetDocValuesFacetField("Author", "Bob"));
    doc.add(new SortedSetDocValuesFacetField("Publish Year", "2010"));
    indexWriter.addDocument(config.build(doc));

    doc = new Document();
    doc.add(new SortedSetDocValuesFacetField("Author", "Lisa"));
    doc.add(new SortedSetDocValuesFacetField("Publish Year", "2010"));
    indexWriter.addDocument(config.build(doc));

    doc = new Document();
    doc.add(new SortedSetDocValuesFacetField("Author", "Lisa"));
    doc.add(new SortedSetDocValuesFacetField("Publish Year", "2012"));
    indexWriter.addDocument(config.build(doc));

    doc = new Document();
    doc.add(new SortedSetDocValuesFacetField("Author", "Susan"));
    doc.add(new SortedSetDocValuesFacetField("Publish Year", "2012"));
    indexWriter.addDocument(config.build(doc));

    doc = new Document();
    doc.add(new SortedSetDocValuesFacetField("Author", "Frank"));
    doc.add(new SortedSetDocValuesFacetField("Publish Year", "1999"));
    indexWriter.addDocument(config.build(doc));

    indexWriter.close();/*w  ww  . j a  v a  2s .c om*/
}

From source file:com.searchcode.app.service.CodeIndexer.java

License:Open Source License

/**
 * Builds a document ready to be indexed by lucene
 *///from   w  ww  . j  ava  2  s  .  c o  m
public Document buildDocument(CodeIndexDocument codeIndexDocument) {
    Document document = new Document();
    // Path is the primary key for documents
    // needs to include repo location, project name and then filepath including file
    Field pathField = new StringField("path", codeIndexDocument.getRepoLocationRepoNameLocationFilename(),
            Field.Store.YES);
    document.add(pathField);

    if (!Singleton.getHelpers().isNullEmptyOrWhitespace(codeIndexDocument.getLanguageName())) {
        document.add(
                new SortedSetDocValuesFacetField(Values.LANGUAGENAME, codeIndexDocument.getLanguageName()));
    }
    if (!Singleton.getHelpers().isNullEmptyOrWhitespace(codeIndexDocument.getRepoName())) {
        document.add(new SortedSetDocValuesFacetField(Values.REPONAME, codeIndexDocument.getRepoName()));
    }
    if (!Singleton.getHelpers().isNullEmptyOrWhitespace(codeIndexDocument.getCodeOwner())) {
        document.add(new SortedSetDocValuesFacetField(Values.CODEOWNER, codeIndexDocument.getCodeOwner()));
    }

    this.searchcodeLib.addToSpellingCorrector(codeIndexDocument.getContents());

    StringBuilder indexContents = new StringBuilder();

    indexContents.append(this.searchcodeLib.codeCleanPipeline(codeIndexDocument.getFileName())).append(" ");
    indexContents.append(this.searchcodeLib.splitKeywords(codeIndexDocument.getFileName())).append(" ");
    indexContents.append(codeIndexDocument.getFileLocationFilename()).append(" ");
    indexContents.append(codeIndexDocument.getFileLocation());
    indexContents.append(this.searchcodeLib.splitKeywords(codeIndexDocument.getContents()));
    indexContents.append(this.searchcodeLib.codeCleanPipeline(codeIndexDocument.getContents()));
    indexContents.append(this.searchcodeLib.findInterestingKeywords(codeIndexDocument.getContents()));
    indexContents.append(this.searchcodeLib.findInterestingCharacters(codeIndexDocument.getContents()));

    document.add(
            new TextField(Values.REPONAME, codeIndexDocument.getRepoName().replace(" ", "_"), Field.Store.YES));
    document.add(new TextField(Values.FILENAME, codeIndexDocument.getFileName(), Field.Store.YES));
    document.add(new TextField(Values.FILELOCATION, codeIndexDocument.getFileLocation(), Field.Store.YES));
    document.add(new TextField(Values.FILELOCATIONFILENAME, codeIndexDocument.getFileLocationFilename(),
            Field.Store.YES));
    document.add(new TextField(Values.MD5HASH, codeIndexDocument.getMd5hash(), Field.Store.YES));
    document.add(new TextField(Values.LANGUAGENAME, codeIndexDocument.getLanguageName().replace(" ", "_"),
            Field.Store.YES));
    document.add(new IntField(Values.CODELINES, codeIndexDocument.getCodeLines(), Field.Store.YES));
    document.add(new TextField(Values.CONTENTS, indexContents.toString().toLowerCase(), Field.Store.NO));
    document.add(
            new TextField(Values.REPOLOCATION, codeIndexDocument.getRepoRemoteLocation(), Field.Store.YES));
    document.add(new TextField(Values.CODEOWNER, codeIndexDocument.getCodeOwner().replace(" ", "_"),
            Field.Store.YES));
    document.add(new TextField(Values.CODEID, codeIndexDocument.getHash(), Field.Store.YES));

    // Extra metadata in this case when it was last indexed
    document.add(new LongField(Values.MODIFIED, new Date().getTime(), Field.Store.YES));
    return document;
}

From source file:com.searchcode.app.service.CodeIndexer.java

License:Open Source License

/**
 * Given a queue of documents to index, index them by popping the queue limited to 1000 items.
 * This method must be synchronized as we have not added any logic to deal with multiple threads writing to the
 * index.//w  w w .  j  a v a 2s .c o  m
 * TODO investigate how Lucene deals with multiple writes
 */
public synchronized void indexTimeDocuments(Queue<CodeIndexDocument> codeIndexDocumentQueue)
        throws IOException {
    // Index all documents and commit at the end for performance gains
    Directory dir = FSDirectory.open(Paths.get(
            Properties.getProperties().getProperty(Values.TIMEINDEXLOCATION, Values.DEFAULTTIMEINDEXLOCATION)));
    Directory facetsdir = FSDirectory.open(Paths.get(Properties.getProperties()
            .getProperty(Values.TIMEINDEXFACETLOCATION, Values.DEFAULTTIMEINDEXFACETLOCATION)));

    Analyzer analyzer = new CodeAnalyzer();
    IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
    FacetsConfig facetsConfig;

    iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);

    IndexWriter writer = new IndexWriter(dir, iwc);
    TaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(facetsdir);

    try {
        CodeIndexDocument codeIndexDocument = codeIndexDocumentQueue.poll();
        int count = 0;

        while (codeIndexDocument != null) {
            Singleton.getLogger()
                    .info("Indexing time file " + codeIndexDocument.getRepoLocationRepoNameLocationFilename());
            this.sharedService.decrementCodeIndexLinesCount(codeIndexDocument.getCodeLines());

            Document doc = new Document();
            // Path is the primary key for documents
            // needs to include repo location, project name and then filepath including file and revision
            Field pathField = new StringField("path",
                    codeIndexDocument.getRepoLocationRepoNameLocationFilename() + ":"
                            + codeIndexDocument.getRevision(),
                    Field.Store.YES);
            doc.add(pathField);

            // Add in facets
            facetsConfig = new FacetsConfig();
            facetsConfig.setIndexFieldName(Values.LANGUAGENAME, Values.LANGUAGENAME);
            facetsConfig.setIndexFieldName(Values.REPONAME, Values.REPONAME);
            facetsConfig.setIndexFieldName(Values.CODEOWNER, Values.CODEOWNER);
            facetsConfig.setIndexFieldName(Values.DATEYEARMONTHDAY, Values.DATEYEARMONTHDAY);
            facetsConfig.setIndexFieldName(Values.DATEYEARMONTH, Values.DATEYEARMONTH);
            facetsConfig.setIndexFieldName(Values.DATEYEAR, Values.DATEYEAR);
            facetsConfig.setIndexFieldName(Values.REVISION, Values.REVISION);
            facetsConfig.setIndexFieldName(Values.DELETED, Values.DELETED);

            if (Singleton.getHelpers().isNullEmptyOrWhitespace(codeIndexDocument.getLanguageName()) == false) {
                doc.add(new SortedSetDocValuesFacetField(Values.LANGUAGENAME,
                        codeIndexDocument.getLanguageName()));
            }
            if (Singleton.getHelpers().isNullEmptyOrWhitespace(codeIndexDocument.getRepoName()) == false) {
                doc.add(new SortedSetDocValuesFacetField(Values.REPONAME, codeIndexDocument.getRepoName()));
            }
            if (Singleton.getHelpers().isNullEmptyOrWhitespace(codeIndexDocument.getCodeOwner()) == false) {
                doc.add(new SortedSetDocValuesFacetField(Values.CODEOWNER, codeIndexDocument.getCodeOwner()));
            }
            if (Singleton.getHelpers().isNullEmptyOrWhitespace(codeIndexDocument.getYearMonthDay()) == false) {
                doc.add(new SortedSetDocValuesFacetField(Values.DATEYEARMONTHDAY,
                        codeIndexDocument.getYearMonthDay()));
            }
            if (Singleton.getHelpers().isNullEmptyOrWhitespace(codeIndexDocument.getYearMonthDay()) == false) {
                doc.add(new SortedSetDocValuesFacetField(Values.DATEYEARMONTH,
                        codeIndexDocument.getYearMonthDay().substring(0, 6)));
            }
            if (Singleton.getHelpers().isNullEmptyOrWhitespace(codeIndexDocument.getYearMonthDay()) == false) {
                doc.add(new SortedSetDocValuesFacetField(Values.DATEYEAR,
                        codeIndexDocument.getYearMonthDay().substring(0, 4)));
            }
            if (Singleton.getHelpers().isNullEmptyOrWhitespace(codeIndexDocument.getRevision()) == false) {
                doc.add(new SortedSetDocValuesFacetField(Values.REVISION, codeIndexDocument.getRevision()));
            }
            if (Singleton.getHelpers().isNullEmptyOrWhitespace(codeIndexDocument.isDeleted()) == false) {
                doc.add(new SortedSetDocValuesFacetField(Values.DELETED, codeIndexDocument.isDeleted()));
            }

            String indexContents = Values.EMPTYSTRING;

            indexContents += this.searchcodeLib.splitKeywords(codeIndexDocument.getContents());
            indexContents += this.searchcodeLib.codeCleanPipeline(codeIndexDocument.getContents());
            this.searchcodeLib.addToSpellingCorrector(codeIndexDocument.getContents()); // Store in spelling corrector

            indexContents = indexContents.toLowerCase();

            doc.add(new TextField(Values.REPONAME, codeIndexDocument.getRepoName(), Field.Store.YES));
            doc.add(new TextField(Values.FILENAME, codeIndexDocument.getFileName(), Field.Store.YES));
            doc.add(new TextField(Values.FILELOCATION, codeIndexDocument.getFileLocation(), Field.Store.YES));
            doc.add(new TextField(Values.FILELOCATIONFILENAME, codeIndexDocument.getFileLocationFilename(),
                    Field.Store.YES));
            doc.add(new TextField(Values.MD5HASH, codeIndexDocument.getMd5hash(), Field.Store.YES));
            doc.add(new TextField(Values.LANGUAGENAME, codeIndexDocument.getLanguageName(), Field.Store.YES));
            doc.add(new IntField(Values.CODELINES, codeIndexDocument.getCodeLines(), Field.Store.YES));
            doc.add(new TextField(Values.CONTENTS, indexContents, Field.Store.NO));
            doc.add(new TextField(Values.REPOLOCATION, codeIndexDocument.getRepoRemoteLocation(),
                    Field.Store.YES));
            doc.add(new TextField(Values.CODEOWNER, codeIndexDocument.getCodeOwner(), Field.Store.YES));
            doc.add(new TextField(Values.REVISION, codeIndexDocument.getRevision(), Field.Store.YES));
            doc.add(new TextField(Values.DATEYEARMONTHDAY, codeIndexDocument.getYearMonthDay(),
                    Field.Store.YES));
            doc.add(new TextField(Values.DATEYEARMONTH, codeIndexDocument.getYearMonth(), Field.Store.YES));
            doc.add(new TextField(Values.DATEYEAR, codeIndexDocument.getYear(), Field.Store.YES));
            doc.add(new TextField(Values.MESSAGE, codeIndexDocument.getMessage(), Field.Store.YES));
            doc.add(new TextField(Values.DELETED, codeIndexDocument.isDeleted(), Field.Store.YES));

            // Extra metadata in this case when it was last indexed
            doc.add(new LongField(Values.MODIFIED, new Date().getTime(), Field.Store.YES));

            writer.updateDocument(
                    new Term(Values.PATH, codeIndexDocument.getRepoLocationRepoNameLocationFilename()),
                    facetsConfig.build(taxoWriter, doc));

            count++;
            if (count >= INDEX_QUEUE_BATCH_SIZE) {
                codeIndexDocument = null;
            } else {
                codeIndexDocument = codeIndexDocumentQueue.poll();
            }
        }
    } finally {
        Singleton.getLogger().info("Closing writers");
        writer.close();
        taxoWriter.close();
    }
}

From source file:com.tripod.lucene.example.TestExampleLuceneBase.java

License:Apache License

@Before
public void setupBase() throws IOException, ParseException {
    analyzer = new StandardAnalyzer();
    directory = new RAMDirectory();

    facetsConfig = new FacetsConfig();
    facetsConfig.setIndexFieldName(ExampleField.COLOR.getName(), ExampleField.COLOR.getName());

    IndexWriterConfig config = new IndexWriterConfig(analyzer);
    try (IndexWriter writer = new IndexWriter(directory, config)) {
        final SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);

        // Doc1//from   w  w  w .  j a va 2 s .  c o m
        Document doc1 = new Document();
        doc1.add(new Field(ExampleField.ID.getName(), "1", StringField.TYPE_STORED));
        doc1.add(new SortedDocValuesField(ExampleField.ID.getName(), new BytesRef("1")));
        doc1.add(new Field(ExampleField.TITLE.getName(), "Title 1", TextField.TYPE_STORED));
        doc1.add(new Field(ExampleField.BODY.getName(), "Body 1 Solr is cool", TextField.TYPE_STORED));
        doc1.add(new Field(ExampleField.COLOR.getName(), "BLUE", StringField.TYPE_STORED));
        doc1.add(new SortedSetDocValuesFacetField(ExampleField.COLOR.getName(), "BLUE"));

        Date createDate1 = dateFormat.parse("2016-10-01T01:00:00Z");
        doc1.add(new NumericDocValuesField(ExampleField.CREATE_DATE.getName(), createDate1.getTime()));
        doc1.add(new StoredField(ExampleField.CREATE_DATE.getName(), createDate1.getTime()));
        writer.addDocument(facetsConfig.build(doc1));

        // Doc2
        Document doc2 = new Document();
        doc2.add(new Field(ExampleField.ID.getName(), "2", StringField.TYPE_STORED));
        doc2.add(new SortedDocValuesField(ExampleField.ID.getName(), new BytesRef("2")));
        doc2.add(new Field(ExampleField.TITLE.getName(), "Title 2", TextField.TYPE_STORED));
        doc2.add(new Field(ExampleField.BODY.getName(), "Body 2 Lucene is cool", TextField.TYPE_STORED));
        doc2.add(new Field(ExampleField.COLOR.getName(), "RED", StringField.TYPE_STORED));
        doc2.add(new SortedSetDocValuesFacetField(ExampleField.COLOR.getName(), "RED"));

        Date createDate2 = dateFormat.parse("2016-10-01T02:00:00Z");
        doc2.add(new NumericDocValuesField(ExampleField.CREATE_DATE.getName(), createDate2.getTime()));
        doc2.add(new StoredField(ExampleField.CREATE_DATE.getName(), createDate2.getTime()));
        writer.addDocument(facetsConfig.build(doc2));

        // Doc3
        Document doc3 = new Document();
        doc3.add(new Field(ExampleField.ID.getName(), "3", StringField.TYPE_STORED));
        doc3.add(new SortedDocValuesField(ExampleField.ID.getName(), new BytesRef("3")));
        doc3.add(new Field(ExampleField.TITLE.getName(), "Title 3", TextField.TYPE_STORED));
        doc3.add(new Field(ExampleField.BODY.getName(), "Body 3 Solr is Great, Solr is Fun",
                TextField.TYPE_STORED));
        doc3.add(new Field(ExampleField.COLOR.getName(), "GREEN", StringField.TYPE_STORED));
        doc3.add(new SortedSetDocValuesFacetField(ExampleField.COLOR.getName(), "GREEN"));

        Date createDate3 = dateFormat.parse("2016-10-01T03:00:00Z");
        doc3.add(new NumericDocValuesField(ExampleField.CREATE_DATE.getName(), createDate3.getTime()));
        doc3.add(new StoredField(ExampleField.CREATE_DATE.getName(), createDate3.getTime()));
        writer.addDocument(facetsConfig.build(doc3));

        // Doc4
        Document doc4 = new Document();
        doc4.add(new Field(ExampleField.ID.getName(), "4", StringField.TYPE_STORED));
        doc4.add(new SortedDocValuesField(ExampleField.ID.getName(), new BytesRef("4")));
        doc4.add(new Field(ExampleField.TITLE.getName(), "Title 4", TextField.TYPE_STORED));
        doc4.add(new Field(ExampleField.BODY.getName(), "Body 4", TextField.TYPE_STORED));
        doc4.add(new Field(ExampleField.COLOR.getName(), "BLUE", StringField.TYPE_STORED));
        doc4.add(new SortedSetDocValuesFacetField(ExampleField.COLOR.getName(), "BLUE"));

        Date createDate4 = dateFormat.parse("2016-10-01T04:00:00Z");
        doc4.add(new NumericDocValuesField(ExampleField.CREATE_DATE.getName(), createDate4.getTime()));
        doc4.add(new StoredField(ExampleField.CREATE_DATE.getName(), createDate4.getTime()));
        writer.addDocument(facetsConfig.build(doc4));

        // Doc5
        Document doc5 = new Document();
        doc5.add(new Field(ExampleField.ID.getName(), "5", StringField.TYPE_STORED));
        doc5.add(new SortedDocValuesField(ExampleField.ID.getName(), new BytesRef("5")));
        doc5.add(new Field(ExampleField.TITLE.getName(), "Title 5", TextField.TYPE_STORED));
        doc5.add(new Field(ExampleField.BODY.getName(), "Body 5", TextField.TYPE_STORED));
        doc5.add(new Field(ExampleField.COLOR.getName(), "RED", StringField.TYPE_STORED));
        doc5.add(new SortedSetDocValuesFacetField(ExampleField.COLOR.getName(), "RED"));

        Date createDate5 = dateFormat.parse("2016-10-01T05:00:00Z");
        doc5.add(new NumericDocValuesField(ExampleField.CREATE_DATE.getName(), createDate5.getTime()));
        doc5.add(new StoredField(ExampleField.CREATE_DATE.getName(), createDate5.getTime()));
        writer.addDocument(facetsConfig.build(doc5));

        // commit docs
        writer.commit();
    }

    // needs to be opened after the writer is closed otherwise it won't see the test data
    searcherManager = new SearcherManager(directory, null);
}

From source file:com.tripod.lucene.example.TestExampleSummaryQueryService.java

License:Apache License

@Test
public void testRefreshingSearcherManager()
        throws IOException, ParseException, QueryException, InterruptedException {
    // Add a new document
    final IndexWriterConfig config = new IndexWriterConfig(analyzer);
    try (IndexWriter writer = new IndexWriter(directory, config)) {
        final SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);

        Document doc = new Document();
        doc.add(new Field(ExampleField.ID.getName(), "99", StringField.TYPE_STORED));
        doc.add(new SortedDocValuesField(ExampleField.ID.getName(), new BytesRef("1")));
        doc.add(new Field(ExampleField.TITLE.getName(), "Title 99", TextField.TYPE_STORED));
        doc.add(new Field(ExampleField.BODY.getName(), "Body 99", TextField.TYPE_STORED));
        doc.add(new Field(ExampleField.COLOR.getName(), "BLUE", StringField.TYPE_STORED));
        doc.add(new SortedSetDocValuesFacetField(ExampleField.COLOR.getName(), "BLUE"));

        Date createDate1 = dateFormat.parse("2016-11-01T01:00:00Z");
        doc.add(new NumericDocValuesField(ExampleField.CREATE_DATE.getName(), createDate1.getTime()));
        doc.add(new StoredField(ExampleField.CREATE_DATE.getName(), createDate1.getTime()));
        writer.addDocument(facetsConfig.build(doc));

        writer.commit();//  w w  w. j ava  2  s  . co  m
    }

    // Query for the new document and shouldn't get it
    LuceneQuery query = new LuceneQuery("id:99");
    QueryResults<ExampleSummary> results = queryService.search(query);

    assertNotNull(results);
    assertNotNull(results.getResults());
    assertEquals(0, results.getResults().size());

    // Start a refresher for the SearchManager
    SearcherManagerRefresher refresher = new SearcherManagerRefresher(searcherManager, 2000);
    try {
        // Start the refresher and then wait slightly longer than refresh interval
        refresher.start();
        Thread.sleep(3000);

        // Query again should get a result now
        query = new LuceneQuery("id:99");
        results = queryService.search(query);

        assertNotNull(results);
        assertNotNull(results.getResults());
        assertEquals(1, results.getResults().size());
    } finally {
        refresher.stop();
    }
}

From source file:com.zghw.lucene.demo.SimpleSortedSetFacetsExample.java

License:Apache License

/** Build the example index. */
private void index() throws IOException {
    IndexWriter indexWriter = new IndexWriter(indexDir,
            new IndexWriterConfig(FacetExamples.EXAMPLES_VER, new WhitespaceAnalyzer()));
    Document doc = new Document();
    doc.add(new SortedSetDocValuesFacetField("Author", "Bob"));
    doc.add(new SortedSetDocValuesFacetField("Publish Year", "2010"));
    indexWriter.addDocument(config.build(doc));

    doc = new Document();
    doc.add(new SortedSetDocValuesFacetField("Author", "Lisa"));
    doc.add(new SortedSetDocValuesFacetField("Publish Year", "2010"));
    indexWriter.addDocument(config.build(doc));

    doc = new Document();
    doc.add(new SortedSetDocValuesFacetField("Author", "Lisa"));
    doc.add(new SortedSetDocValuesFacetField("Publish Year", "2012"));
    indexWriter.addDocument(config.build(doc));

    doc = new Document();
    doc.add(new SortedSetDocValuesFacetField("Author", "Susan"));
    doc.add(new SortedSetDocValuesFacetField("Publish Year", "2012"));
    indexWriter.addDocument(config.build(doc));

    doc = new Document();
    doc.add(new SortedSetDocValuesFacetField("Author", "Frank"));
    doc.add(new SortedSetDocValuesFacetField("Publish Year", "1999"));
    indexWriter.addDocument(config.build(doc));

    indexWriter.close();/*from   w w  w .  j av  a2  s.co m*/
}