In Lucene.Net how to pass object of parameters to my FieldComparer

I have the following structure/code that I’m trying to fix one issue in it. It could have been done differently from the start but it is what it is.

When searching to get all employees by department id, the employees should be returned in the search with ASC order of the tag except when the tag is null, which should send it to the bottom of the search.

this is the relationship of the structure

Employee Department ID Department Name Tag
Sam 101 Accounting “aaa”
Sam 102 Shipping Null
Tim 101 Accounting “eee”
Tim 102 Shipping “zzz”

and json view:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{
"Name": "Sam",
"Departments": [
{
"ID": 101,
"Name": "Accounting",
"Tag": "aaa"
},
{
"ID": 102,
"Name": "Shipping",
"Tag": Null
},
]
},
{
"Name": "Tim",
"Departments" : [
{
"ID": 101,
"Name": "Accounting",
"Tag": "eee"
},
{
"ID": 102,
"Name": "Shipping",
"Tag": "zzz"
},
]
}
</code>
<code>{ "Name": "Sam", "Departments": [ { "ID": 101, "Name": "Accounting", "Tag": "aaa" }, { "ID": 102, "Name": "Shipping", "Tag": Null }, ] }, { "Name": "Tim", "Departments" : [ { "ID": 101, "Name": "Accounting", "Tag": "eee" }, { "ID": 102, "Name": "Shipping", "Tag": "zzz" }, ] } </code>
{
    "Name": "Sam",
    "Departments": [
        {
            "ID": 101,
            "Name": "Accounting",
            "Tag": "aaa"
        },
        {
            "ID": 102,
            "Name": "Shipping",
            "Tag": Null
        },
    ]
  },
  {
    "Name": "Tim",
    "Departments" : [
        {
            "ID": 101,
            "Name": "Accounting",
            "Tag": "eee"
        },
        {
            "ID": 102,
            "Name": "Shipping",
            "Tag": "zzz"
        },
    ]
  }

So, if I’m searching for all employess that belong to account department (101), since Sam has a value for tagNumber “aaa” and it is preceding Tim’s whose tag is: “eee” then Sam should return first.
But if we are searching for Shipping department (102), Sam’s tagnumber in that department is null; and Tim’s has a tagnumber value for that department, then Tim should show up first.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>When searching by Accounting ID (101) ASC --> Tag number determines order (aaa is before eee)
Sam
Tim
When searching by Shipping ID (102) ASC --> Tag number Null goes to bottom. zzz has precedence over null and therfore, Tim should show up first
Tim
Sam
</code>
<code>When searching by Accounting ID (101) ASC --> Tag number determines order (aaa is before eee) Sam Tim When searching by Shipping ID (102) ASC --> Tag number Null goes to bottom. zzz has precedence over null and therfore, Tim should show up first Tim Sam </code>
When searching by Accounting ID (101) ASC --> Tag number determines order (aaa is before eee)
   Sam
   Tim
When searching by Shipping ID (102) ASC --> Tag number Null goes to bottom. zzz has precedence over null and therfore, Tim should show up first
   Tim
   Sam

This is how the docs and fields are created currently:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>var doc = new Document();
doc.Add(new Field("Name", "Sam", fieldStored));
doc.Add(new Field("Department.Id", 101, fieldStored));
doc.Add(new Field("Department.Name", "Accounting", fieldStored));
doc.Add(new Field("Department.Tag", "aaa", fieldStored));
doc.Add(new Field("Department.Id", 102, fieldStored));
doc.Add(new Field("Department.Name", "Shipping", fieldStored));
doc.Add(new Field("Department.Tag", null, fieldStored));
var doc = new Document();
doc.Add(new Field("Name", "Tim", fieldStored));
doc.Add(new Field("Department.Id", 101, fieldStored));
doc.Add(new Field("Department.Name", "Accounting", fieldStored));
doc.Add(new Field("Department.Tag", "eee", fieldStored));
doc.Add(new Field("Department.Id", 102, fieldStored));
doc.Add(new Field("Department.Name", "Shipping", fieldStored));
doc.Add(new Field("Department.Tag", "zzz", fieldStored));
</code>
<code>var doc = new Document(); doc.Add(new Field("Name", "Sam", fieldStored)); doc.Add(new Field("Department.Id", 101, fieldStored)); doc.Add(new Field("Department.Name", "Accounting", fieldStored)); doc.Add(new Field("Department.Tag", "aaa", fieldStored)); doc.Add(new Field("Department.Id", 102, fieldStored)); doc.Add(new Field("Department.Name", "Shipping", fieldStored)); doc.Add(new Field("Department.Tag", null, fieldStored)); var doc = new Document(); doc.Add(new Field("Name", "Tim", fieldStored)); doc.Add(new Field("Department.Id", 101, fieldStored)); doc.Add(new Field("Department.Name", "Accounting", fieldStored)); doc.Add(new Field("Department.Tag", "eee", fieldStored)); doc.Add(new Field("Department.Id", 102, fieldStored)); doc.Add(new Field("Department.Name", "Shipping", fieldStored)); doc.Add(new Field("Department.Tag", "zzz", fieldStored)); </code>
var doc = new Document();
doc.Add(new Field("Name", "Sam", fieldStored));
doc.Add(new Field("Department.Id", 101, fieldStored));
doc.Add(new Field("Department.Name", "Accounting", fieldStored));
doc.Add(new Field("Department.Tag", "aaa", fieldStored));
doc.Add(new Field("Department.Id", 102, fieldStored));
doc.Add(new Field("Department.Name", "Shipping", fieldStored));
doc.Add(new Field("Department.Tag", null, fieldStored));

var doc = new Document();
doc.Add(new Field("Name", "Tim", fieldStored));
doc.Add(new Field("Department.Id", 101, fieldStored));
doc.Add(new Field("Department.Name", "Accounting", fieldStored));
doc.Add(new Field("Department.Tag", "eee", fieldStored));
doc.Add(new Field("Department.Id", 102, fieldStored));
doc.Add(new Field("Department.Name", "Shipping", fieldStored));
doc.Add(new Field("Department.Tag", "zzz", fieldStored));

My search is executed as follows in the TagComparer class

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class TagComparator : FieldComparer<BytesRef>
{
private readonly BytesRef[] bvalues;
..........................
public override FieldComparer SetNextReader(AtomicReaderContext context)
{
sortedResults = FieldCache.DEFAULT.GetTermsIndex(context.AtomicReader, field);
return this;
}
public override void Copy(int slot, int doc)
{
termCopy = new BytesRef();
sortedResults.Get(doc, termCopy);
bvalues[slot] = termCopy;
}
}
</code>
<code>public class TagComparator : FieldComparer<BytesRef> { private readonly BytesRef[] bvalues; .......................... public override FieldComparer SetNextReader(AtomicReaderContext context) { sortedResults = FieldCache.DEFAULT.GetTermsIndex(context.AtomicReader, field); return this; } public override void Copy(int slot, int doc) { termCopy = new BytesRef(); sortedResults.Get(doc, termCopy); bvalues[slot] = termCopy; } } </code>
public class TagComparator : FieldComparer<BytesRef>
{
    private readonly BytesRef[] bvalues;
    ..........................
    public override FieldComparer SetNextReader(AtomicReaderContext context)
    {
        sortedResults = FieldCache.DEFAULT.GetTermsIndex(context.AtomicReader, field);
        return this;
    }

    public override void Copy(int slot, int doc)
    {
        termCopy = new BytesRef();
        sortedResults.Get(doc, termCopy);
        bvalues[slot] = termCopy;
    }
}

ISSUE SUMMARY:
Right now, it returns “Sam” before Tim for Shipping Department (102) even though Sam’s tag number is Null and “Tim” later even though he has value of the tagnumber for that department “zzz”.

MY ATTEMPT:
Since my servers have many resources allocated, I can sacrifice a little on the search speed by adding more code to the Copy override method. The solution will mean:

  1. store the department id before searcher code takes over

  2. Copy method will execute once for every hit of user who belong to that department

  3. get the whole doc of the hit that shows department id and tagnumber value

  4. if tagnumber has legit value, it shows normall, if not, it shows in the bottom

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code> for (int i = 0; i < wholeDoc.GetValues("Department.Id").Length; i++)
    {
    if(int.Parse(wholeDoc.GetValues("Department.Id")[i]) == 1234) // --> SOLUTION
    {
    string tagNumber= wholeDoc.GetValues("TagNumber")[i];
    if (string.IsNullOrWhiteSpace(tagNumber))
    SetBottom(doc);
    else
    bvalues[slot] = new BytesRef(wholeDoc.GetValues("TagNumber")[i]);
    break;
    }
    }
    </code>
    <code> for (int i = 0; i < wholeDoc.GetValues("Department.Id").Length; i++) { if(int.Parse(wholeDoc.GetValues("Department.Id")[i]) == 1234) // --> SOLUTION { string tagNumber= wholeDoc.GetValues("TagNumber")[i]; if (string.IsNullOrWhiteSpace(tagNumber)) SetBottom(doc); else bvalues[slot] = new BytesRef(wholeDoc.GetValues("TagNumber")[i]); break; } } </code>
         for (int i = 0; i < wholeDoc.GetValues("Department.Id").Length; i++)
         {
             if(int.Parse(wholeDoc.GetValues("Department.Id")[i]) == 1234) // --> SOLUTION
             {
                 string tagNumber= wholeDoc.GetValues("TagNumber")[i];
                 if (string.IsNullOrWhiteSpace(tagNumber))
                     SetBottom(doc);
                 else 
                     bvalues[slot] = new BytesRef(wholeDoc.GetValues("TagNumber")[i]);
                 break;
             }
         }
    

This solution works (I’ve tested it against small subset of data) But the problem is with step #1. I don’t know what the Department.Id will be each time for the search. Once the search library codes executes, I couldn’t find a way to remember what department id was used. I’ve tried session variables, but they don’t work with external libraries. This is the last line that know of the department id being passed, but after that it is darkness. How do I pass the department id beyong this line?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>var topDocs = searcher.Search(query, filter, (pageNumber * recordsPerPage), sort);
</code>
<code>var topDocs = searcher.Search(query, filter, (pageNumber * recordsPerPage), sort); </code>
var topDocs = searcher.Search(query, filter, (pageNumber * recordsPerPage), sort);

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật