List of usage examples for org.apache.lucene.search WildcardQuery WildcardQuery
public WildcardQuery(Term term)
term. From source file:SimpleNaiveBayesClassifier.java
License:Apache License
/** * count the number of documents in the index having at least a value for the 'class' field * * @return the no. of documents having a value for the 'class' field * @throws IOException if accessing to term vectors or search fails *//*from w ww . ja v a2s. c o m*/ protected int countDocsWithClass() throws IOException { int docCount = MultiFields.getTerms(this.leafReader, this.classFieldName).getDocCount(); if (docCount == -1) { // in case codec doesn't support getDocCount TotalHitCountCollector classQueryCountCollector = new TotalHitCountCollector(); BooleanQuery.Builder q = new BooleanQuery.Builder(); q.add(new BooleanClause( new WildcardQuery(new Term(classFieldName, String.valueOf(WildcardQuery.WILDCARD_STRING))), BooleanClause.Occur.MUST)); if (query != null) { q.add(query, BooleanClause.Occur.MUST); } indexSearcher.search(q.build(), classQueryCountCollector); docCount = classQueryCountCollector.getTotalHits(); } return docCount; }
From source file:SearcherTest.java
/** * ??WildcardQuery/*from w w w .ja v a2 s .c o m*/ * Lucene???WildcardQuery * ???1*?0 * * @throws Exception */ @Test public void testWildcardQuery() throws Exception { String searchField = "contents"; String q = "bb??qq"; Term t = new Term(searchField, q); Query query = new WildcardQuery(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:KNearestNeighbourDocumentClassifier.java
License:Apache License
/** * Returns the top k results from a More Like This query based on the input document * * @param document the document to use for More Like This search * @return the top results for the MLT query * @throws IOException If there is a low-level I/O error *//* w w w . ja v a 2s . co m*/ private TopDocs knnSearch(Document document) throws IOException { BooleanQuery.Builder mltQuery = new BooleanQuery.Builder(); for (String fieldName : textFieldNames) { String boost = null; if (fieldName.contains("^")) { String[] field2boost = fieldName.split("\\^"); fieldName = field2boost[0]; boost = field2boost[1]; } String[] fieldValues = document.getValues(fieldName); if (boost != null) { mlt.setBoost(true); mlt.setBoostFactor(Float.parseFloat(boost)); } mlt.setAnalyzer(field2analyzer.get(fieldName)); for (String fieldContent : fieldValues) { mltQuery.add(new BooleanClause(mlt.like(fieldName, new StringReader(fieldContent)), BooleanClause.Occur.SHOULD)); } mlt.setBoost(false); } Query classFieldQuery = new WildcardQuery(new Term(classFieldName, "*")); mltQuery.add(new BooleanClause(classFieldQuery, BooleanClause.Occur.MUST)); if (query != null) { mltQuery.add(query, BooleanClause.Occur.MUST); } return indexSearcher.search(mltQuery.build(), k); }
From source file:KNearestNeighborClassifier.java
License:Apache License
private TopDocs knnSearch(String text) throws IOException { BooleanQuery.Builder mltQuery = new BooleanQuery.Builder(); for (String fieldName : textFieldNames) { String boost = null;//www .j ava2s . co m mlt.setBoost(true); //terms boost actually helps in MLT queries if (fieldName.contains("^")) { String[] field2boost = fieldName.split("\\^"); fieldName = field2boost[0]; boost = field2boost[1]; } if (boost != null) { mlt.setBoostFactor(Float.parseFloat(boost));//if we have a field boost, we add it } mltQuery.add( new BooleanClause(mlt.like(fieldName, new StringReader(text)), BooleanClause.Occur.SHOULD)); mlt.setBoostFactor(1);// restore neutral boost for next field } Query classFieldQuery = new WildcardQuery(new Term(classFieldName, "*")); mltQuery.add(new BooleanClause(classFieldQuery, BooleanClause.Occur.MUST)); if (query != null) { mltQuery.add(query, BooleanClause.Occur.MUST); } return indexSearcher.search(mltQuery.build(), k); }
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.ScoreTest.java
License:Apache License
public void testWildcard() throws Exception { indexSingleFieldDocs(new Field[] { new Field("contents", "wild", Field.Store.YES, Field.Index.ANALYZED), new Field("contents", "child", Field.Store.YES, Field.Index.ANALYZED), new Field("contents", "mild", Field.Store.YES, Field.Index.ANALYZED), new Field("contents", "mildew", Field.Store.YES, Field.Index.ANALYZED) }); IndexSearcher searcher = new IndexSearcher(directory); Query query = new WildcardQuery(new Term("contents", "?ild*")); //#A TopDocs matches = searcher.search(query, 10); assertEquals("child no match", 3, matches.totalHits); assertEquals("score the same", matches.scoreDocs[0].score, matches.scoreDocs[1].score, 0.0); assertEquals("score the same", matches.scoreDocs[1].score, matches.scoreDocs[2].score, 0.0); searcher.close();//from w w w . ja v a 2 s. com }
From source file:au.org.ala.biocache.util.ALANameSearcherExt.java
License:Open Source License
private Query buildAutocompleteQuery(String field, String q, boolean allSearches) { //best match/*from w w w . j a v a 2 s . com*/ Query fq1 = new TermQuery(new Term(field, q)); //exact match fq1.setBoost(12f); //partial matches Query fq5 = new WildcardQuery(new Term(field, q + "*")); //begins with that begins with Query fq6 = new WildcardQuery(new Term(field, "* " + q + "*")); //contains word that begins with //any match Query fq7 = new WildcardQuery(new Term(field, "*" + q + "*")); //any match //join BooleanQuery o = new BooleanQuery(); o.add(fq1, BooleanClause.Occur.SHOULD); o.add(fq5, BooleanClause.Occur.SHOULD); o.add(fq6, BooleanClause.Occur.SHOULD); o.add(fq7, BooleanClause.Occur.SHOULD); return o; }
From source file:au.org.ala.biocache.util.ALANameSearcherExt.java
License:Open Source License
/** * Basic autocomplete. All matches are resolved to accepted LSID. * * @param q//from w w w .j a va2 s. c o m * @param max * @param includeSynonyms * @return */ public List<Map> autocomplete(String q, int max, boolean includeSynonyms) { try { if (false) { return null; } else { Map<String, Map> output = new HashMap<String, Map>(); //more queries for better scoring values String lq = q.toLowerCase(); String uq = q.toUpperCase(); //name search Query fq = buildAutocompleteQuery("name", lq, false); BooleanQuery b = new BooleanQuery(); b.add(fq, BooleanClause.Occur.MUST); b.add(new WildcardQuery(new Term("left", "*")), includeSynonyms ? BooleanClause.Occur.SHOULD : BooleanClause.Occur.MUST); TopDocs results = getIdentifierIdxSearcher().search(b, max); appendAutocompleteResults(output, results, includeSynonyms, false); //format search term for the current common name index uq = concatName(uq).toUpperCase(); //common name search fq = buildAutocompleteQuery("common", uq, true); results = getVernIdxSearcher().search(fq, max); appendAutocompleteResults(output, results, includeSynonyms, true); return new ArrayList(output.values()); } } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:brightsolid.solr.plugins.TestTargetPositionQueryFuzzy.java
License:Apache License
public void testTargetPositionWildcard() throws Exception { WildcardQuery fq = new WildcardQuery(new Term("field", "tw*")); SpanQuery stq = new SpanMultiTermQueryWrapper<WildcardQuery>(fq); SpanQuery tpq = new SpanTargetPositionQuery(stq, 2); TopDocs td = searcher.search(tpq, 10); assertEquals(fieldValue(td, 0), "threx one twp"); assertEquals(3, td.totalHits);//from w w w . j a v a 2s . c o m }
From source file:ca.dracode.ais.indexer.FileSearcher.java
License:Open Source License
/** * Creates a query based on the given term field and type * @param term Search Term for the query * @param field Document Field for the Query which the term is matched against * @param type The type of query to be created, either QUERY_BOOLEAN, or QUERY_STANDARD, * @return a query for the given field and term using either a BooleanQuery with a * WildcardQuery for the term or a Query built from a QueryParser and SimpleAnalyzer *//*from ww w . java 2 s . c o m*/ private Query getQuery(String term, String field, int type) { Query qry = null; if (type == FileSearcher.QUERY_BOOLEAN) { qry = new BooleanQuery(); String[] words = term.split(" "); ((BooleanQuery) qry).add(new WildcardQuery(new Term(field, "*" + words[0])), BooleanClause.Occur.MUST); if (words.length > 1) { for (int i = 1; i < words.length - 1; i++) { ((BooleanQuery) qry).add(new WildcardQuery(new Term(field, words[i])), BooleanClause.Occur.MUST); } ((BooleanQuery) qry).add(new WildcardQuery(new Term(field, words[words.length - 1] + "*")), BooleanClause.Occur.MUST); } } else if (type == FileSearcher.QUERY_STANDARD) { try { qry = new QueryParser(Version.LUCENE_47, field, new SimpleAnalyzer(Version.LUCENE_47)).parse(term); } catch (ParseException e) { e.printStackTrace(); } } return qry; }