Lucene - MatchAllDocsQuery

MatchAllDocsQuery come suggerisce il nome, corrisponde a tutti i documenti.

Dichiarazione di classe

Di seguito è riportata la dichiarazione per org.apache.lucene.search.MatchAllDocsQuery classe:

public class MatchAllDocsQuery
   extends Query

Costruttori di classi

S.No. Costruttore e descrizione
1

MatchAllDocsQuery()

2

MatchAllDocsQuery(String normsField)

Metodi di classe

S.No. Metodo e descrizione
1

Weight create Weight(Searcher searcher)

Esperto: crea un'implementazione del peso appropriata per questa query.

2

boolean equals(Object o)

3

void extractTerms(Set terms)

Esperto: aggiunge tutti i termini presenti in questa query al set di termini.

4

int hashCode()

5

String to String(String field)

Stampa una query su una stringa, con il campo assunto come campo predefinito e omesso.

Metodi ereditati

Questa classe eredita i metodi dalle seguenti classi:

  • org.apache.lucene.search.Query
  • java.lang.Object

Utilizzo

private void searchUsingMatchAllDocsQuery(String searchQuery)
   throws IOException, ParseException {
   searcher = new Searcher(indexDir);
   long startTime = System.currentTimeMillis();
   
   //create the term query object
   Query query = new MatchAllDocsQuery(searchQuery);
   //do the search
   TopDocs hits = searcher.search(query);
   long endTime = System.currentTimeMillis();

   System.out.println(hits.totalHits +
      " documents found. Time :" + (endTime - startTime) + "ms");
   for(ScoreDoc scoreDoc : hits.scoreDocs) {
      Document doc = searcher.getDocument(scoreDoc);
      System.out.print("Score: "+ scoreDoc.score + " ");
      System.out.println("File: "+ doc.get(LuceneConstants.FILE_PATH));
   }
   searcher.close();
}

Applicazione di esempio

Creiamo un'applicazione Lucene di prova per testare la ricerca utilizzando MatchAllDocsQuery.

Passo Descrizione
1

Creare un progetto con un nome LuceneFirstApplication sotto un packagecom.tutorialspoint.lucene come spiegato nel capitolo Lucene - Prima applicazione . È inoltre possibile utilizzare il progetto creato nel capitolo Lucene - Prima applicazione in quanto tale per questo capitolo per comprendere il processo di ricerca.

2

Creare LuceneConstants.java e Searcher.java come spiegato nel capitolo Lucene - Prima applicazione . Mantieni invariato il resto dei file.

3

Crea LuceneTester.java come indicato di seguito.

4

Pulisci e crea l'applicazione per assicurarti che la logica aziendale funzioni secondo i requisiti.

LuceneConstants.java

Questa classe viene utilizzata per fornire varie costanti da utilizzare nell'applicazione di esempio.

package com.tutorialspoint.lucene;

public class LuceneConstants {
   public static final String CONTENTS = "contents";
   public static final String FILE_NAME = "filename";
   public static final String FILE_PATH = "filepath";
   public static final int MAX_SEARCH = 10;
}

Searcher.java

Questa classe viene utilizzata per leggere gli indici realizzati su dati grezzi e ricercare dati utilizzando la libreria Lucene.

package com.tutorialspoint.lucene;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class Searcher {
	
   IndexSearcher indexSearcher;
   QueryParser queryParser;
   Query query;

   public Searcher(String indexDirectoryPath) throws IOException {
      Directory indexDirectory = 
         FSDirectory.open(new File(indexDirectoryPath));
      indexSearcher = new IndexSearcher(indexDirectory);
      queryParser = new QueryParser(Version.LUCENE_36,
         LuceneConstants.CONTENTS,
         new StandardAnalyzer(Version.LUCENE_36));
   }

   public TopDocs search( String searchQuery) 
      throws IOException, ParseException {
      query = queryParser.parse(searchQuery);
      return indexSearcher.search(query, LuceneConstants.MAX_SEARCH);
   }
   
   public TopDocs search(Query query) throws IOException, ParseException {
      return indexSearcher.search(query, LuceneConstants.MAX_SEARCH);
   }

   public Document getDocument(ScoreDoc scoreDoc) 
      throws CorruptIndexException, IOException {
      return indexSearcher.doc(scoreDoc.doc);	
   }

   public void close() throws IOException {
      indexSearcher.close();
   }
}

LuceneTester.java

Questa classe viene utilizzata per testare la capacità di ricerca della libreria Lucene.

package com.tutorialspoint.lucene;

import java.io.IOException;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;

public class LuceneTester {
	
   String indexDir = "E:\\Lucene\\Index";
   String dataDir = "E:\\Lucene\\Data";
   Searcher searcher;

   public static void main(String[] args) {
      LuceneTester tester;
      try {
         tester = new LuceneTester();
         tester.searchUsingMatchAllDocsQuery("");
      } catch (IOException e) {
         e.printStackTrace();
      } catch (ParseException e) {
         e.printStackTrace();
      }
   }
   private void searchUsingMatchAllDocsQuery(String searchQuery)
      throws IOException, ParseException {
      searcher = new Searcher(indexDir);
      long startTime = System.currentTimeMillis();
      
      //create the term query object
      Query query = new MatchAllDocsQuery(searchQuery);
      //do the search
      TopDocs hits = searcher.search(query);
      long endTime = System.currentTimeMillis();

      System.out.println(hits.totalHits +
         " documents found. Time :" + (endTime - startTime) + "ms");
      for(ScoreDoc scoreDoc : hits.scoreDocs) {
         Document doc = searcher.getDocument(scoreDoc);
         System.out.print("Score: "+ scoreDoc.score + " ");
         System.out.println("File: "+ doc.get(LuceneConstants.FILE_PATH));
      }
      searcher.close();
   }
}

Creazione di directory di dati e indici

Ho usato 10 file di testo da record1.txt a record10.txt contenenti nomi e altri dettagli degli studenti e li ho inseriti nella directory E:\Lucene\Data. Dati di prova . Un percorso della directory dell'indice dovrebbe essere creato comeE:\Lucene\Index. Dopo aver eseguito il programma di indicizzazione nel capitoloLucene - Indexing Process, puoi vedere l'elenco dei file di indice creati in quella cartella.

Esecuzione del programma

Una volta che hai finito con la creazione del sorgente, dei dati grezzi, della directory dei dati, della directory degli indici e degli indici, puoi procedere compilando ed eseguendo il tuo programma. Per fare ciò, mantenere attiva la scheda del file LuceneTester.Java e utilizzare il fileRun opzione disponibile nell'IDE Eclipse o utilizzare Ctrl + F11 per compilare ed eseguire il tuo LuceneTesterapplicazione. Se l'applicazione viene eseguita correttamente, stamperà il seguente messaggio nella console dell'IDE di Eclipse:

10 documents found. Time :9ms
Score: 1.0 File: E:\Lucene\Data\record1.txt
Score: 1.0 File: E:\Lucene\Data\record10.txt
Score: 1.0 File: E:\Lucene\Data\record2.txt
Score: 1.0 File: E:\Lucene\Data\record3.txt
Score: 1.0 File: E:\Lucene\Data\record4.txt
Score: 1.0 File: E:\Lucene\Data\record5.txt
Score: 1.0 File: E:\Lucene\Data\record6.txt
Score: 1.0 File: E:\Lucene\Data\record7.txt
Score: 1.0 File: E:\Lucene\Data\record8.txt
Score: 1.0 File: E:\Lucene\Data\record9.txt