List of usage examples for org.apache.lucene.search BooleanQuery BooleanQuery
BooleanQuery
From source file:GeonamesQParserPlugin.java
License:Apache License
public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) { return new QParser(qstr, localParams, params, req) { @Override/*from w w w . j a va 2 s. c o m*/ public Query parse() throws ParseException { final int rows = localParams.getInt("rows", 1); final int start = localParams.getInt("start", 0); String topo = localParams.get("topo"); String distFunc = localParams.get("dist"); String latFieldName = localParams.get("lat"); String lonFieldName = localParams.get("lon"); String ghFieldName = localParams.get("gh"); String units = localParams.get("unit", "M"); float boost = localParams.getFloat("boost", 1.0f); double radius = Constants.EARTH_RADIUS_MI; if (units.equalsIgnoreCase("KM")) { radius = Constants.EARTH_RADIUS_KM; } ValueSource vs = null, latVS = null, lonVS = null, ghVS = null; IndexSchema schema = req.getSchema(); DistType distType = DistType.NORM; float power = 2; List<ValueSource> latLon = new ArrayList<ValueSource>(2); if (ghFieldName != null && ghFieldName.equals("") == false) { FieldType ft = schema.getFieldType(ghFieldName); SchemaField sf = schema.getField(ghFieldName); ghVS = ft.getValueSource(sf, this);//Should we pass this here? distType = DistType.GHHSIN; } else if (latFieldName != null && latFieldName.equals("") == false && lonFieldName != null && lonFieldName.equals("") == false) { FieldType ftLat = schema.getFieldType(latFieldName); FieldType ftLon = schema.getFieldType(lonFieldName); SchemaField sfLat = schema.getField(latFieldName); SchemaField sfLon = schema.getField(lonFieldName); latVS = ftLat.getValueSource(sfLat, this); lonVS = ftLon.getValueSource(sfLon, this); latLon.add(latVS); latLon.add(lonVS); try { power = Float.parseFloat(distFunc); distType = DistType.NORM; } catch (NumberFormatException e) { if (distFunc.equals("hsin")) { distType = DistType.HSIN; } else if (distFunc.equals("ghsin")) { distType = DistType.GHHSIN; } } } else { throw new ParseException("Either gh or both lat and lon must be specified."); } ToponymSearchCriteria searchCriteria = new ToponymSearchCriteria(); searchCriteria.setQ(topo); searchCriteria.setMaxRows(rows); searchCriteria.setStartRow(start); Query query = null; try { ToponymSearchResult searchResult = WebService.search(searchCriteria); List<Toponym> topos = searchResult.getToponyms(); if (topos.size() > 1) { BooleanQuery tmp = new BooleanQuery(); for (Toponym toponym : topos) { double lat = toponym.getLatitude(); double lon = toponym.getLongitude(); FunctionQuery fq = getFunction(distType, ghVS, power, latLon, lat, lon, radius); tmp.add(fq, BooleanClause.Occur.SHOULD); } query = tmp; } else if (topos.size() == 1) { Toponym curr = topos.get(0); query = getFunction(distType, ghVS, power, latLon, curr.getLatitude(), curr.getLongitude(), radius); } } catch (Exception e) { //TODO: deal with errors } query.setBoost(boost); return query; } }; }
From source file:aos.lucene.search.advanced.BooksLikeThis.java
License:Apache License
public Document[] docsLike(int id, int max) throws IOException { Document doc = reader.document(id); String[] authors = doc.getValues("author"); BooleanQuery authorQuery = new BooleanQuery(); // for (String author : authors) { // authorQuery.add(new TermQuery(new Term("author", author)), // BooleanClause.Occur.SHOULD); // }/* ww w . j a v a 2s . com*/ authorQuery.setBoost(2.0f); TermFreqVector vector = // reader.getTermFreqVector(id, "subject"); // BooleanQuery subjectQuery = new BooleanQuery(); // for (String vecTerm : vector.getTerms()) { // TermQuery tq = new TermQuery( // new Term("subject", vecTerm)); // subjectQuery.add(tq, BooleanClause.Occur.SHOULD); // } BooleanQuery likeThisQuery = new BooleanQuery(); // likeThisQuery.add(authorQuery, BooleanClause.Occur.SHOULD); // likeThisQuery.add(subjectQuery, BooleanClause.Occur.SHOULD); // likeThisQuery.add(new TermQuery( // new Term("isbn", doc.get("isbn"))), BooleanClause.Occur.MUST_NOT); // // LOGGER.info(" Query: " + // likeThisQuery.toString("contents")); TopDocs hits = searcher.search(likeThisQuery, 10); int size = max; if (max > hits.scoreDocs.length) size = hits.scoreDocs.length; Document[] docs = new Document[size]; for (int i = 0; i < size; i++) { docs[i] = reader.document(hits.scoreDocs[i].doc); } return docs; }
From source file:aos.lucene.search.advanced.FilterTest.java
License:Apache License
public void testFilterAlternative() throws Exception { TermQuery categoryQuery = new TermQuery(new Term("category", "/philosophy/eastern")); BooleanQuery constrainedQuery = new BooleanQuery(); constrainedQuery.add(allBooks, BooleanClause.Occur.MUST); constrainedQuery.add(categoryQuery, BooleanClause.Occur.MUST); assertEquals("only tao te ching", 1, TestUtil.hitCount(searcher, constrainedQuery)); }
From source file:aos.lucene.search.advanced.MultiPhraseQueryTest.java
License:Apache License
public void testAgainstOR() throws Exception { PhraseQuery quickFox = new PhraseQuery(); quickFox.setSlop(1);//w w w . j a va 2s. co m quickFox.add(new Term("field", "quick")); quickFox.add(new Term("field", "fox")); PhraseQuery fastFox = new PhraseQuery(); fastFox.add(new Term("field", "fast")); fastFox.add(new Term("field", "fox")); BooleanQuery query = new BooleanQuery(); query.add(quickFox, BooleanClause.Occur.SHOULD); query.add(fastFox, BooleanClause.Occur.SHOULD); TopDocs hits = searcher.search(query, 10); assertEquals(2, hits.totalHits); }
From source file:aos.lucene.search.advanced.SortingExample.java
License:Apache License
public static void main(String[] args) throws Exception { Query allBooks = new MatchAllDocsQuery(); QueryParser parser = new QueryParser(Version.LUCENE_46, // "contents", // new StandardAnalyzer( // Version.LUCENE_46)); // BooleanQuery query = new BooleanQuery(); // query.add(allBooks, BooleanClause.Occur.SHOULD); // query.add(parser.parse("java OR action"), BooleanClause.Occur.SHOULD); // Directory directory = TestUtil.getBookIndexDirectory(); // SortingExample example = new SortingExample(directory); // example.displayResults(query, Sort.RELEVANCE); example.displayResults(query, Sort.INDEXORDER); example.displayResults(query, new Sort(new SortField("category", SortField.STRING))); example.displayResults(query, new Sort(new SortField("pubmonth", SortField.INT, true))); example.displayResults(query, new Sort(new SortField("category", SortField.STRING), SortField.FIELD_SCORE, new SortField("pubmonth", SortField.INT, true))); example.displayResults(query,/* w w w . ja va 2 s . c om*/ new Sort(new SortField[] { SortField.FIELD_SCORE, new SortField("category", SortField.STRING) })); directory.close(); }
From source file:aos.lucene.search.ext.filters.SpecialsFilterTest.java
License:Apache License
public void testFilteredQuery() throws Exception { String[] isbns = new String[] { "9780880105118" }; SpecialsAccessor accessor = new TestSpecialsAccessor(isbns); Filter filter = new SpecialsFilter(accessor); WildcardQuery educationBooks = new WildcardQuery(new Term("category", "*education*")); FilteredQuery edBooksOnSpecial = new FilteredQuery(educationBooks, filter); TermQuery logoBooks = new TermQuery(new Term("subject", "logo")); BooleanQuery logoOrEdBooks = new BooleanQuery(); logoOrEdBooks.add(logoBooks, BooleanClause.Occur.SHOULD); logoOrEdBooks.add(edBooksOnSpecial, BooleanClause.Occur.SHOULD); TopDocs hits = searcher.search(logoOrEdBooks, 10); LOGGER.info(logoOrEdBooks.toString()); assertEquals("Papert and Steiner", 2, hits.totalHits); }
From source file:aos.lucene.search.msc.BooleanQueryTest.java
License:Apache License
public void testAnd() throws Exception { TermQuery searchingBooks = new TermQuery(new Term("subject", "search")); //#1 Query books2010 = //#2 NumericRangeQuery.newIntRange("pubmonth", 201001, //#2 201012, //#2 true, true); //#2 BooleanQuery searchingBooks2010 = new BooleanQuery(); //#3 searchingBooks2010.add(searchingBooks, BooleanClause.Occur.MUST); //#3 searchingBooks2010.add(books2010, BooleanClause.Occur.MUST); //#3 Directory dir = TestUtil.getBookIndexDirectory(); IndexSearcher searcher = new IndexSearcher(dir); TopDocs matches = searcher.search(searchingBooks2010, 10); assertTrue(TestUtil.hitsIncludeTitle(searcher, matches, "Lucene in Action, Second Edition")); searcher.close();// w ww. ja va2s .c om dir.close(); }
From source file:aos.lucene.search.msc.BooleanQueryTest.java
License:Apache License
public void testOr() throws Exception { TermQuery methodologyBooks = new TermQuery( // new Term("category", // "/technology/computers/programming/methodology")); // TermQuery easternPhilosophyBooks = new TermQuery( // new Term("category", // "/philosophy/eastern")); // BooleanQuery enlightenmentBooks = new BooleanQuery(); // enlightenmentBooks.add(methodologyBooks, // BooleanClause.Occur.SHOULD); // enlightenmentBooks.add(easternPhilosophyBooks, // BooleanClause.Occur.SHOULD); // Directory dir = TestUtil.getBookIndexDirectory(); IndexSearcher searcher = new IndexSearcher(dir); TopDocs matches = searcher.search(enlightenmentBooks, 10); LOGGER.info("or = " + enlightenmentBooks); assertTrue(TestUtil.hitsIncludeTitle(searcher, matches, "Extreme Programming Explained")); assertTrue(TestUtil.hitsIncludeTitle(searcher, matches, "Tao Te Ching \u9053\u5FB7\u7D93")); searcher.close();//www . ja v a2s . co m dir.close(); }
From source file:aos.lucene.search.msc.QueryParserTest.java
License:Apache License
public void testToString() throws Exception { BooleanQuery query = new BooleanQuery(); query.add(new FuzzyQuery(new Term("field", "kountry")), BooleanClause.Occur.MUST); query.add(new TermQuery(new Term("title", "western")), BooleanClause.Occur.SHOULD); assertEquals("both kinds", "+kountry~0.5 title:western", query.toString("field")); }
From source file:aos.lucene.tools.ChainedFilterTest.java
License:Apache License
@Override public void setUp() throws Exception { directory = new RAMDirectory(); IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(Version.LUCENE_46), IndexWriter.MaxFieldLength.UNLIMITED); Calendar cal = Calendar.getInstance(); cal.set(2009, 1, 1, 0, 0);/*from ww w. ja v a2s . co m*/ for (int i = 0; i < MAX; i++) { Document doc = new Document(); doc.add(new Field("key", "" + (i + 1), Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.add(new Field("owner", (i < MAX / 2) ? "bob" : "sue", Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.add(new Field("date", DateTools.timeToString(cal.getTimeInMillis(), DateTools.Resolution.DAY), Field.Store.YES, Field.Index.NOT_ANALYZED)); writer.addDocument(doc); cal.add(Calendar.DATE, 1); } writer.close(); searcher = new IndexSearcher(directory); BooleanQuery bq = new BooleanQuery(); bq.add(new TermQuery(new Term("owner", "bob")), BooleanClause.Occur.SHOULD); bq.add(new TermQuery(new Term("owner", "sue")), BooleanClause.Occur.SHOULD); query = bq; cal.set(2099, 1, 1, 0, 0); dateFilter = TermRangeFilter.Less("date", DateTools.timeToString(cal.getTimeInMillis(), DateTools.Resolution.DAY));// C bobFilter = new CachingWrapperFilter(new QueryWrapperFilter(new TermQuery(new Term("owner", "bob")))); sueFilter = new CachingWrapperFilter(new QueryWrapperFilter(new TermQuery(new Term("owner", "sue")))); }