- /**
- * 根據(jù)信息分類和關(guān)鍵詞進(jìn)行查詢
- * @param type,資源的類型,其值為news或product
- * @param searchKey,搜索的關(guān)鍵字
- * @return Hits
- */
- public Hits executeSearch(String type,String keyword)
- {
- Hits result = null;
- if(type != null && !type.equals("") && keyword != null && !keyword.equals(""))
- {
- try
- {
- //根據(jù)關(guān)鍵字構(gòu)造一個數(shù)組
- String[] key = new String[]{keyword,type};
- //同時聲明一個與之對應(yīng)的字段數(shù)組
- String[] fields = {"title","type"};
- //聲明BooleanClause.Occur[]數(shù)組,它表示多個條件之間的關(guān)系
- BooleanClause.Occur[] flags=new BooleanClause.Occur[]{BooleanClause.Occur.MUST,BooleanClause.Occur.MUST};
- ChineseAnalyzer analyzer = new ChineseAnalyzer();
- //用MultiFieldQueryParser得到query對象
- Query query = MultiFieldQueryParser.parse(key, fields, flags, analyzer);
- //c:/index表示我們的索引文件所在的目錄
- IndexSearcher searcher = new IndexSearcher("c:/index");
- //查詢結(jié)果
- result = searcher.search(query);
- } catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- return result;
- }
這里需要注意的就是BooleanClause.Occur[]數(shù)組,它表示多個條件之間的關(guān)系,BooleanClause.Occur.MUST表示and,BooleanClause.Occur.MUST_NOT表示not,BooleanClause.Occur.SHOULD表示or.
安徽新華電腦學(xué)校專業(yè)職業(yè)規(guī)劃師為你提供更多幫助【在線咨詢】