public void search(String queryString) { try { IndexReader reader = DirectoryReader.open(FSDirectory.open(new File("D:/data/indices/sandbox"))); IndexSearcher searcher = new IndexSearcher(reader); searcher.setSimilarity(new MyPayloadSimilarity()); PayloadTermQuery query = new PayloadTermQuery(new Term("concept", queryString), new AveragePayloadFunction()); System.out.println("Query: " + query.toString()); TopDocs topDocs = searcher.search(query, 999); ScoreDoc[] hits = topDocs.scoreDocs; System.out.println("Number of results:" + hits.length); // output for (int i = 0; i < hits.length; i++) { Document doc = searcher.doc(hits[i].doc); System.out.println("-> docid: " + doc.get("path") + " score: " + hits[i].score); } reader.close(); } catch (Exception e) { System.out.println("Exception while searching: " + e.getMessage()); } }
@Override public SpanQuery getSpanQuery(Element e) throws ParserException { String fieldName = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName"); String value = DOMUtils.getNonBlankTextOrFail(e); PayloadTermQuery btq = new PayloadTermQuery(new Term(fieldName, value), new AveragePayloadFunction()); btq.setBoost(DOMUtils.getAttribute(e, "boost", 1.0f)); return btq; }
private List<Query> getMathQueries(Map<String, Float> queryForms, MathTokenizer.MathMLType type) { String field = (type == MathTokenizer.MathMLType.PRESENTATION ? "p" : "c") + "math"; List<Query> result = new ArrayList<>(); Iterator<Map.Entry<String, Float>> it = queryForms.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, Float> entry = it.next(); Float boost = entry.getValue(); PayloadTermQuery ptq = new PayloadTermQuery(new Term(field, entry.getKey()), new AveragePayloadFunction()); ptq.setBoost(boost); result.add(ptq); } return result; }
private void matchSequence(IndexReader reader, String sequence, int docs, int continuations) throws IOException { ContinuationCollector collector = new ContinuationCollector(); ContinuationSimilarity similarity = new ContinuationSimilarity(collector); IndexSearcher searcher = new IndexSearcher(reader); searcher.setSimilarity(similarity); Query query = new PayloadTermQuery(new Term(FIELD_NAME, sequence), new AveragePayloadFunction()); searcher.search(query, collector); TopDocs results = collector.getTopDocs(); assertEquals(docs, results.scoreDocs.length); assertEquals(continuations, collector.getContinuationsCount()); }
public void testPayloadTermQuery() throws Throwable { addDoc("Hurricane warning", "Bulletin: A hurricane warning was issued at " + "6 AM for the outer great banks"); addDoc("Warning label maker", "The warning label maker is a delightful toy for " + "your precocious seven year old's warning needs"); addDoc("Tornado warning", "Bulletin: There is a tornado warning for " + "Worcester county until 6 PM today"); IndexReader r = writer.getReader(); writer.close(); IndexSearcher searcher = new IndexSearcher(r); searcher.setSimilarity(new BoostingSimilarity()); Term warning = new Term("contents", "warning"); Query query1 = new TermQuery(warning); LOGGER.info("\nTermQuery results:"); TopDocs hits = searcher.search(query1, 10); TestUtil.dumpHits(searcher, hits); assertEquals("Warning label maker", // #B searcher.doc(hits.scoreDocs[0].doc).get("title")); // #B Query query2 = new PayloadTermQuery(warning, new AveragePayloadFunction()); LOGGER.info("\nPayloadTermQuery results:"); hits = searcher.search(query2, 10); TestUtil.dumpHits(searcher, hits); assertEquals("Warning label maker", // #C searcher.doc(hits.scoreDocs[2].doc).get("title")); // #C r.close(); searcher.close(); }
/** * Searches for documents based on a text query. * @param queryString The query that is used to search */ public List<Result> search(String queryString) { // for results List<Result> results = new ArrayList<>(); String conceptIndexfolder = configManager.getProperty("conceptindexer.indexfolder"); try { // access the index and create a searcher IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(conceptIndexfolder))); IndexSearcher searcher = new IndexSearcher(reader); searcher.setSimilarity(new PayloadSimilarity()); // create query PayloadTermQuery query = new PayloadTermQuery(new Term("concept", queryString), new AveragePayloadFunction()); logger.info("Query: " + query.toString()); // searching... TopDocs topDocs = searcher.search(query,999); ScoreDoc[] hits = topDocs.scoreDocs; // transform into Results objects for (int i = 0; i < hits.length; i++) { // get document from index Document doc = searcher.doc(hits[i].doc); // store docIDs (filename) and score Result result = new Result(doc.get("path"), doc.get("path"), hits[i].score, "NO_USER"); results.add(result); } reader.close(); } catch (Exception e) { logger.error("Exception while searching for '" + queryString + "': " + e.getMessage()); e.printStackTrace(); } return results; }