List of usage examples for org.apache.lucene.search IndexSearcher IndexSearcher
public IndexSearcher(IndexReaderContext context)
From source file:com.leavesfly.lia.advsearching.MultiSearcherTest.java
License:Apache License
public void setUp() throws Exception { String[] animals = { "aardvark", "beaver", "coati", "dog", "elephant", "frog", "gila monster", "horse", "iguana", "javelina", "kangaroo", "lemur", "moose", "nematode", "orca", "python", "quokka", "rat", "scorpion", "tarantula", "uromastyx", "vicuna", "walrus", "xiphias", "yak", "zebra" }; Analyzer analyzer = new WhitespaceAnalyzer(); Directory aTOmDirectory = new RAMDirectory(); // #1 Directory nTOzDirectory = new RAMDirectory(); // #1 IndexWriter aTOmWriter = new IndexWriter(aTOmDirectory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED); IndexWriter nTOzWriter = new IndexWriter(nTOzDirectory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED); for (int i = animals.length - 1; i >= 0; i--) { Document doc = new Document(); String animal = animals[i]; doc.add(new Field("animal", animal, Field.Store.YES, Field.Index.NOT_ANALYZED)); if (animal.charAt(0) < 'n') { aTOmWriter.addDocument(doc); // #2 } else {//from w ww . j av a 2s.co m nTOzWriter.addDocument(doc); // #2 } } aTOmWriter.close(); nTOzWriter.close(); searchers = new IndexSearcher[2]; searchers[0] = new IndexSearcher(aTOmDirectory); searchers[1] = new IndexSearcher(nTOzDirectory); }
From source file:com.leavesfly.lia.advsearching.SecurityFilterTest.java
License:Apache License
protected void setUp() throws Exception { Directory directory = new RAMDirectory(); IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED); Document document = new Document(); // 1 document.add(new Field("owner", // 1 "elwood", // 1 Field.Store.YES, // 1 Field.Index.NOT_ANALYZED)); // 1 document.add(new Field("keywords", // 1 "elwood's sensitive info", // 1 Field.Store.YES, // 1 Field.Index.ANALYZED)); // 1 writer.addDocument(document);/*w w w . ja va 2 s.com*/ document = new Document(); // 2 document.add(new Field("owner", // 2 "jake", // 2 Field.Store.YES, // 2 Field.Index.NOT_ANALYZED)); // 2 document.add(new Field("keywords", // 2 "jake's sensitive info", // 2 Field.Store.YES, // 2 Field.Index.ANALYZED)); // 2 writer.addDocument(document); writer.close(); searcher = new IndexSearcher(directory); }
From source file:com.leavesfly.lia.advsearching.SortingExample.java
License:Apache License
public void displayResults(Query query, Sort sort) // #1 throws IOException { IndexSearcher searcher = new IndexSearcher(directory); searcher.setDefaultFieldSortScoring(true, false); // #2 TopDocs results = searcher.search(query, null, // #3 20, sort); // #3 System.out.println("\nResults for: " + // #4 query.toString() + " sorted by " + sort); System.out.println(StringUtils.rightPad("Title", 30) + StringUtils.rightPad("pubmonth", 10) + StringUtils.center("id", 4) + StringUtils.center("score", 15)); PrintStream out = new PrintStream(System.out, true, "UTF-8"); // #5 DecimalFormat scoreFormatter = new DecimalFormat("0.######"); for (ScoreDoc sd : results.scoreDocs) { int docID = sd.doc; float score = sd.score; Document doc = searcher.doc(docID); out.println(StringUtils.rightPad( // #6 StringUtils.abbreviate(doc.get("title"), 29), 30) + // #6 StringUtils.rightPad(doc.get("pubmonth"), 10) + // #6 StringUtils.center("" + docID, 4) + // #6 StringUtils.leftPad( // #6 scoreFormatter.format(score), 12)); // #6 out.println(" " + doc.get("category")); // out.println(searcher.explain(query, docID)); // #7 }//ww w . ja v a 2 s. co m searcher.close(); }
From source file:com.leavesfly.lia.advsearching.SpanQueryTest.java
License:Apache License
protected void setUp() throws Exception { directory = new RAMDirectory(); analyzer = new WhitespaceAnalyzer(); IndexWriter writer = new IndexWriter(directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED); Document doc = new Document(); doc.add(new Field("f", "the quick brown fox jumps over the lazy dog", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc);// w ww. ja v a2s.c o m doc = new Document(); doc.add(new Field("f", "the quick red fox jumps over the sleepy cat", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc); writer.close(); searcher = new IndexSearcher(directory); reader = searcher.getIndexReader(); quick = new SpanTermQuery(new Term("f", "quick")); brown = new SpanTermQuery(new Term("f", "brown")); red = new SpanTermQuery(new Term("f", "red")); fox = new SpanTermQuery(new Term("f", "fox")); lazy = new SpanTermQuery(new Term("f", "lazy")); sleepy = new SpanTermQuery(new Term("f", "sleepy")); dog = new SpanTermQuery(new Term("f", "dog")); cat = new SpanTermQuery(new Term("f", "cat")); }
From source file:com.leavesfly.lia.advsearching.TimeLimitingCollectorTest.java
License:Apache License
public void testTimeLimitingCollector() throws Exception { Directory dir = TestUtil.getBookIndexDirectory(); IndexSearcher searcher = new IndexSearcher(dir); Query q = new MatchAllDocsQuery(); int numAllBooks = TestUtil.hitCount(searcher, q); TopScoreDocCollector topDocs = TopScoreDocCollector.create(10, false); Collector collector = new TimeLimitingCollector(topDocs, // #A 1000); // #A try {//from w w w . j a v a 2 s . co m searcher.search(q, collector); assertEquals(numAllBooks, topDocs.getTotalHits()); // #B } catch (TimeExceededException tee) { // #C System.out.println("Too much time taken."); // #C } // #C searcher.close(); dir.close(); }
From source file:com.leavesfly.lia.analysis.keyword.KeywordAnalyzerTest.java
License:Apache License
public void setUp() throws Exception { Directory directory = new RAMDirectory(); IndexWriter writer = new IndexWriter(directory, new SimpleAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED); Document doc = new Document(); doc.add(new Field("partnum", "Q36", Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS)); // A doc.add(new Field("description", "Illidium Space Modulator", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc);//from www. j a v a 2 s .co m writer.close(); searcher = new IndexSearcher(directory); }
From source file:com.leavesfly.lia.extsearch.payloads.PayloadsTest.java
License:Apache License
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();//from www .jav a2 s.c o m writer.close(); IndexSearcher searcher = new IndexSearcher(r); searcher.setSimilarity(new BoostingSimilarity()); Term warning = new Term("contents", "warning"); Query query1 = new TermQuery(warning); System.out.println("\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()); System.out.println("\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(); }
From source file:com.leavesfly.lia.extsearch.sorting.DistanceSortingTest.java
License:Apache License
protected void setUp() throws Exception { directory = new RAMDirectory(); IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED); addPoint(writer, "El Charro", "restaurant", 1, 2); addPoint(writer, "Cafe Poca Cosa", "restaurant", 5, 9); addPoint(writer, "Los Betos", "restaurant", 9, 6); addPoint(writer, "Nico's Taco Shop", "restaurant", 3, 8); writer.close();/* w w w. ja va 2 s . c o m*/ searcher = new IndexSearcher(directory); query = new TermQuery(new Term("type", "restaurant")); }
From source file:com.leavesfly.lia.meetlucene.Fragments.java
License:Apache License
public void simpleSearch() throws IOException { Directory dir = FSDirectory.open(new File("/tmp/index")); IndexSearcher searcher = new IndexSearcher(dir); Query q = new TermQuery(new Term("contents", "lucene")); TopDocs hits = searcher.search(q, 10); searcher.close();/*from w w w .j ava 2 s .c o m*/ }
From source file:com.leavesfly.lia.meetlucene.Searcher.java
License:Apache License
public static void search(String indexDir, String q) throws IOException, ParseException { Directory dir = FSDirectory.open(new File(indexDir)); //3 IndexSearcher is = new IndexSearcher(dir); //3 QueryParser parser = new QueryParser(Version.LUCENE_30, // 4 "contents", //4 new StandardAnalyzer( //4 Version.LUCENE_30)); //4 Query query = parser.parse(q); //4 long start = System.currentTimeMillis(); TopDocs hits = is.search(query, 10); //5 long end = System.currentTimeMillis(); System.err.println("Found " + hits.totalHits + //6 " document(s) (in " + (end - start) + // 6 " milliseconds) that matched query '" + // 6 q + "':"); // 6 for (ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = is.doc(scoreDoc.doc); //7 System.out.println(doc.get("fullpath")); //8 }//from ww w . j a v a 2 s. c o m is.close(); //9 }