简体   繁体   中英

lucene search all the fields

This code indexes array of strings into fields

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);                                
          }

i want to search across all the fields and print the fields that match I tried this code but it didn't work as i need

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();
               }

I used this code to index

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;           
       }

and this code to search

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();
    }

and that worked

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM