Giter Club home page Giter Club logo

Comments (1)

NightOwl888 avatar NightOwl888 commented on June 3, 2024

Maybe I am missing something, but why aren't you using the built-in string comparer (TermOrdValComparer) with reverse support?

/// <summary>
/// Tests reverse sorting on type string with a missing
/// value sorted last
/// </summary>
[Test]
public virtual void TestStringValMissingSortedLastReverse()
{
Directory dir = NewDirectory();
RandomIndexWriter writer = new RandomIndexWriter(Random, dir);
Document doc = new Document();
writer.AddDocument(doc);
doc = new Document();
doc.Add(NewStringField("value", "foo", Field.Store.YES));
writer.AddDocument(doc);
doc = new Document();
doc.Add(NewStringField("value", "bar", Field.Store.YES));
writer.AddDocument(doc);
IndexReader ir = writer.GetReader();
writer.Dispose();
IndexSearcher searcher = NewSearcher(ir);
SortField sf = new SortField("value", SortFieldType.STRING, true);
sf.SetMissingValue(SortField.STRING_LAST);
Sort sort = new Sort(sf);
TopDocs td = searcher.Search(new MatchAllDocsQuery(), 10, sort);
Assert.AreEqual(3, td.TotalHits);
// null comes first
Assert.IsNull(searcher.Doc(td.ScoreDocs[0].Doc).Get("value"));
Assert.AreEqual("foo", searcher.Doc(td.ScoreDocs[1].Doc).Get("value"));
Assert.AreEqual("bar", searcher.Doc(td.ScoreDocs[2].Doc).Get("value"));
ir.Dispose();
dir.Dispose();
}

/// <summary>
/// Tests reverse sorting on type string with a missing
/// value sorted first
/// </summary>
[Test]
public virtual void TestStringMissingSortedFirstReverse()
{
Directory dir = NewDirectory();
RandomIndexWriter writer = new RandomIndexWriter(Random, dir);
Document doc = new Document();
writer.AddDocument(doc);
doc = new Document();
doc.Add(NewStringField("value", "foo", Field.Store.YES));
writer.AddDocument(doc);
doc = new Document();
doc.Add(NewStringField("value", "bar", Field.Store.YES));
writer.AddDocument(doc);
IndexReader ir = writer.GetReader();
writer.Dispose();
IndexSearcher searcher = NewSearcher(ir);
SortField sf = new SortField("value", SortFieldType.STRING, true);
Sort sort = new Sort(sf);
TopDocs td = searcher.Search(new MatchAllDocsQuery(), 10, sort);
Assert.AreEqual(3, td.TotalHits);
Assert.AreEqual("foo", searcher.Doc(td.ScoreDocs[0].Doc).Get("value"));
Assert.AreEqual("bar", searcher.Doc(td.ScoreDocs[1].Doc).Get("value"));
// null comes last
Assert.IsNull(searcher.Doc(td.ScoreDocs[2].Doc).Get("value"));
ir.Dispose();
dir.Dispose();
}

If the custom FieldComparer is required, your main issue is that you are allocating memory inside of the comparison methods. Comparers should only allocate memory in the class constructor and/or field initializers, never in the comparison methods, because this will cause a lot of garbage collection overhead.

    public override void Copy(int slot, int doc)
    {
        termCopy = new BytesRef(); // Expensive allocation. This method will be called a lot.
        sortedResults.Get(doc, termCopy);
        bvalues[slot] = termCopy;
    }

    public override int CompareBottom(int doc)
    {
        BytesRef termOrd = new BytesRef(); // Expensive allocation. This method will be called a lot.
        int ord = sortedResults.GetOrd(doc);
        sortedResults.LookupOrd(ord, termOrd); // Expensive lookup. The TermOrdValComparer only does lookups in the Copy() method and only when necessary.
        var result = DoCompare(bvalues[bottomSlot], termOrd);

        return result;
    }

Also, the TermOrdValComparer uses ords to do the comparison in most cases, falling back to term comparison only when necessary. See the TermOrdValComparer docs and FieldComparer<T> docs.

from lucenenet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.