Java Sortowanie zaindeksowanych dokumentów Apache Lucene

0

Witam, na podstawie API Apache Lucene indeksuje dokumenty tekstowe. Korzystając z "loop through folders" indeksuje wszystkie dokumenty w folderze głównym i we wszystkich znajdujących się w nim:

 public MakeIndex() throws Exception {
    	// create a Lucene index IndexWriter instance:
        IndexWriter indexWriter = new IndexWriter("index", new StandardAnalyzer(), true);
        AllTextFilesFilter filter = new AllTextFilesFilter();
    	
        File[] folders = new File(EXAMPLE_DOCUMENT_PATH).listFiles();
    	for (File folder : folders){
    		File[] files = folder.listFiles();
    		for (File file : files){
    			String fullPath = file.toString();
                // add the text in this file to the lucene index:
                Document document = new Document();
                // note: the second argument to Field.Text can be the string of text to index (in which
                // case it is stored in the index) or a file reader (in which case the text is read
                // and indexed, but not stored in the index)
                document.add(Field.Text("text", new FileReader(fullPath)));
                document.add(Field.UnIndexed("filepath", fullPath));
                indexWriter.addDocument(document);
                System.out.println("Wrote file " + fullPath + " to index."); 

Następnie chcę z lokalizacji gdzie są zaindeksowane dokumenty wyświetlać je ale z sortowaniem.

public class SearchIndex {
    public static void main(String[] args) throws Exception {
        Searcher searcher = new IndexSearcher("index");
        Analyzer analyzer = new StandardAnalyzer();

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            System.out.print("Search query (enter a blank query to stop) : ");
            String line = in.readLine();
            
            

            if (line == null || line.length() < 1)  break;
            
       
         

            Query query = QueryParser.parse(line, "text", analyzer);
            System.out.println("Searching for: " + query.toString("text"));

            Hits hits = searcher.search(query);
            System.out.println("Number of matching documents = " + hits.length());

            for (int i = 0; i < hits.length(); i++) {
                Document doc = hits.doc(i);
                System.out.println("File: " + doc.get("filepath") + ", score: " + hits.score(i));
           
     
            //System.out.println(hits.score(i));
            if (i<=0.5){
            	break;
            }
     
            }
        }
        searcher.close(); 

W konsoli mogę wpisywać zapytania a wyniki są wyświetlane z wartością indeksu.
Czy da się po uruchomieniu programu wyświetlane wyniki posortować w konsoli??

0

Jako parametr searchera można podać obiekt sort > https://builds.apache.org//job/Lucene-trunk/javadoc/core/org/apache/lucene/search/Sort.html z odpowiednim polem.

1 użytkowników online, w tym zalogowanych: 0, gości: 1