About the problem of distinct under the field obtained by Lucene.net when querying
See original GitHub issueI’m sorry to ask here, I really can’t find how to use the distinct function on lucene.net4.8 beta16. My idea is to construct query conditions first, then filter, and finally get the result of the query as Distinct For example, the current query field results are: 1,1,1,1,1,5,6,7,8,4,5,6,4,5,6,5,5,4 Distinct result: 1, 5, 6, 7, 8, 4
I read Lucene.net.grouping on the official website But the demo can’t run normally, I’m helpless, looking forward to your reply. Thanks!
This is my current code and the performance is horrible
`public static SearchFilterList SearchFilter(string keyword, int brandId, string spec = “”, string model = “”, bool isCas = false) { string[] fileds = { “name”, “supplierNo”, “alias”, “engname” }; BooleanQuery bq = new BooleanQuery();
if (keyword.IsNullOrEmpty() && brandId > 0)
{
bq.Add(new TermQuery(new Term("brand", brandId.ToString())), Occur.MUST);
}
else if (!isCas)
{
var query = MultiFieldQueryParser.Parse(version, GetKeyword(keyword), fileds, new Occur[] { Occur.SHOULD, Occur.SHOULD, Occur.SHOULD, Occur.SHOULD }, analyzer);
bq.Add(query, Occur.SHOULD);
foreach (var item in fileds)
{
bq.Add(new RegexpQuery(new Term(item, $".*{keyword}.*")), Occur.SHOULD);
}
}
else
{
bq.Add(new TermQuery(new Term("cas", keyword)), Occur.MUST);
}
BooleanFilter filter = null;
if (brandId > 0)
{
if (filter == null) filter = new BooleanFilter();
filter.Add(new FilterClause(new FieldCacheTermsFilter("brand", new[] { brandId.ToString() }), Occur.MUST));
}
if (!string.IsNullOrEmpty(spec))
{
if (filter == null) filter = new BooleanFilter();
filter.Add(new FilterClause(new FieldCacheTermsFilter("spec", new[] { spec }), Occur.MUST));
}
if (!string.IsNullOrEmpty(model))
{
if (filter == null) filter = new BooleanFilter();
filter.Add(new FilterClause(new FieldCacheTermsFilter("model", new[] { model }), Occur.MUST));
}
IndexSearcher searcher = new IndexSearcher(DirectoryReader.Open(direcotry));
TopDocs docs = searcher.Search(bq, filter, 10000);
if (docs == null)
{
return null;
}
ConcurrentQueue<int> brands = new ConcurrentQueue<int>();
ConcurrentQueue<string> specs = new ConcurrentQueue<string>();
ConcurrentQueue<string> models = new ConcurrentQueue<string>();
Parallel.ForEach(docs.ScoreDocs, new ParallelOptions() { MaxDegreeOfParallelism = 8 }, sd =>
{
try
{
Document doc = searcher.Doc(sd.Doc);
brands.Enqueue(doc.Get("brand").ToInt32());
var specValue = doc.Get("spec");
if (!string.IsNullOrEmpty(specValue))
{
specs.Enqueue(specValue);
}
var modelValue = doc.Get("model");
if (!string.IsNullOrEmpty(modelValue))
{
models.Enqueue(modelValue);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
});
var dto = new Json.Dto.SearchFilterList()
{
Brands = brands.Distinct().AsParallel(),
Specifications = specs.Distinct().AsParallel(),
Models = models.Distinct().AsParallel()
};
return dto;
}`
Issue Analytics
- State:
- Created a year ago
- Comments:8 (4 by maintainers)
this is not an issue, and this is not questions & answers site. if you have questions, please ask them on stackoverflow
@NightOwl888, please close. thanks.
Sorry I didn’t find any clue you said, here I am not using groupingsearch but boolean query,groupingsearch use filter data item,It has been done with your help,and I want to sort by similarity, but I can’t find it(This is a new query, groupingsearch is not used, sorry my english is not very good)