Example usage for org.apache.lucene.index IndexWriter addDocument

List of usage examples for org.apache.lucene.index IndexWriter addDocument

Introduction

In this page you can find the example usage for org.apache.lucene.index IndexWriter addDocument.

Prototype

public long addDocument(Iterable<? extends IndexableField> doc) throws IOException 

Source Link

Document

Adds a document to this index.

Usage

From source file:com.leavesfly.lia.searching.NearRealTimeTest.java

License:Apache License

public void testNearRealTime() throws Exception {
    Directory dir = new RAMDirectory();
    IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_30),
            IndexWriter.MaxFieldLength.UNLIMITED);
    for (int i = 0; i < 10; i++) {
        Document doc = new Document();
        doc.add(new Field("id", "" + i, Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));
        doc.add(new Field("text", "aaa", Field.Store.NO, Field.Index.ANALYZED));
        writer.addDocument(doc);
    }// w  w w  .j  ava 2s.  com
    IndexReader reader = writer.getReader(); // #1
    IndexSearcher searcher = new IndexSearcher(reader); // #A

    Query query = new TermQuery(new Term("text", "aaa"));
    TopDocs docs = searcher.search(query, 1);
    assertEquals(10, docs.totalHits); // #B

    writer.deleteDocuments(new Term("id", "7")); // #2

    Document doc = new Document(); // #3
    doc.add(new Field("id", // #3
            "11", // #3
            Field.Store.NO, // #3
            Field.Index.NOT_ANALYZED_NO_NORMS)); // #3
    doc.add(new Field("text", // #3
            "bbb", // #3
            Field.Store.NO, // #3
            Field.Index.ANALYZED)); // #3
    writer.addDocument(doc); // #3

    IndexReader newReader = reader.reopen(); // #4
    assertFalse(reader == newReader); // #5
    reader.close(); // #6
    searcher = new IndexSearcher(newReader);

    TopDocs hits = searcher.search(query, 10); // #7
    assertEquals(9, hits.totalHits); // #7

    query = new TermQuery(new Term("text", "bbb")); // #8
    hits = searcher.search(query, 1); // #8
    assertEquals(1, hits.totalHits); // #8

    newReader.close();
    writer.close();
}

From source file:com.leavesfly.lia.searching.PhraseQueryTest.java

License:Apache License

protected void setUp() throws IOException {
    dir = new RAMDirectory();
    IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED);
    Document doc = new Document();
    doc.add(new Field("field", // 1
            "the quick brown fox jumped over the lazy dog", // 1
            Field.Store.YES, // 1
            Field.Index.ANALYZED)); // 1
    writer.addDocument(doc);
    writer.close();/*from  ww  w.ja  va 2 s  .  c o  m*/

    searcher = new IndexSearcher(dir);
}

From source file:com.leavesfly.lia.searching.ScoreTest.java

License:Apache License

private void indexSingleFieldDocs(Field[] fields) throws Exception {
    IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(),
            IndexWriter.MaxFieldLength.UNLIMITED);
    for (Field f : fields) {
        Document doc = new Document();
        doc.add(f);/* w w w  .ja v a2  s.  c  om*/
        writer.addDocument(doc);
    }
    writer.optimize();
    writer.close();
}

From source file:com.leavesfly.lia.tool.ChainedFilterTest.java

License:Apache License

public void setUp() throws Exception {
    directory = new RAMDirectory();
    IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(),
            IndexWriter.MaxFieldLength.UNLIMITED);

    Calendar cal = Calendar.getInstance();
    cal.set(2009, 1, 1, 0, 0); // A

    for (int i = 0; i < MAX; i++) {
        Document doc = new Document();
        doc.add(new Field("key", "" + (i + 1), Field.Store.YES, Field.Index.NOT_ANALYZED));
        doc.add(new Field("owner", (i < MAX / 2) ? "bob" : "sue", Field.Store.YES, Field.Index.NOT_ANALYZED));
        doc.add(new Field("date", DateTools.timeToString(cal.getTimeInMillis(), DateTools.Resolution.DAY),
                Field.Store.YES, Field.Index.NOT_ANALYZED));
        writer.addDocument(doc);

        cal.add(Calendar.DATE, 1);
    }/*ww w  .  j  a v a2 s . co  m*/

    writer.close();

    searcher = new IndexSearcher(directory);

    BooleanQuery bq = new BooleanQuery(); // B
    bq.add(new TermQuery(new Term("owner", "bob")), // B
            BooleanClause.Occur.SHOULD); // B
    bq.add(new TermQuery(new Term("owner", "sue")), // B
            BooleanClause.Occur.SHOULD); // B
    query = bq;

    cal.set(2099, 1, 1, 0, 0);
    dateFilter = TermRangeFilter.Less("date", // C
            DateTools.timeToString( // C
                    cal.getTimeInMillis(), // C
                    DateTools.Resolution.DAY));// C

    bobFilter = new CachingWrapperFilter( // D
            new QueryWrapperFilter( // D
                    new TermQuery(new Term("owner", "bob")))); // D

    sueFilter = new CachingWrapperFilter( // E
            new QueryWrapperFilter( // E
                    new TermQuery(new Term("owner", "sue")))); // E
}

From source file:com.leavesfly.lia.tool.SpatialLuceneExample.java

License:Apache License

private void addLocation(IndexWriter writer, String name, double lat, double lng) throws IOException {

    Document doc = new Document();
    doc.add(new Field("name", name, Field.Store.YES, Field.Index.ANALYZED));

    doc.add(new Field(latField, NumericUtils.doubleToPrefixCoded(lat), // #A
            Field.Store.YES, Field.Index.NOT_ANALYZED)); // #A
    doc.add(new Field(lngField, NumericUtils.doubleToPrefixCoded(lng), // #A
            Field.Store.YES, Field.Index.NOT_ANALYZED)); // #A

    doc.add(new Field("metafile", "doc", Field.Store.YES, Field.Index.ANALYZED));

    IProjector projector = new SinusoidalProjector(); // #B

    int startTier = 5; // #C
    int endTier = 15; // #C

    for (; startTier <= endTier; startTier++) {
        CartesianTierPlotter ctp;/*from ww w . j  a  v a 2  s  . c  om*/
        ctp = new CartesianTierPlotter(startTier, // #D
                projector, tierPrefix); // #D

        double boxId = ctp.getTierBoxId(lat, lng); // #D
        System.out.println("Adding field " + ctp.getTierFieldName() + ":" + boxId);
        doc.add(new Field(ctp.getTierFieldName(), NumericUtils // #E
                .doubleToPrefixCoded(boxId), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));

    }

    writer.addDocument(doc);
    System.out.println("===== Added Doc to index ====");
}

From source file:com.leixl.easyframework.lucene.impl.LuceneEMovieDaoImpl.java

License:Open Source License

public Integer index(IndexWriter writer, Date startDate, Date endDate, Integer startId, Integer max)
        throws CorruptIndexException, IOException {
    Finder f = Finder.create("select bean from EMovie bean");

    f.append(" where 1=1");
    if (startId != null) {
        f.append(" and bean.id > :startId");
        f.setParam("startId", startId);
    }//from  ww w. j a  v  a  2  s.  c o m
    if (startDate != null) {
        f.append(" and bean.createDate >= :startDate");
        f.setParam("startDate", startDate);
    }
    if (endDate != null) {
        f.append(" and bean.createDate <= :endDate");
        f.setParam("endDate", endDate);
    }
    f.append(" order by bean.id asc");
    if (max != null) {
        f.setMaxResults(max);
    }
    Session session = getSession();
    ScrollableResults movies = f.createQuery(getSession()).setCacheMode(CacheMode.IGNORE)
            .scroll(ScrollMode.FORWARD_ONLY);
    int count = 0;
    EMovie movie = null;
    while (movies.next()) {
        movie = (EMovie) movies.get(0);
        writer.addDocument(LuceneContent.createDocument(movie));
        if (++count % 20 == 0) {
            session.clear();
        }
    }
    if (count < max || movie == null) {
        // ????????
        return null;
    } else {
        // ?ID
        return movie.getId();
    }

}

From source file:com.lin.studytest.lucene.IndexFiles.java

License:Apache License

/** Indexes a single document */
static void indexDoc(IndexWriter writer, Path file, long lastModified) throws IOException {
    try {/*  www  . j  a  va 2s.c o  m*/
        InputStream stream = Files.newInputStream(file);
        // make a new, empty document
        Document doc = new Document();

        // Add the path of the file as a field named "path".  Use a
        // field that is indexed (i.e. searchable), but don't tokenize 
        // the field into separate words and don't index term frequency
        // or positional information:
        Field pathField = new StringField("path", file.toString(), Field.Store.YES);
        doc.add(pathField);

        // Add the last modified date of the file a field named "modified".
        // Use a LongField that is indexed (i.e. efficiently filterable with
        // NumericRangeFilter).  This indexes to milli-second resolution, which
        // is often too fine.  You could instead create a number based on
        // year/month/day/hour/minutes/seconds, down the resolution you require.
        // For example the long value 2011021714 would mean
        // February 17, 2011, 2-3 PM.
        doc.add(new LongField("modified", lastModified, Field.Store.NO));

        // Add the contents of the file to a field named "contents".  Specify a Reader,
        // so that the text of the file is tokenized and indexed, but not stored.
        // Note that FileReader expects the file to be in UTF-8 encoding.
        // If that's not the case searching for special characters will fail.
        doc.add(new TextField("contents",
                new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))));

        if (writer.getConfig().getOpenMode() == OpenMode.CREATE) {
            // New index, so we just add the document (no old document can be there):
            System.out.println("adding " + file);
            writer.addDocument(doc);
        } else {
            // Existing index (an old copy of this document may have been indexed) so 
            // we use updateDocument instead to replace the old one matching the exact 
            // path, if present:
            System.out.println("updating " + file);
            writer.updateDocument(new Term("path", file.toString()), doc);
        }
    } finally {

    }
}

From source file:com.liusoft.dlog4j.search.SearchProxy.java

License:Open Source License

/**
 * //  w  w  w .j a va 2  s  . c o m
 * @param doc
 * @throws Exception 
 */
public static synchronized void add(SearchEnabled doc) throws Exception {
    if (doc == null)
        return;

    Document lucene_doc = new Document();

    //Set keyword field
    String key = getField(doc, doc.getKeywordField());
    lucene_doc.add(Keyword(doc.getKeywordField(), key));

    //Set identity(classname) of object
    lucene_doc.add(Keyword(CLASSNAME_FIELD, doc.getClass().getName()));

    //Set storage field
    String[] storeFields = doc.getStoreFields();
    for (int i = 0; storeFields != null && i < storeFields.length; i++) {
        String propertyValue = getField(doc, storeFields[i]);
        if (propertyValue != null)
            lucene_doc.add(Keyword(storeFields[i], propertyValue));
    }
    //Set indexed field
    String[] indexFields = doc.getIndexFields();
    for (int i = 0; indexFields != null && i < indexFields.length; i++) {
        String propertyValue = getField(doc, indexFields[i]);
        lucene_doc.add(UnStored(indexFields[i], propertyValue));
    }
    //Write document
    IndexWriter writer = getWriter(doc.name());
    try {
        writer.addDocument(lucene_doc);
        writer.optimize();
    } finally {
        try {
            writer.close();
        } catch (Exception e) {
            log.error("Error occur when closing IndexWriter", e);
        } finally {
            writer = null;
        }
        lucene_doc = null;
    }

}

From source file:com.lorelib.analyzer.sample.LuceneIndexAndSearchDemo.java

License:Apache License

/**
 * /*from   ww  w  .  j a  v a  2 s .c o  m*/
 * ???
 * @param args
 */
public static void main(String[] args) {
    //Lucene Document??
    String fieldName = "text";
    //
    String text = "IK Analyzer???????";

    //IKAnalyzer?
    Analyzer analyzer = new IKAnalyzer(true);

    Directory directory = null;
    IndexWriter iwriter = null;
    IndexReader ireader = null;
    IndexSearcher isearcher = null;
    try {
        //
        directory = new RAMDirectory();

        //?IndexWriterConfig
        IndexWriterConfig iwConfig = new IndexWriterConfig(analyzer);
        iwConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
        iwriter = new IndexWriter(directory, iwConfig);
        //
        Document doc = new Document();
        doc.add(new StringField("ID", "10000", Field.Store.YES));
        doc.add(new TextField(fieldName, text, Field.Store.YES));
        iwriter.addDocument(doc);
        iwriter.close();

        //?**********************************
        //?
        ireader = DirectoryReader.open(directory);
        isearcher = new IndexSearcher(ireader);

        String keyword = "?";
        //QueryParser?Query
        QueryParser qp = new QueryParser(fieldName, analyzer);
        qp.setDefaultOperator(QueryParser.AND_OPERATOR);
        Query query = qp.parse(keyword);
        System.out.println("Query = " + query);

        //?5?
        TopDocs topDocs = isearcher.search(query, 5);
        System.out.println("" + topDocs.totalHits);
        //
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        for (int i = 0; i < topDocs.totalHits; i++) {
            Document targetDoc = isearcher.doc(scoreDocs[i].doc);
            System.out.println("" + targetDoc.toString());
        }

    } catch (CorruptIndexException e) {
        e.printStackTrace();
    } catch (LockObtainFailedException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    } finally {
        if (ireader != null) {
            try {
                ireader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (directory != null) {
            try {
                directory.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.luc.indexer.LuceneCommonIndexer.java

License:Apache License

private static void indexFile(IndexWriter writer, File file) throws IOException, SAXException, TikaException {

    if (file.isHidden() || !file.exists() || !file.canRead()) {
        return;/*from  www.jav  a 2  s.  com*/
    }

    /*
            
    System.out.println("Indexing " + file.getCanonicalPath());
            
    */

    is = new FileInputStream(file);
    textHandler = new BodyContentHandler();
    metadata = new Metadata();

    commonParser = new AutoDetectParser();
    commonParser.parse(is, textHandler, metadata, new ParseContext());

    Document doc = new Document();

    doc.add(new Field("contents", textHandler.toString(), Field.Store.NO, Field.Index.ANALYZED));
    doc.add(new Field("filename", file.getCanonicalPath(), Field.Store.YES, Field.Index.NOT_ANALYZED));
    writer.addDocument(doc);
}