예제 #1
0
  public TopDocCollectorSearchResult searchByScore(
      String queryStr, int startFrom, String operator) {

    try {
      queryStr = queryStr.trim();
      QueryParser parser = new QueryParser("contents", analyzer);
      if (QueryParser.AND_OPERATOR.toString().equalsIgnoreCase(operator)) {
        parser.setDefaultOperator(QueryParser.AND_OPERATOR);
      } else {
        parser.setDefaultOperator(QueryParser.OR_OPERATOR);
      }
      Query query;
      query = parser.parse(queryStr);
      TopDocCollector collector = doPagingSearch(query, startFrom);
      TopDocCollectorSearchResult result = new TopDocCollectorSearchResult(collector, searcher);
      return result;
    } catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    return null;

    // System.out.println("Searching for: " + query.toString("contents"));

    // doPagingSearch(in, searcher, query, hitsPerPage, raw, queries ==
    // null);

    // }
    // reader.close();
  }
예제 #2
0
  public TopFieldDocsSearchResult searchBySession(String queryStr, int startFrom, String operator) {

    try {
      queryStr = queryStr.trim();
      QueryParser parser = new QueryParser("contents", analyzer);
      Operator op = QueryParser.AND_OPERATOR;
      if (QueryParser.AND_OPERATOR.toString().equalsIgnoreCase(operator)) {
        parser.setDefaultOperator(QueryParser.AND_OPERATOR);
      } else {
        parser.setDefaultOperator(QueryParser.OR_OPERATOR);
      }

      Query query;
      query = parser.parse(queryStr);
      Sort sort = new Sort("summary", true);

      TopFieldDocs tfd = searcher.search(query, null, startFrom + 10, sort);
      TopFieldDocsSearchResult result = new TopFieldDocsSearchResult(tfd, searcher);
      return result;
    } catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    return null;
  }
 @SuppressWarnings("static-access")
 private QueryParser getQueryParser(Analyzer analyzer) {
   // defaultSearchField indicates which field search against when there is no term
   // indicated in the query string.
   // The analyzer is needed so that we use the same analyzer on the search queries as
   // was used on the text that was indexed.
   QueryParser qp = new QueryParser(defaultSearchField, analyzer);
   // this sets the query parser to AND all of the query terms it finds.
   qp.setDefaultOperator(QueryParser.AND_OPERATOR);
   // set up the map of stemmed field names -> unstemmed field names
   //        HashMap<String,String> map = new HashMap<String, String>();
   //        map.put(Entity2LuceneDoc.term.ALLTEXT,Entity2LuceneDoc.term.ALLTEXTUNSTEMMED);
   //        qp.setStemmedToUnstemmed(map);
   return qp;
 }
예제 #4
0
  private Document findByKey(IndexReader reader, Field keyField)
      throws ParseException, IOException {
    Searcher searcher = new IndexSearcher(reader);
    QueryParser queryParser = new QueryParser(luceneVersion, keyField.name(), queryAnalyzer);
    queryParser.setDefaultOperator(QueryParser.Operator.AND);

    String queryString = keyField.name() + ":" + keyField.stringValue();
    Query query = queryParser.parse(queryString);

    TopDocs docs = searcher.search(query, 10000);
    ScoreDoc[] scoreDocs = docs.scoreDocs;
    if (scoreDocs.length != 1) {
      return null;
    }

    ScoreDoc doc = scoreDocs[0];
    return searcher.doc(doc.doc);
  }
예제 #5
0
파일: Test.java 프로젝트: xjjaid13/datac
 @SuppressWarnings("resource")
 public static void main(String[] args) throws CorruptIndexException, Exception {
   // Lucene Document的域名
   String fieldName = "text";
   // 检索内容
   String text = "的数据库的设计可怜的";
   // 实例化IKAnalyzer分词器
   Analyzer analyzer = new IKAnalyzer();
   Directory directory = FSDirectory.open(new File("D:/luce/index"));
   IndexWriter iwriter = null;
   IndexReader ireader = null;
   IndexSearcher isearcher = null;
   // 建立内存索引对象
   // directory = new RAMDirectory();
   // 配置IndexWriterConfig
   IndexWriterConfig iwConfig = new IndexWriterConfig(Version.LUCENE_34, analyzer);
   iwConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
   iwriter = new IndexWriter(directory, iwConfig);
   // 写入索引
   Document doc = new Document();
   doc.add(new Field("ID", "10000", Field.Store.YES, Field.Index.NOT_ANALYZED));
   doc.add(new Field(fieldName, text, Field.Store.YES, Field.Index.ANALYZED));
   iwriter.addDocument(doc);
   iwriter.close();
   // 搜索过程********************************** //实例化搜索器
   ireader = IndexReader.open(directory);
   isearcher = new IndexSearcher(ireader);
   String keyword = "dsfdjksjflkds";
   // 使用QueryParser查询分析器构造Query对象
   QueryParser qp = new QueryParser(Version.LUCENE_34, fieldName, analyzer);
   qp.setDefaultOperator(QueryParser.AND_OPERATOR);
   Query query = qp.parse(keyword);
   // 搜索相似度最高的5条记录
   TopDocs topDocs = isearcher.search(query, 5);
   System.out.println("命中:" + topDocs.totalHits);
   // 输出结果
   ScoreDoc[] scoreDocs = topDocs.scoreDocs;
   for (int i = 0; i < topDocs.totalHits; i++) {
     Document targetDoc = isearcher.doc(scoreDocs[i].doc);
     System.out.println("内容:" + targetDoc.toString());
     // System.out.println(scoreDocs[i].);
   }
 }
예제 #6
0
 /**
  * You can use it in {@link #prepareQuery()}.
  *
  * @return the query parser
  */
 protected QueryParser newQueryParser() {
   Analyzer analyzer = getFullTextSession().getSearchFactory().getAnalyzer(getType());
   QueryParser queryParser = new QueryParser(Version.LUCENE_31, null, analyzer);
   queryParser.setDefaultOperator(Operator.AND);
   return queryParser;
 }