有3种方法可以做到这一点.
第一种方法是手动构建查询,这就是QueryParser
内部执行的操作.这是最强大的方法,并且意味着如果您想要阻止访问以下某些更奇特的功能,则不必解析用户输入QueryParser
:
IndexReader reader = IndexReader.Open(""); Searcher searcher = new IndexSearcher(reader); BooleanQuery booleanQuery = new BooleanQuery(); Query query1 = new TermQuery(new Term("filename", " ")); Query query2 = new TermQuery(new Term("filetext", " ")); booleanQuery.add(query1, BooleanClause.Occur.SHOULD); booleanQuery.add(query2, BooleanClause.Occur.SHOULD); // Use BooleanClause.Occur.MUST instead of BooleanClause.Occur.SHOULD // for AND queries Hits hits = searcher.Search(booleanQuery);
第二种方法是使用MultiFieldQueryParser
,这种行为类似QueryParser
,允许访问它拥有的所有权力,除了它将搜索多个字段.
IndexReader reader = IndexReader.Open(""); Searcher searcher = new IndexSearcher(reader); Analyzer analyzer = new StandardAnalyzer(); MultiFieldQueryParser queryParser = new MultiFieldQueryParser( new string[] {"filename", "filetext"}, analyzer); Hits hits = searcher.Search(queryParser.parse(" "));
最后一种方法是使用特殊语法,QueryParser
请参见此处.
IndexReader reader = IndexReader.Open(""); Searcher searcher = new IndexSearcher(reader); Analyzer analyzer = new StandardAnalyzer(); QueryParser queryParser = new QueryParser(" ", analyzer); // is the field that QueryParser will search if you don't // prefix it with a field. string special = "filename:" + text + " OR filetext:" + text; Hits hits = searcher.Search(queryParser.parse(special));
您的另一种选择是创建新的领域,当你索引你的内容称为filenameandtext,可以在其中放置的内容既文件名和FILETEXT,那么你只需要搜索一个领域.
有3种方法可以做到这一点.
第一种方法是手动构建查询,这就是QueryParser
内部执行的操作.这是最强大的方法,并且意味着如果您想要阻止访问以下某些更奇特的功能,则不必解析用户输入QueryParser
:
IndexReader reader = IndexReader.Open(""); Searcher searcher = new IndexSearcher(reader); BooleanQuery booleanQuery = new BooleanQuery(); Query query1 = new TermQuery(new Term("filename", " ")); Query query2 = new TermQuery(new Term("filetext", " ")); booleanQuery.add(query1, BooleanClause.Occur.SHOULD); booleanQuery.add(query2, BooleanClause.Occur.SHOULD); // Use BooleanClause.Occur.MUST instead of BooleanClause.Occur.SHOULD // for AND queries Hits hits = searcher.Search(booleanQuery);
第二种方法是使用MultiFieldQueryParser
,这种行为类似QueryParser
,允许访问它拥有的所有权力,除了它将搜索多个字段.
IndexReader reader = IndexReader.Open(""); Searcher searcher = new IndexSearcher(reader); Analyzer analyzer = new StandardAnalyzer(); MultiFieldQueryParser queryParser = new MultiFieldQueryParser( new string[] {"filename", "filetext"}, analyzer); Hits hits = searcher.Search(queryParser.parse(" "));
最后一种方法是使用特殊语法,QueryParser
请参见此处.
IndexReader reader = IndexReader.Open(""); Searcher searcher = new IndexSearcher(reader); Analyzer analyzer = new StandardAnalyzer(); QueryParser queryParser = new QueryParser(" ", analyzer); // is the field that QueryParser will search if you don't // prefix it with a field. string special = "filename:" + text + " OR filetext:" + text; Hits hits = searcher.Search(queryParser.parse(special));
您的另一种选择是创建新的领域,当你索引你的内容称为filenameandtext,可以在其中放置的内容既文件名和FILETEXT,那么你只需要搜索一个领域.