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

  • SEARCH
  • Home
  • 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 6627605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:02:01+00:00 2026-05-25T22:02:01+00:00

I have the following program: public class Hit { readonly Hits _hits; readonly int

  • 0

I have the following program:

    public class Hit
    {
        readonly Hits _hits;
        readonly int _index;

        public Hit(Hits hits, int index)
        {
            this._hits = hits;
            this._index = index;
        }

        public int id { get { return _hits.Id(_index); } }
        public float score { get { return _hits.Score(_index); } }
        public string this[string key] { get { return _hits.Doc(_index).Get(key); } }
    }

    class HitList : IList<Hit>
    {
        protected Hits hits;

        public HitList(Hits hits)
        {
            this.hits = hits;
        }

        #region IList Members
        public int Add(object value) { throw new NotImplementedException(); }
        public void Clear() { throw new NotImplementedException(); }
        public bool Contains(object value) { throw new NotImplementedException(); }
        public int IndexOf(object value) { throw new NotImplementedException(); }
        public void Insert(int index, object value) { throw new NotImplementedException(); }
        public bool IsFixedSize { get { throw new NotImplementedException(); } }
        public bool IsReadOnly { get { throw new NotImplementedException(); } }
        public void Remove(object value) { throw new NotImplementedException(); }
        public void RemoveAt(int index) { throw new NotImplementedException(); }
        public object this[int index] { get { return new Hit(hits, index); } set { throw new NotImplementedException(); } }
        #endregion

        #region ICollection Members
        public void CopyTo(Array array, int index) { throw new NotImplementedException(); }
        public int Count { get { return hits.Length(); } }
        public bool IsSynchronized { get { throw new NotImplementedException(); } }
        public object SyncRoot { get { throw new NotImplementedException(); } }
        #endregion

        #region IEnumerable Members
        public System.Collections.IEnumerator GetEnumerator() { throw new NotImplementedException(); }
        #endregion

        #region IList<Hit> Members
        public int IndexOf(Hit item) { throw new NotImplementedException(); }
        public void Insert(int index, Hit item) { throw new NotImplementedException(); }
        Hit IList<Hit>.this[int index] { get { return new Hit(hits, index); } set { throw new NotImplementedException(); } }
        #endregion

        #region ICollection<Hit> Members
        public void Add(Hit item) { throw new NotImplementedException(); }
        public bool Contains(Hit item) { throw new NotImplementedException(); }
        public void CopyTo(Hit[] array, int arrayIndex) { throw new NotImplementedException(); }
        public bool Remove(Hit item) { throw new NotImplementedException(); }
        #endregion

        #region IEnumerable<Hit> Members
        IEnumerator<Hit> IEnumerable<Hit>.GetEnumerator() { throw new NotImplementedException(); }
        #endregion
    }
    private const string IndexFileLocation = @"C:\Users\Public\Index";
    private IList<Hit> _hits;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Lucene.Net.Store.Directory dir = Lucene.Net.Store.FSDirectory.GetDirectory(IndexFileLocation, true);

        Lucene.Net.Analysis.Analyzer analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer();
        var indexWriter = new Lucene.Net.Index.IndexWriter(dir, analyzer, true);


        for (var i = 0; i < 10; i++)
        {
            var doc = new Lucene.Net.Documents.Document();
            var fldContent = new Lucene.Net.Documents.Field("content", "test " + i,
                                                             Lucene.Net.Documents.Field.Store.YES,
                                                             Lucene.Net.Documents.Field.Index.TOKENIZED,
                                                             Lucene.Net.Documents.Field.TermVector.YES);
            doc.Add(fldContent);
            indexWriter.AddDocument(doc);
        }
        indexWriter.Optimize();
        indexWriter.Close();

        var searcher = new Lucene.Net.Search.IndexSearcher(dir);
        var searchTerm = new Lucene.Net.Index.Term("content", "test");
        Lucene.Net.Search.Query query = new Lucene.Net.Search.TermQuery(searchTerm);

        Lucene.Net.Search.Hits hits = searcher.Search(query);
        for (var i = 0; i < hits.Length(); i++)
        {
            Document doc = hits.Doc(i);
            string contentValue = doc.Get("content");

            Debug.WriteLine(contentValue);
        }
        HitList h = new HitList(hits);

        h.Shuffle();

        for (var i = 0; i < h.Count; i++)
        {
            var z = (Hit)h[i];
            string contentValue = z.id.ToString();

            Debug.WriteLine(contentValue);
        }
    }
}

public static class SiteItemExtensions
{
    public static void Shuffle<T>(this IList<T> list)
    {
        var rng = new Random();
        int n = list.Count;
        while (n > 1)
        {
            n--;
            int k = rng.Next(n + 1);
            T value = list[k];
            list[k] = list[n];
            list[n] = value;
        }
    }
}

What I am trying to do is “shuffle” the results I get back from the Hits collection. When I run this program, as is, it bombs when I get to the h.Shuffle(); line. I understand why its bombing. Its bombing because its executing my Shuffle extension method, when in turn, is trying to do a set operation on an array value and I do not have a set implementation on the public object this[int index] line.

My problem is, I can’t implement a set because the Lucene id and score properties are read only, which, again, makes sense why Apache made them read only. My question is, how can I “shuffle” or randomize the Hits that I’m getting back? Any help would be appreciated.

  • 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-25T22:02:02+00:00Added an answer on May 25, 2026 at 10:02 pm

    You need to copy your hits to an appropiate data structure and do your sorting there; the underlying problem is that the Hits type is not intended for modification.

    For the shuffling, I believe this should do the trick:

    var shuffledHits = hits.Cast<Hit>().OrderBy(h => rng.Next());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following in a program (written in VB.NET): Imports Microsoft.Office.Interop.Excel Public Class
I have the following code: using System; using System.Linq; using System.Linq.Expressions; public class Program
I have the following code: public class Foo {} static class Program { [XmlElement(foo)]
I have the following program import java.util.*; public class Test { public static void
If you have the following: Public Class Person { public int Id; Public String
I have the following example relationship: namespace Yesod { public class Program { //
I have the following class in my program: public class RZoom extends Activity {
I have the following code : public class MyClass { private readonly string name;
I have the following program: ~/test> cat test.cc int main() { int i =
I have this following simple service program: using System.Diagnostics; using System.ServiceProcess; namespace BasicService {

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.