lucene搜索所有领域

时间:2015-10-06 05:00:20

标签: java lucene

此代码将字符串数组索引到字段

for (int i =0; i < fileFields.length; i++)
          {
              Field field = new Field("field" + String.valueOf(i + 1),
                      fileFields[i],
                         Field.Store.YES,Field.Index.NOT_ANALYZED);
              document.add(field);                                
          }

我想搜索所有字段并打印匹配的字段 我尝试了这段代码,但它不能正常工作

private void searchUsingPhraseQuery(String[] phrases)
                  throws IOException, ParseException{
                  searcher = new Searcher(indexDir);
                  long startTime = System.currentTimeMillis();                                                                

                  PhraseQuery query = new PhraseQuery();
                  query.setSlop(0);

                  for(String word:phrases){
                     query.add(new Term(LuceneConstants.CONTENTS,word));
                  }                               

                  //do the search
                  TopDocs hits = searcher.search(query);
                  long endTime = System.currentTimeMillis();

                  System.out.println(hits.totalHits +
                     " Phrases found. Time :" + (endTime - startTime) + "ms");
                  int i = 1;
                  for(ScoreDoc scoreDoc : hits.scoreDocs) {
                     Document doc = searcher.getDocument(scoreDoc);
                     System.out.println("Phrase: "+ doc.get("field" + String.valueOf(i)));
                     i++;
                  }
                  searcher.close();
               }

1 个答案:

答案 0 :(得分:0)

我用这段代码索引

for (String field: fileFields)
          {              
              indexParagraph(field);
          }

public void indexParagraph(String paragraph) throws IOException{             
              System.out.println("Indexing paragraph " + "\n" + paragraph);
              Document document = getDocument(paragraph);
              writer.addDocument(document);
           }

private Document getDocument(String paragraph)
       {
           Document document = new Document();
           Field paragraphContents = new Field(LuceneConstants.paragraph,
           paragraph, Field.Store.YES, Field.Index.ANALYZED);
           document.add(paragraphContents);        
        return document;           
       }

和此代码搜索

private void sortUsingRelevance(String searchQuery)
            throws IOException, ParseException
    {
        searcher = new Searcher(indexDir);
        long startTime = System.currentTimeMillis();

        //create a term to search file name
        Term term = new Term(LuceneConstants.paragraph, searchQuery);
        //create the term query object
        Query query = new FuzzyQuery(term);
        searcher.setDefaultFieldSortScoring(true, false);
        //do the search
        TopDocs hits = searcher.search(query, Sort.RELEVANCE);
        long endTime = System.currentTimeMillis();

        System.out.println(hits.totalHits
                + " documents found. Time :" + (endTime - startTime) + "ms");
        DefaultListModel listModel = new DefaultListModel();
        for (ScoreDoc scoreDoc : hits.scoreDocs) {

            Document doc = searcher.getDocument(scoreDoc);
            listModel.addElement(doc.get(LuceneConstants.paragraph));
            jList1.setModel(listModel);

        }
        searcher.close();
    }

并且有效