Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6736535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:08:27+00:00 2026-05-26T11:08:27+00:00

I have the following code I wrote: Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_29); IndexWriter writer

  • 0

I have the following code I wrote:

        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_29);
        IndexWriter writer = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);
        Document doc = new Document();

        foreach (string fieldName in Request.Form)
        {
            if (fieldName == "channelID" && string.IsNullOrEmpty(Request["channelID"]))
            {
                List<long> channelIDS = new List<long>();
                IndexReader indexReader = IndexReader.Open(directory, true);
                TermEnum te = indexReader.Terms(new Term("ID"));
                do
                {
                    Term t = te.Term();
                    if (t == null || t.Field() != "channelID") break;
                    channelIDS.Add(long.Parse(t.Text()));
                } while (te.Next());
                te.Close();

                long nextAvailable = channelIDS.Concat(new[] { long.MaxValue })
                                            .OrderBy(x => x)
                                            .Select((value, index) => new { value, index })
                                            .Where(pair => pair.value != pair.index)
                                            .Select(pair => pair.index)
                                            .First();
                doc.Add(new Field(fieldName, nextAvailable.ToString(), Field.Store.YES, Field.Index.ANALYZED));
            }
            else
            {
                doc.Add(new Field(fieldName, Request.Form[fieldName], Field.Store.YES, Field.Index.ANALYZED));
            }
        }

        writer.AddDocument(doc);
        writer.Optimize();
        writer.Close();

The document already in the index has a channelID with a value of 0, and all other fields empty.

I have verified that the data fed to the document has a different channelID (even due all the other fields are empty, same as the existing document.

For some reason, the code updates the single document I already have in Lucene,
instead of adding a new one… what am I missing here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:08:27+00:00Added an answer on May 26, 2026 at 11:08 am

    In your code, you are re-creating the index with every run/method-call. So your previous index is deleted.

    IndexWriter writer = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);


    BTW, terms are stored sorted in lucene index. You can do your calculations while enumerating the terms.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: MemoryStream foo(){ MemoryStream ms = new MemoryStream(); // write
I have following code class User attr_accessor :name end u = User.new u.name =
I have the following code and would appreciate your advice. QueryParser queryParser = new
I have the following code I wrote in Asp.NET and I am trying to
I have following code to serialize my data into a file: out = new
i have wrote the following polymorphic code: public abstract class A { readonly int
I have wrote the following code to get the offset of a tuple element
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following Code Block Which I tried to optimize in the Optimized section
I have following code in my application: // to set tip - photo in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.