本文整理汇总了C#中Lucene.Net.Index.IndexWriter.UpdateDocument方法的典型用法代码示例。如果您正苦于以下问题:C# IndexWriter.UpdateDocument方法的具体用法?C# IndexWriter.UpdateDocument怎么用?C# IndexWriter.UpdateDocument使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Lucene.Net.Index.IndexWriter
的用法示例。
在下文中一共展示了IndexWriter.UpdateDocument方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestRollbackIntegrityWithBufferFlush
public void TestRollbackIntegrityWithBufferFlush()
{
Directory dir = new MockRAMDirectory();
IndexWriter w = new IndexWriter(dir, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED);
for (int i = 0; i < 5; i++)
{
Document doc = new Document();
doc.Add(new Field("pk", i.ToString(), Field.Store.YES, Field.Index.ANALYZED_NO_NORMS));
w.AddDocument(doc);
}
w.Close();
// If buffer size is small enough to cause a flush, errors ensue...
w = new IndexWriter(dir, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED);
w.SetMaxBufferedDocs(2);
Term pkTerm = new Term("pk", "");
for (int i = 0; i < 3; i++)
{
Document doc = new Document();
String value = i.ToString();
doc.Add(new Field("pk", value, Field.Store.YES, Field.Index.ANALYZED_NO_NORMS));
doc.Add(new Field("text", "foo", Field.Store.YES, Field.Index.ANALYZED_NO_NORMS));
w.UpdateDocument(pkTerm.CreateTerm(value), doc);
}
w.Rollback();
IndexReader r = IndexReader.Open(dir, true);
Assert.AreEqual(5, r.NumDocs(), "index should contain same number of docs post rollback");
r.Close();
dir.Close();
}
示例2: TestRollbackIntegrityWithBufferFlush
public virtual void TestRollbackIntegrityWithBufferFlush()
{
Directory dir = NewDirectory();
RandomIndexWriter rw = new RandomIndexWriter(Random(), dir);
for (int i = 0; i < 5; i++)
{
Document doc = new Document();
doc.Add(NewStringField("pk", Convert.ToString(i), Field.Store.YES));
rw.AddDocument(doc);
}
rw.Dispose();
// If buffer size is small enough to cause a flush, errors ensue...
IndexWriter w = new IndexWriter(dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random())).SetMaxBufferedDocs(2).SetOpenMode(IndexWriterConfig.OpenMode_e.APPEND));
for (int i = 0; i < 3; i++)
{
Document doc = new Document();
string value = Convert.ToString(i);
doc.Add(NewStringField("pk", value, Field.Store.YES));
doc.Add(NewStringField("text", "foo", Field.Store.YES));
w.UpdateDocument(new Term("pk", value), doc);
}
w.Rollback();
IndexReader r = DirectoryReader.Open(dir);
Assert.AreEqual(5, r.NumDocs, "index should contain same number of docs post rollback");
r.Dispose();
dir.Dispose();
}
示例3: AddToIndex
// This method indexes the given text.
private static void AddToIndex(int id, string text, IndexWriter writer)
{
Term term = new Term("id", id.ToString());
Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document();
doc.Add(new Field("id", id.ToString(), Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new Field("mainText", text, Field.Store.YES, Field.Index.ANALYZED));
writer.AddDocument(doc);
writer.UpdateDocument(term, doc);
}
示例4: FindChangedAndIndex
public int FindChangedAndIndex()
{
var lastDateTimeFile = Path.Combine(path, "last.time");
var lastDateTime = DateTime.MinValue;
try
{
if (File.Exists(lastDateTimeFile))
{
lastDateTime = DateTime.Parse(File.ReadAllText(lastDateTimeFile)).ToUniversalTime();
}
}
catch (FormatException) { }
catch (ArgumentNullException) { }
var copyLastDateTime = lastDateTime;
lastDateTime = DateTime.UtcNow;
var texts = SelectTextsForIndex(copyLastDateTime, true);
if (0 < texts.Count)
{
var directory = GetOrCreateDirectory(path);
var analyzer = new AnalyzersProvider().GetAnalyzer(tenant.GetCulture().TwoLetterISOLanguageName);
var create = directory.ListAll().Length == 0;
var index = new IndexWriter(directory, analyzer, create, IndexWriter.MaxFieldLength.UNLIMITED);
try
{
foreach (var t in texts)
{
var term = new Term("Id", t.Item1);
if (string.IsNullOrEmpty(t.Item2))
{
index.DeleteDocuments(term);
}
else
{
var doc = new Document();
doc.Add(new Field("Id", t.Item1, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.YES));
doc.Add(new Field("Text", t.Item2, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));
index.UpdateDocument(term, doc);
}
}
}
finally
{
index.Optimize();
index.Commit();
index.Close();
}
File.WriteAllText(lastDateTimeFile, lastDateTime.ToString("o"));
}
return texts.Count;
}
示例5: IndexUrlsInTweet
/// <summary>
/// Indexes all the urls in the supplied tweet
/// </summary>
public void IndexUrlsInTweet(Tweet tweet, IList<string> indexes)
{
//find urls to index in the url index
foreach (Uri uri in tweet.GetUrlsFromTweet())
{
Console.WriteLine("URL" + uri);
//setup index writer
FSDirectory luceneDirectory = FSDirectory.Open(new DirectoryInfo(Settings.URL_INDEX_DIR));
IndexWriter writer = new IndexWriter(luceneDirectory, _analyzer, IndexWriter.MaxFieldLength.UNLIMITED);
//need to check if its not already indexed
//if it is already indexed, then just add a user to the index field in lucene
Document existingDoc = _searchengine.GetDocumentForUrl(uri.ToString());
if (existingDoc != null)
{
//document already exists, add a user to it.
Console.WriteLine("Already Exists");
bool wasUpdated = false;
wasUpdated |= UpdateIndexes(existingDoc, indexes);
wasUpdated |= UpdateTweets(existingDoc, tweet.TweetId);
//only update document if it was changed.
if (wasUpdated)
writer.UpdateDocument(new Term(Settings.FIELD_URL_ID, existingDoc.GetField(Settings.FIELD_URL_ID).StringValue()), existingDoc);
writer.Close();
continue;
}
Document luceneDocument = IndexUrl(uri, indexes, tweet.TweetId);
if (luceneDocument != null)
writer.AddDocument(luceneDocument);
writer.Optimize();
writer.Close();
}
//update the date indexed on the tweet
_tweetIndexer.UpdateDateIndexed(tweet);
}
示例6: Execute
public void Execute(IndexWriter writer)
{
Document document = IndexDefinition.Convert(Entity);
Term index = IndexDefinition.GetIndex(Entity);
if (document == null || index == null)
return;
switch (Operation)
{
case LuceneOperation.Insert:
writer.AddDocument(document);
break;
case LuceneOperation.Update:
writer.UpdateDocument(index, document);
break;
case LuceneOperation.Delete:
writer.DeleteDocuments(index);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
示例7: UpdateDoc
private void UpdateDoc(IndexWriter modifier, int id, int value)
{
Document doc = new Document();
doc.Add(NewTextField("content", "aaa", Field.Store.NO));
doc.Add(NewStringField("id", Convert.ToString(id), Field.Store.YES));
doc.Add(NewStringField("value", Convert.ToString(value), Field.Store.NO));
if (DefaultCodecSupportsDocValues())
{
doc.Add(new NumericDocValuesField("dv", value));
}
modifier.UpdateDocument(new Term("id", Convert.ToString(id)), doc);
}
示例8: TestFlushPushedDeletesByCount
public virtual void TestFlushPushedDeletesByCount()
{
Directory dir = NewDirectory();
// Cannot use RandomIndexWriter because we don't want to
// ever call commit() for this test:
int flushAtDelCount = AtLeast(1020);
IndexWriter w = new IndexWriter(dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random())).SetMaxBufferedDeleteTerms(flushAtDelCount).SetMaxBufferedDocs(1000).SetRAMBufferSizeMB(IndexWriterConfig.DISABLE_AUTO_FLUSH).SetMergePolicy(NoMergePolicy.NO_COMPOUND_FILES).SetReaderPooling(false));
int count = 0;
while (true)
{
Document doc = new Document();
doc.Add(new StringField("id", count + "", Field.Store.NO));
Term delTerm;
if (count == 1010)
{
// this is the only delete that applies
delTerm = new Term("id", "" + 0);
}
else
{
// These get buffered, taking up RAM, but delete
// nothing when applied:
delTerm = new Term("id", "x" + count);
}
w.UpdateDocument(delTerm, doc);
// Eventually segment 0 should get a del docs:
// TODO: fix this test
if (SlowFileExists(dir, "_0_1.del") || SlowFileExists(dir, "_0_1.liv"))
{
break;
}
count++;
if (count > flushAtDelCount)
{
Assert.Fail("delete's were not applied at count=" + flushAtDelCount);
}
}
w.Dispose();
dir.Dispose();
}
示例9: TestFlushPushedDeletesByRAM
public virtual void TestFlushPushedDeletesByRAM()
{
Directory dir = NewDirectory();
// Cannot use RandomIndexWriter because we don't want to
// ever call commit() for this test:
// note: tiny rambuffer used, as with a 1MB buffer the test is too slow (flush @ 128,999)
IndexWriter w = new IndexWriter(dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random())).SetRAMBufferSizeMB(0.1f).SetMaxBufferedDocs(1000).SetMergePolicy(NoMergePolicy.NO_COMPOUND_FILES).SetReaderPooling(false));
int count = 0;
while (true)
{
Document doc = new Document();
doc.Add(new StringField("id", count + "", Field.Store.NO));
Term delTerm;
if (count == 1010)
{
// this is the only delete that applies
delTerm = new Term("id", "" + 0);
}
else
{
// These get buffered, taking up RAM, but delete
// nothing when applied:
delTerm = new Term("id", "x" + count);
}
w.UpdateDocument(delTerm, doc);
// Eventually segment 0 should get a del docs:
// TODO: fix this test
if (SlowFileExists(dir, "_0_1.del") || SlowFileExists(dir, "_0_1.liv"))
{
if (VERBOSE)
{
Console.WriteLine("TEST: deletes created @ count=" + count);
}
break;
}
count++;
// Today we applyDeletes @ count=21553; even if we make
// sizable improvements to RAM efficiency of buffered
// del term we're unlikely to go over 100K:
if (count > 100000)
{
Assert.Fail("delete's were not applied");
}
}
w.Dispose();
dir.Dispose();
}
示例10: TestRollingUpdates_Mem
public virtual void TestRollingUpdates_Mem()
{
Random random = new Random(Random().Next());
BaseDirectoryWrapper dir = NewDirectory();
LineFileDocs docs = new LineFileDocs(random, DefaultCodecSupportsDocValues());
//provider.register(new MemoryCodec());
if ((!"Lucene3x".Equals(Codec.Default.Name)) && Random().NextBoolean())
{
Codec.Default =
TestUtil.AlwaysPostingsFormat(new MemoryPostingsFormat(Random().nextBoolean(), random.NextFloat()));
}
MockAnalyzer analyzer = new MockAnalyzer(Random());
analyzer.MaxTokenLength = TestUtil.NextInt(Random(), 1, IndexWriter.MAX_TERM_LENGTH);
IndexWriter w = new IndexWriter(dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, analyzer));
int SIZE = AtLeast(20);
int id = 0;
IndexReader r = null;
IndexSearcher s = null;
int numUpdates = (int)(SIZE * (2 + (TEST_NIGHTLY ? 200 * Random().NextDouble() : 5 * Random().NextDouble())));
if (VERBOSE)
{
Console.WriteLine("TEST: numUpdates=" + numUpdates);
}
int updateCount = 0;
// TODO: sometimes update ids not in order...
for (int docIter = 0; docIter < numUpdates; docIter++)
{
Documents.Document doc = docs.NextDoc();
string myID = "" + id;
if (id == SIZE - 1)
{
id = 0;
}
else
{
id++;
}
if (VERBOSE)
{
Console.WriteLine(" docIter=" + docIter + " id=" + id);
}
((Field)doc.GetField("docid")).StringValue = myID;
Term idTerm = new Term("docid", myID);
bool doUpdate;
if (s != null && updateCount < SIZE)
{
TopDocs hits = s.Search(new TermQuery(idTerm), 1);
Assert.AreEqual(1, hits.TotalHits);
doUpdate = !w.TryDeleteDocument(r, hits.ScoreDocs[0].Doc);
if (VERBOSE)
{
if (doUpdate)
{
Console.WriteLine(" tryDeleteDocument failed");
}
else
{
Console.WriteLine(" tryDeleteDocument succeeded");
}
}
}
else
{
doUpdate = true;
if (VERBOSE)
{
Console.WriteLine(" no searcher: doUpdate=true");
}
}
updateCount++;
if (doUpdate)
{
w.UpdateDocument(idTerm, doc);
}
else
{
w.AddDocument(doc);
}
if (docIter >= SIZE && Random().Next(50) == 17)
{
if (r != null)
{
r.Dispose();
}
bool applyDeletions = Random().NextBoolean();
if (VERBOSE)
{
Console.WriteLine("TEST: reopen applyDeletions=" + applyDeletions);
}
//.........这里部分代码省略.........
示例11: TestOperationsOnDiskFull
//.........这里部分代码省略.........
}
if (debug)
{
System.Console.Out.WriteLine("\ncycle: " + diskFree + " bytes");
}
testName = "disk full during reader.close() @ " + thisDiskFree + " bytes";
}
else
{
thisDiskFree = 0;
rate = 0.0;
if (debug)
{
System.Console.Out.WriteLine("\ncycle: same writer: unlimited disk space");
}
testName = "reader re-use after disk full";
}
dir.SetMaxSizeInBytes(thisDiskFree);
dir.SetRandomIOExceptionRate(rate, diskFree);
try
{
if (0 == x)
{
int docId = 12;
for (int i = 0; i < 13; i++)
{
if (updates)
{
Document d = new Document();
d.Add(new Field("id", System.Convert.ToString(i), Field.Store.YES, Field.Index.UN_TOKENIZED));
d.Add(new Field("content", "bbb " + i, Field.Store.NO, Field.Index.TOKENIZED));
modifier.UpdateDocument(new Term("id", System.Convert.ToString(docId)), d);
}
else
{
// deletes
modifier.DeleteDocuments(new Term("id", System.Convert.ToString(docId)));
// modifier.setNorm(docId, "contents", (float)2.0);
}
docId += 12;
}
}
modifier.Close();
success = true;
if (0 == x)
{
done = true;
}
}
catch (System.IO.IOException e)
{
if (debug)
{
//UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
System.Console.Out.WriteLine(" hit IOException: " + e);
System.Console.Out.WriteLine(e.StackTrace);
}
err = e;
if (1 == x)
{
System.Console.Error.WriteLine(e.StackTrace);
Assert.Fail(testName + " hit IOException after disk space was freed up");
}
}
示例12: WriteToIndex
private void WriteToIndex(ContentItem item, IndexWriter iw)
{
var doc = CreateDocument(item);
if (doc != null)
{
iw.UpdateDocument(new Term(Properties.ID, item.ID.ToString()), doc);
iw.Commit();
}
}
示例13: btnUpdateDoc_Click
private void btnUpdateDoc_Click(object sender, EventArgs e)
{
try
{
string path = System.Configuration.ConfigurationSettings.AppSettings["Data"].ToString();
Analyzer analyzer = new StockFooAnalyzer(path);
FSDirectory dy = FSDirectory.Open(new DirectoryInfo(System.Configuration.ConfigurationSettings.AppSettings["IndexDirectory"].ToString()));
IndexWriter indexWriter = new IndexWriter(dy, analyzer, false);
IOpen oldopen = CurrentIndex.GetCurrentOpendIndex();
Document olddoc = oldopen.Reader.Document(Convert.ToInt32(textBox1.Text));
string oldurl = olddoc.Get("url").ToString();
bool oldisnew = false;
if (oldurl.Trim().Equals(txturl.Text.Trim()))
oldisnew = true;
Document document = new Document();
Field ftitle = new Field("title", txttitle.Text, Field.Store.YES, Field.Index.ANALYZED);//存储,索引
document.Add(ftitle);
Field furl = new Field("url", txturl.Text, Field.Store.YES, Field.Index.NOT_ANALYZED);//存储,不索引
document.Add(furl);
Field fsite = new Field("site", txtsite.Text, Field.Store.YES, Field.Index.NOT_ANALYZED);//存储,不索引
document.Add(fsite);
Field fbody = new Field("body", txtbody.Text, Field.Store.YES, Field.Index.ANALYZED);//存储,索引
document.Add(fbody);
Field fpublishtime = new Field("publish_time", txtpublishtime.Text, Field.Store.YES, Field.Index.NOT_ANALYZED);//存储,不索引
document.Add(fpublishtime);
Term term = new Term("url", txturl.Text);
indexWriter.UpdateDocument(term, document, analyzer);
indexWriter.Optimize();
indexWriter.Close();
IndexOpen open = new IndexOpen(System.Configuration.ConfigurationSettings.AppSettings["IndexDirectory"].ToString(), false);
bool isOpend = open.Open();
if (isOpend)
{
CurrentIndex.SetCurrentOpendIndex(open);
}
if (!oldisnew)
{
FindDocuemnt(0);
label9.Text = "当前为文档1";
textBox1.Text = "0";
docNum = docNum + 1;
label1.Text = string.Format("共有文档{0}条", docNum);
}
else
{
docNum = open.Reader.MaxDoc();
FindDocuemnt(docNum - 1);
label9.Text = "当前为文档" + (docNum).ToString();
textBox1.Text = (docNum - 1).ToString();
}
MessageBox.Show("更新成功!");
}
catch (Exception ex)
{
MessageBox.Show("更新失败!\\n"+ex.Message);
}
}
示例14: UpdateDoc
private void UpdateDoc(IndexWriter modifier, int id, int value_Renamed)
{
Document doc = new Document();
doc.Add(new Field("content", "aaa", Field.Store.NO, Field.Index.ANALYZED));
doc.Add(new Field("id", System.Convert.ToString(id), Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.Add(new Field("value", System.Convert.ToString(value_Renamed), Field.Store.NO, Field.Index.NOT_ANALYZED));
modifier.UpdateDocument(new Term("id", System.Convert.ToString(id)), doc);
}
示例15: DoTestOperationsOnDiskFull
//.........这里部分代码省略.........
dir.RandomIOExceptionRateOnOpen = Random().NextDouble() * 0.01;
}
else
{
thisDiskFree = 0;
rate = 0.0;
if (VERBOSE)
{
Console.WriteLine("\ncycle: same writer: unlimited disk space");
}
testName = "reader re-use after disk full";
dir.RandomIOExceptionRateOnOpen = 0.0;
}
dir.MaxSizeInBytes = thisDiskFree;
dir.RandomIOExceptionRate = rate;
try
{
if (0 == x)
{
int docId = 12;
for (int i = 0; i < 13; i++)
{
if (updates)
{
Document d = new Document();
d.Add(NewStringField("id", Convert.ToString(i), Field.Store.YES));
d.Add(NewTextField("content", "bbb " + i, Field.Store.NO));
if (DefaultCodecSupportsDocValues())
{
d.Add(new NumericDocValuesField("dv", i));
}
modifier.UpdateDocument(new Term("id", Convert.ToString(docId)), d);
} // deletes
else
{
modifier.DeleteDocuments(new Term("id", Convert.ToString(docId)));
// modifier.setNorm(docId, "contents", (float)2.0);
}
docId += 12;
}
}
modifier.Dispose();
success = true;
if (0 == x)
{
done = true;
}
}
catch (IOException e)
{
if (VERBOSE)
{
Console.WriteLine(" hit IOException: " + e);
Console.WriteLine(e.StackTrace);
}
err = e;
if (1 == x)
{
Console.WriteLine(e.ToString());
Console.Write(e.StackTrace);
Assert.Fail(testName + " hit IOException after disk space was freed up");
}
}
// prevent throwing a random exception here!!