Lucence学习记录

Lucence学习记录

 

一、创建索引 TDictionaryIndex.java 例子

二、实用Lucene收索

A.      普通查询

    if (pname != null && !("").equals(pname)) {

                         queryParser = new QueryParser("name", analyzer);

                         query = queryParser.parse(pname);

                         booleanQuery.add(query, BooleanClause.Occur.MUST);

                    }

B.      In 范围查询

 if(datavarsor.trim().equals("30,22,4,14,12,2,7,15,21,1,6,8,5,28")){

               datavarsor="30 22 4 14 12 2 7 15 21 1 6 8 5 28";

              queryParser = new QueryParser("datavarsort", analyzer);

     queryParser.setDefaultOperator(QueryParser.Operator.OR);//

              query = queryParser.parse(datavarsor);// 多选择产品税号

              booleanQuery.add(query, BooleanClause.Occur.MUST);

         }

C.      选择查询 关键字

 if (wd != null && !wd.equals("")) {

              queryParser = new MultiFieldQueryParser(new String[] {// 查询条件是或的关系。。。

                     "department", "isorno", "filename" }, analyzer);

              query = queryParser.parse(wd);

              booleanQuery.add(query, BooleanClause.Occur.MUST);

              HeighlighterQuery = query;

D.准确查询

   注意创建索引的时候:

if (typename != null && !typename.equals(""))

                  document.add(new Field("typename", typename, Field.Store.YES,

                         Field.Index.UN_TOKENIZED));

    查询的时候:

  query = new TermQuery(new Term("typename", typename));     

              booleanQuery.add(query, BooleanClause.Occur.MUST);

E.时间排序

    org.apache.lucene.search.Sort sort2 = new org.apache.lucene.search.Sort(new SortField("endtime", SortField.STRING,

              true));// 完成按照时间来排序

 

           hits = search.search(booleanQuery, null, toIndex,sort2).scoreDocs;

 }

你可能感兴趣的:(Lucence学习记录)