Lucene - MatchAllDocsQuery

MatchAllDocsQuery comme son nom l'indique, correspond à tous les documents.

Déclaration de classe

Voici la déclaration pour org.apache.lucene.search.MatchAllDocsQuery classe:

public class MatchAllDocsQuery
   extends Query

Constructeurs de classe

S.No. Constructeur et description
1

MatchAllDocsQuery()

2

MatchAllDocsQuery(String normsField)

Méthodes de classe

S.No. Méthode et description
1

Weight create Weight(Searcher searcher)

Expert: construit une implémentation Weight appropriée pour cette requête.

2

boolean equals(Object o)

3

void extractTerms(Set terms)

Expert: ajoute tous les termes apparaissant dans cette requête à l'ensemble de termes.

4

int hashCode()

5

String to String(String field)

Imprime une requête dans une chaîne, le champ étant supposé être le champ par défaut et omis.

Méthodes héritées

Cette classe hérite des méthodes des classes suivantes -

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

Usage

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

Exemple d'application

Créons une application de test Lucene pour tester la recherche à l'aide de MatchAllDocsQuery.

Étape La description
1

Créez un projet avec un nom LuceneFirstApplication sous un packagecom.tutorialspoint.lucene comme expliqué dans le chapitre Lucene - Première application . Vous pouvez également utiliser le projet créé dans le chapitre Lucene - Première application en tant que tel pour ce chapitre pour comprendre le processus de recherche.

2

Créez LuceneConstants.java et Searcher.java comme expliqué dans le chapitre Lucene - Première application . Gardez le reste des fichiers inchangés.

3

Créez LuceneTester.java comme indiqué ci-dessous.

4

Nettoyez et créez l'application pour vous assurer que la logique métier fonctionne conformément aux exigences.

LuceneConstants.java

Cette classe est utilisée pour fournir diverses constantes à utiliser dans l'exemple d'application.

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

Cette classe permet de lire les index créés sur des données brutes et de rechercher des données à l'aide de la bibliothèque 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

Cette classe est utilisée pour tester la capacité de recherche de la bibliothèque 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();
   }
}

Création de répertoires de données et d'index

J'ai utilisé 10 fichiers texte de record1.txt à record10.txt contenant les noms et autres détails des étudiants et les ai mis dans le répertoire E:\Lucene\Data. Données de test . Un chemin de répertoire d'index doit être créé commeE:\Lucene\Index. Après avoir exécuté le programme d'indexation dans le chapitreLucene - Indexing Process, vous pouvez voir la liste des fichiers d'index créés dans ce dossier.

Exécution du programme

Une fois que vous avez terminé la création de la source, des données brutes, du répertoire de données, du répertoire d'index et des index, vous pouvez continuer en compilant et en exécutant votre programme. Pour ce faire, gardez l'onglet de fichier LuceneTester.Java actif et utilisez soit leRun option disponible dans l'IDE Eclipse ou utilisez Ctrl + F11 pour compiler et exécuter votre LuceneTesterapplication. Si votre application s'exécute avec succès, elle imprimera le message suivant dans la console d'Eclipse IDE -

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