Java Code Examples for org.apache.lucene.index.IndexWriter#updateDocument()

The following examples show how to use org.apache.lucene.index.IndexWriter#updateDocument() . 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 File: test.java    From vscode-extension with MIT License 6 votes vote down vote up
private void updateDocs(final Term uid, final List<ParseContext.Document> docs, final IndexWriter indexWriter) throws IOException {
    if (softDeleteEnabled) {
        if (docs.size() > 1) {
            indexWriter.softUpdateDocuments(uid, docs, softDeletesField);
        } else {
            indexWriter.softUpdateDocument(uid, docs.get(0), softDeletesField);
        }
    } else {
        if (docs.size() > 1) {
            indexWriter.updateDocuments(uid, docs);
        } else {
            indexWriter.updateDocument(uid, docs.get(0));
        }
    }
    numDocUpdates.inc(docs.size());
}
 
Example 2
Source File: IndexFiles.java    From Java-Data-Science-Cookbook with MIT License 6 votes vote down vote up
static void indexDoc(IndexWriter writer, Path file, long lastModified) throws IOException {
	try (InputStream stream = Files.newInputStream(file)) {
		Document doc = new Document();
		Field pathField = new StringField("path", file.toString(), Field.Store.YES);
		doc.add(pathField);
		doc.add(new LongPoint("modified", lastModified));
		doc.add(new TextField("contents", new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))));

		if (writer.getConfig().getOpenMode() == OpenMode.CREATE) {
			System.out.println("adding " + file);
			writer.addDocument(doc);
		} else {
			System.out.println("updating " + file);
			writer.updateDocument(new Term("path", file.toString()), doc);
		}
	}
}
 
Example 3
Source File: InternalEngine.java    From crate with Apache License 2.0 6 votes vote down vote up
private void updateDocs(final Term uid, final List<ParseContext.Document> docs, final IndexWriter indexWriter) throws IOException {
    if (softDeleteEnabled) {
        if (docs.size() > 1) {
            indexWriter.softUpdateDocuments(uid, docs, softDeletesField);
        } else {
            indexWriter.softUpdateDocument(uid, docs.get(0), softDeletesField);
        }
    } else {
        if (docs.size() > 1) {
            indexWriter.updateDocuments(uid, docs);
        } else {
            indexWriter.updateDocument(uid, docs.get(0));
        }
    }
    numDocUpdates.inc(docs.size());
}
 
Example 4
Source File: BaseIndex.java    From everywhere with Apache License 2.0 5 votes vote down vote up
private static void indexDoc(IndexWriter writer, FileBean t) throws Exception {
        Document doc = new Document();
        if (t.getContent() != null) {
            doc.add(new TextField(LuceneConstants.PATH, t.getFilepath(), Field.Store.YES));
            doc.add(new StringField(LuceneConstants.MODIFIED, UtilsTool.getDateStrByLastModified(t.getLastModified()), Field.Store.YES));
            doc.add(new TextField(LuceneConstants.CONTENT, t.getContent(), CommonConstants.IS_OPEN_CONTEXT ? Field.Store.YES : Field.Store.NO));
//            System.out.println("added to document:" + t.getFilepath());
            if (writer.getConfig().getOpenMode() == IndexWriterConfig.OpenMode.CREATE){
                writer.addDocument(doc);
            } else{
                writer.updateDocument(new Term(LuceneConstants.PATH, t.getFilepath()), doc);
            }
        }
    }
 
Example 5
Source File: IndexFiles.java    From elasticsearch-full with Apache License 2.0 5 votes vote down vote up
static void indexDoc(IndexWriter writer, Path file, long lastModified) throws IOException {
    try (InputStream stream = Files.newInputStream(file)) {
        Document doc = new Document();
        Field pathField = new StringField("path", file.toString(), Field.Store.YES);
        doc.add(pathField);
        doc.add(new LongPoint("modified", lastModified));
        doc.add(new TextField("contents", new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))));
        if (writer.getConfig().getOpenMode() == IndexWriterConfig.OpenMode.CREATE) {
            System.out.println("adding " + file);
            writer.addDocument(doc);
            System.out.println("updating " + file);
            writer.updateDocument(new Term("path", file.toString()), doc);
        }
    }
}
 
Example 6
Source File: LuceneIndex.java    From cjs_ssms with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 更新博客索引
 *
 * @param user
 * @throws Exception
 */
public void updateIndex(UUser user) throws Exception {
  IndexWriter writer = getWriter();
  Document doc = new Document();
  doc.add(new StringField("userid", String.valueOf(user.getId()), Field.Store.YES));
  doc.add(new TextField("username", user.getUsername(), Field.Store.YES));
  doc.add(new TextField("description", user.getDescription(), Field.Store.YES));

  writer.updateDocument(new Term("userid", String.valueOf(user.getId())), doc);
  writer.close();
}
 
Example 7
Source File: UpdateDocTask.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public int doLogic() throws Exception {
  final String docID = doc.get(DocMaker.ID_FIELD);
  if (docID == null) {
    throw new IllegalStateException("document must define the docid field");
  }
  final IndexWriter iw = getRunData().getIndexWriter();
  iw.updateDocument(new Term(DocMaker.ID_FIELD, docID), doc);
  return 1;
}
 
Example 8
Source File: LuceneRecord.java    From HongsCORE with MIT License 5 votes vote down vote up
public void setDoc(String id, Document doc) throws HongsException {
    IndexWriter iw = getWriter();
    try {
        iw.updateDocument (new Term("@"+Cnst.ID_KEY, id), doc);
    } catch (IOException ex) {
        throw new HongsException(ex);
    }
    if (!REFLUX_MODE) {
        commit();
    }
}
 
Example 9
Source File: CompositeTermRecognitionProcessor.java    From jate with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Boolean candidateExtraction(SolrCore core, String jatePropertyFile)
        throws IOException, JATEException {
    SolrIndexSearcher indexSearcher = core.getSearcher().get();
    IndexWriter writerIn = null;
    try {
    	writerIn = core.getSolrCoreState().getIndexWriter(core).get();
     Map<String,List<CopyField>> copyFields = core.getLatestSchema().getCopyFieldsMap();
	
     for (int i=0; i<indexSearcher.maxDoc(); i++) {
         Document doc = indexSearcher.doc(i);
	
         SolrUtil.copyFields(copyFields, DEFAULT_BOOST_VALUE, doc);
	
         writerIn.updateDocument(new Term("id",doc.get("id")), doc);
     }
     writerIn.commit();
	
     return true;
    } finally {
    	indexSearcher.close();
    	if (writerIn != null) {
    		writerIn.close();
    	}
    	
    }
}
 
Example 10
Source File: BasicStorageTest.java    From lumongo with Apache License 2.0 5 votes vote down vote up
private static void addDoc(IndexWriter w, String title, String uid) throws IOException {
	Document doc = new Document();
	doc.add(new TextField("title", title, Field.Store.YES));
	doc.add(new TextField("uid", uid, Field.Store.YES));
	doc.add(new StringField("uid", uid, Field.Store.YES));
	doc.add(new IntPoint("testIntField", 3));
	long date = System.currentTimeMillis();
	doc.add(new LongPoint("date", date));
	doc.add(new NumericDocValuesField("date", date));
	doc.add(new SortedSetDocValuesField("category", new BytesRef("Anything")));
	Term uidTerm = new Term("uid", uid);

	w.updateDocument(uidTerm, doc);
}
 
Example 11
Source File: IndexFiles.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/** Indexes a single document */
static void indexDoc(IndexWriter writer, Path file, long lastModified) throws IOException {
  try (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 LongPoint that is indexed (i.e. efficiently filterable with
    // PointRangeQuery).  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 LongPoint("modified", lastModified));
    
    // 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);
    }
  }
}
 
Example 12
Source File: BuildIndex.java    From fnlp with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @param args
 * @throws IOException 
 * @throws LoadModelException 
 */
public static void main(String[] args) throws IOException, LoadModelException {
	String indexPath = "../tmp/lucene";
	System.out.println("Indexing to directory '" + indexPath  + "'...");
	Date start = new Date();
	Directory dir = FSDirectory.open(new File(indexPath));//Dirctory dir-->FSDirectory
	//需要先初始化 CNFactory
	CNFactory factory = CNFactory.getInstance("../models",Models.SEG_TAG);
	Analyzer analyzer = new FNLPAnalyzer(Version.LUCENE_47);
	IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_47, analyzer);
	iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
	IndexWriter writer = new IndexWriter(dir, iwc);

	String[] strs = new String[]{
			"终端的保修期为一年。",
			"凡在保修期内非人为损坏,均可免费保修。",
			"人为损坏的终端将视情况收取维修费用。",
			"中国"
	};
	//Date start = new Date();
	for(int i=0;i<strs.length;i++){

		Document doc = new Document();

		Field field = new TextField("content", strs[i] , Field.Store.YES);
		doc.add(field);
		if (writer.getConfig().getOpenMode() == OpenMode.CREATE) {
			writer.addDocument(doc);
		} else {
			writer.updateDocument(new Term("content",strs[i]), doc);
		}
	}
	writer.close();
	
	//!!这句话是不是漏了
	//dir.close();
	//!!这句话是不是漏了

	Date end = new Date();
	System.out.println(end.getTime() - start.getTime() + " total milliseconds");

}