Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package ntu.searchengine; import java.io.IOException; import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; //import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer; import ntu.searchengine.scoring.ScoringFiltering; import ntu.searchengine.ui.SearchResultModel; import org.apache.lucene.analysis.miscellaneous.PerFieldAnalyzerWrapper; import org.apache.lucene.document.Document; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexableField; import org.apache.lucene.queryparser.classic.ParseException; import org.apache.lucene.search.*; import org.apache.lucene.store.FSDirectory; /** * * @author espen1 */ public class Searcher { private final DirectoryReader rdr; private final IndexSearcher is; private final CustomQueryParser parser; public Searcher(String indexDir) throws IOException { rdr = DirectoryReader.open(FSDirectory.open(Paths.get(indexDir))); is = new IndexSearcher(rdr); PerFieldAnalyzerWrapper aWrapper = FieldAnalyzerFactory.getAnalyzer(); if (Main.boosting) { Map<String, Float> boosts = new HashMap<>(); //Title, author, journal, booktitle float[] boostsValues = { 5, 3, 1, 1 }; int index = 0; for (String field : FieldConstants.getSearchableFields()) { boosts.put(field, boostsValues[index]); index++; } parser = new CustomQueryParser(FieldConstants.getSearchableFields(), aWrapper, boosts); } else { parser = new CustomQueryParser(FieldConstants.getSearchableFields(), aWrapper); } } public ArrayList<SearchResultModel> search(String queryString) throws ParseException, IOException { parser.prepare(queryString); Query query = parser.parse(queryString); TopDocs hits = null; if (Main.sortResultByDocId) { //If we sort the documents the score field will be NaN //Sort by doc number final Sort sort = new Sort(new SortedNumericSortField(FieldConstants.DOCNAME, SortField.Type.INT)); hits = is.search(query, Main.topNResult, sort); } else { hits = is.search(query, Main.topNResult); } System.out.println("Result for query: " + queryString); ArrayList<SearchResultModel> result = getResult(hits, query); return ScoringFiltering.filterScores(result); } public ArrayList<SearchResultModel> getResult(TopDocs hits, Query query) throws IOException { ArrayList<SearchResultModel> results = new ArrayList<>(); for (ScoreDoc scoreDoc : hits.scoreDocs) { System.out.println("================================"); String explanation = is.explain(query, scoreDoc.doc).toString(); System.out.println(explanation); Document doc = is.doc(scoreDoc.doc); String output = getFieldValues(doc); float score = scoreDoc.score; System.out.println("Result score: " + score); System.out.println(output); results.add(new SearchResultModel(explanation, output, score, doc)); } return results; } public String getFieldValues(Document doc) { String output = ""; for (IndexableField f : doc.getFields()) { output += f.name() + ":" + f.stringValue() + '\n'; } return output; } public void close() throws IOException { rdr.close(); } }