List of usage examples for org.apache.lucene.search TermQuery TermQuery
public TermQuery(Term t)
t. From source file:SimpleNaiveBayesClassifier.java
License:Apache License
/** * Returns the number of documents of the input class ( from the whole index or from a subset) * that contains the word ( in a specific field or in all the fields if no one selected) * @param word the token produced by the analyzer * @param term the term representing the class * @return the number of documents of the input class * @throws IOException if a low level I/O problem happens *//*from w w w. jav a 2s. com*/ private int getWordFreqForClass(String word, Term term) throws IOException { BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder(); BooleanQuery.Builder subQuery = new BooleanQuery.Builder(); for (String textFieldName : textFieldNames) { subQuery.add( new BooleanClause(new TermQuery(new Term(textFieldName, word)), BooleanClause.Occur.SHOULD)); } booleanQuery.add(new BooleanClause(subQuery.build(), BooleanClause.Occur.MUST)); booleanQuery.add(new BooleanClause(new TermQuery(term), BooleanClause.Occur.MUST)); if (query != null) { booleanQuery.add(query, BooleanClause.Occur.MUST); } TotalHitCountCollector totalHitCountCollector = new TotalHitCountCollector(); indexSearcher.search(booleanQuery.build(), totalHitCountCollector); return totalHitCountCollector.getTotalHits(); }
From source file:ContentBasedAnalysis.java
License:Apache License
private static int findDocId(IndexSearcher searcher, String filename) throws Exception { Term t = new Term("path", filename); Query q = new TermQuery(t); TopDocs td = searcher.search(q, 2);//from w ww . j a v a 2 s.c o m if (td.totalHits < 1) return -1; else return td.scoreDocs[0].doc; }
From source file:SearcherTest.java
/** * ?// w w w.j ava2 s. c om * ???TermQuery * TermQuery??QueryTermQuery??????? * ??????TermQuery?? * Lucene??????????/ * ?????????? * * @throws Exception */ @Test public void testTermQuery() throws Exception { String searchField = "contents"; String q = "xxxxxxxxx$"; Term t = new Term(searchField, q); Query query = new TermQuery(t); TopDocs hits = is.search(query, 10); System.out.println("? '" + q + "'" + hits.totalHits + ""); for (ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = is.doc(scoreDoc.doc); System.out.println(doc.get("fullPath")); } }
From source file:SearcherTest.java
/** * ???BooleanQuery//from w w w . j a v a 2s .c om * BooleanQuery???Query * ?Query???Query * ?BooleanQuery?? * BooleanQuery?????API?? * ?BooleanQuery????API? * * @throws Exception */ @Test public void testBooleanQuery() throws Exception { String searchField = "contents"; String q1 = "xxxxxxxxx"; String q2 = "oooooooooooooooo"; Query query1 = new TermQuery(new Term(searchField, q1)); Query query2 = new TermQuery(new Term(searchField, q2)); BooleanQuery.Builder builder = new BooleanQuery.Builder(); // 1MUSTMUST??? // 2MUSTMUST_NOT??MUST_NOT?? // 3SHOULDMUST_NOT?MUSTMUST_NOT // 4SHOULDMUSTMUST??,SHOULD??? // 5SHOULDSHOULD??? // 6MUST_NOTMUST_NOT? builder.add(query1, BooleanClause.Occur.MUST); builder.add(query2, BooleanClause.Occur.MUST); BooleanQuery booleanQuery = builder.build(); TopDocs hits = is.search(booleanQuery, 10); System.out.println("? " + q1 + "And" + q2 + "" + hits.totalHits + ""); for (ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = is.doc(scoreDoc.doc); System.out.println(doc.get("fullPath")); } }
From source file:SimpleNaiveBayesDocumentClassifier.java
License:Apache License
/** * Returns the number of documents of the input class ( from the whole index or from a subset) * that contains the word ( in a specific field or in all the fields if no one selected) * * @param word the token produced by the analyzer * @param fieldName the field the word is coming from * @param term the class term// w w w . j a va 2 s .com * @return number of documents of the input class * @throws java.io.IOException If there is a low-level I/O error */ private int getWordFreqForClass(String word, String fieldName, Term term) throws IOException { BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder(); BooleanQuery.Builder subQuery = new BooleanQuery.Builder(); subQuery.add(new BooleanClause(new TermQuery(new Term(fieldName, word)), BooleanClause.Occur.SHOULD)); booleanQuery.add(new BooleanClause(subQuery.build(), BooleanClause.Occur.MUST)); booleanQuery.add(new BooleanClause(new TermQuery(term), BooleanClause.Occur.MUST)); if (query != null) { booleanQuery.add(query, BooleanClause.Occur.MUST); } TotalHitCountCollector totalHitCountCollector = new TotalHitCountCollector(); indexSearcher.search(booleanQuery.build(), totalHitCountCollector); return totalHitCountCollector.getTotalHits(); }
From source file:NoPackageTest.java
License:LGPL
@Test public void testMultipleEntitiesPerIndex() throws Exception { try (Session s = openSession()) { s.getTransaction().begin();/*from www. j av a2s . co m*/ NotPackagedEntity box = new NotPackagedEntity(); box.title = "This feels dirty"; s.persist(box); s.getTransaction().commit(); } try (Session s = openSession()) { s.getTransaction().begin(); TermQuery q = new TermQuery(new Term("title", "dirty")); List results = Search.getFullTextSession(s).createFullTextQuery(q, NotPackagedEntity.class).list(); assertEquals(1, results.size()); } }
From source file:luceneInterface.java
License:Apache License
public static List<Document> query(String index, String stoppath, String question, int numResult, String sim) throws Exception { IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(index))); IndexSearcher searcher = new IndexSearcher(reader); Analyzer analyzer = new EnglishAnalyzer(StopFilter.makeStopSet(mygetStopwords(stoppath))); if (sim.equals("TFIDF")) searcher.setSimilarity(new ClassicSimilarity()); else if (sim.equals("BM25")) searcher.setSimilarity(new BM25Similarity()); else// w w w. ja va 2 s . c o m searcher.setSimilarity(new BM25Similarity()); String field = "contents"; QueryParser parser = new QueryParser(field, analyzer); Query query = parser.parse(parser.escape(question)); BooleanQuery.Builder bqb = new BooleanQuery.Builder(); bqb.add(new TermQuery(new Term("contents", parser.escape(question))), BooleanClause.Occur.SHOULD); bqb.add(new TermQuery(new Term("sec", parser.escape(question))), BooleanClause.Occur.SHOULD); // Term term = new Term(field, question); // Query query = new TermQuery(term); // TopDocs results = searcher.search(query, numResult); TopDocs results = searcher.search(parser.parse(bqb.build().toString()), numResult); ScoreDoc[] hits = results.scoreDocs; List<Document> docs = new ArrayList<Document>(); int numTotalHits = results.totalHits; // System.out.println(numTotalHits + " total matching documents"); int end = Math.min(numTotalHits, numResult); String searchResult = ""; // System.out.println("Only results 1 - " + hits.length); for (int i = 0; i < end; i++) { Document doc = searcher.doc(hits[i].doc); docs.add(doc); } return docs; }
From source file:TfIdfViewer.java
License:Apache License
private static int findDocId(IndexSearcher searcher, String filename) throws Exception { Term t = new Term("path", filename); Query q = new TermQuery(t); TopDocs td = searcher.search(q, 2); // get a list of docs matching the query if (td.totalHits < 1) return -1; // no hits found else/*from w ww .j av a 2 s . c om*/ return td.scoreDocs[0].doc; // returns first matching docId }
From source file:action.indexing.IndexingTest.java
License:Apache License
protected int getHitCount(String fieldName, String searchString) throws IOException { IndexSearcher searcher = new IndexSearcher(directory); //4 Term t = new Term(fieldName, searchString); Query query = new TermQuery(t); //5 int hitCount = TestUtil.hitCount(searcher, query); //6 searcher.close();// ww w. j ava 2 s . c om return hitCount; }
From source file:action.meetlucene.Fragments.java
License:Apache License
public static void simpleSearch() throws IOException { Directory dir = FSDirectory.open(new File("index")); IndexSearcher searcher = new IndexSearcher(dir); Query q = new TermQuery(new Term("contents", "licenses")); TopDocs hits = searcher.search(q, 10); System.out.println(hits.scoreDocs.length); searcher.close();/* w ww. ja v a 2 s .c om*/ }