2022-10-21

lucene.net 4.8 custom sorting according to string length

How can I sort by string length?
For example, I use lucene.net to search, the keyword is "toluene", then the name of "toluene" is in the front, and the name containing "toluene" is in the back, which makes me very troubled.

This is my search:

var query = MultiFieldQueryParser.Parse(version, GetKeyword(keyword), fileds, new Occur[] { Occur.SHOULD, Occur.SHOULD, Occur.SHOULD, Occur.SHOULD }, analyzer);

foreach (var item in fileds)
{
    var regexpQuery = new RegexpQuery(new Term(item, $".*{keyword}.*"));

    bq.Add(regexpQuery, Occur.SHOULD);
}

Sort sort = new Sort(new SortField("name", SortFieldType.SCORE, false));

Image



No comments:

Post a Comment