传递动态过滤器以在Lucene Net C#中创建索引

时间:2016-01-25 12:47:21

标签: c# .net lucene lucene.net

我已经从我的项目中创建了Lucene索引&查询&如果条件过滤器是硬编码的,则过滤工作,因此在搜索它时只获得匹配的过滤字和&因为我要去搜索。现在我想根据我的要求传递过滤器&我不想硬编码过滤条件。例如,我将通过过滤器,如学生应该属于特定部门&他/她应该被特别采取使用sql的一些科目写作

select * from dbo.[Student] where departmentId=1 
                            and subject ='history'

和IO使用binaryquery实现了这一点。现在我想通过像

这样的过滤器
select * from dbo.[Student] where departmentId=1 
                            and subject in('history','general') 
                            and (class='5sem' or class='6sem')

select * from dbo.[Student] where subject in('history','general') 
                            and departmentId=1 and (class='5sem' or class='6sem')


搜索方法

public DataSet Search(字符串条件,字符串术语,字符串过滤器){
           Lucene.Net.Store.Directory目录= FSDirectory.Open(新的DirectoryInfo(@" D:\ LuceneIndex"));             Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);

        BooleanQuery bq = new BooleanQuery();

        IndexReader indexReader = IndexReader.Open(directory, true);
        Searcher searcher = new IndexSearcher(indexReader);

        var queryParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, criterion, analyzer);

        queryParser.AllowLeadingWildcard = true;

        var query = queryParser.Parse(term.ToLower() + "*");

        bq.Add(query, Occur.MUST);
        foreach (string str in filter.Split(','))
        {
            if (str.Split('$').Length == 3)
            {
                if (str.Split('$')[1] == "=")
                {
                    bq.Add(new TermQuery(new Term(str.Split('$')[0], (str.Split('$')[2]).ToLower())), Occur.MUST);
                }
                else if (str.Split('$')[1] == "<>")
                {
                    bq.Add(new TermQuery(new Term(str.Split('$')[0], (str.Split('$')[2]).ToLower())), Occur.MUST_NOT);
                }
            }
        }

        TopDocs resultDocs = searcher.Search(bq, null, indexReader.MaxDoc, Sort.RELEVANCE);

        var topDocs = resultDocs.ScoreDocs;

        DataSet Students = new DataSet();
        DataTable dt = new DataTable();
        dt.Columns.Add("UAN");
        dt.Columns.Add("Firstname");
        DataRow row = null;
        foreach (var hit in topDocs)
        {
            var documentFromSerach = searcher.Doc(hit.Doc);
            row = dt.NewRow();
            row["UAN"] = documentFromSerach.Get("UAN");
            row["FirstName"] = documentFromSerach.Get("FirstName");
            dt.Rows.Add(row);
        }
        Students.Tables.Add(dt);

        return Students;
    }


在Seach按钮上单击
StudentLuceneIndex.StudentLuceneIndexSearch obj = new StudentLuceneIndex.StudentLuceneIndexSearch(); DataSet ds = obj.Search(&#34; All&#34;,&#34; *&#34; + textBox1.Text.Trim(),&#34; departmentid $ = $ 1,subject $ = $ history&#34 );

如何在lucene.net中实现这种类型的过滤器?

0 个答案:

没有答案