List of usage examples for org.apache.lucene.search BooleanQuery BooleanQuery
BooleanQuery
From source file:com.browseengine.bobo.test.section.TestSectionSearch.java
License:Apache License
private void metaDataSearch(IndexSearcher searcher) throws Exception { IndexReader reader = searcher.getIndexReader(); BooleanQuery bquery;/* ww w .j ava 2 s . co m*/ SectionSearchQuery squery; Scorer scorer; int count; // 1. bquery = new BooleanQuery(); bquery.add(new TermQuery(new Term("f1", "aa")), BooleanClause.Occur.MUST); bquery.add(new IntMetaDataQuery(intMetaTerm, new IntMetaDataQuery.SimpleValueValidator(100)), BooleanClause.Occur.MUST); squery = new SectionSearchQuery(bquery); scorer = squery.createWeight(searcher).scorer(reader, true, true); count = 0; while (scorer.nextDoc() != Scorer.NO_MORE_DOCS) count++; assertEquals("section count mismatch", 1, count); // 2. bquery = new BooleanQuery(); bquery.add(new TermQuery(new Term("f1", "aa")), BooleanClause.Occur.MUST); bquery.add(new IntMetaDataQuery(intMetaTerm, new IntMetaDataQuery.SimpleValueValidator(200)), BooleanClause.Occur.MUST); squery = new SectionSearchQuery(bquery); scorer = squery.createWeight(searcher).scorer(reader, true, true); count = 0; while (scorer.nextDoc() != Scorer.NO_MORE_DOCS) count++; assertEquals("section count mismatch", 1, count); // 3. bquery = new BooleanQuery(); bquery.add(new TermQuery(new Term("f1", "bb")), BooleanClause.Occur.MUST); bquery.add(new IntMetaDataQuery(intMetaTerm, new IntMetaDataQuery.SimpleValueValidator(200)), BooleanClause.Occur.MUST); squery = new SectionSearchQuery(bquery); scorer = squery.createWeight(searcher).scorer(reader, true, true); count = 0; while (scorer.nextDoc() != Scorer.NO_MORE_DOCS) count++; assertEquals("section count mismatch", 2, count); // 4. bquery = new BooleanQuery(); bquery.add(new TermQuery(new Term("f1", "aa")), BooleanClause.Occur.MUST); bquery.add(new IntMetaDataQuery(intMetaTerm, new IntMetaDataQuery.SimpleValueValidator(300)), BooleanClause.Occur.MUST); squery = new SectionSearchQuery(bquery); scorer = squery.createWeight(searcher).scorer(reader, true, true); count = 0; while (scorer.nextDoc() != Scorer.NO_MORE_DOCS) count++; assertEquals("section count mismatch", 1, count); // 5. bquery = new BooleanQuery(); bquery.add(new TermQuery(new Term("f1", "bb")), BooleanClause.Occur.MUST); bquery.add(new IntMetaDataQuery(intMetaTerm, new IntMetaDataQuery.SimpleValueValidator(300)), BooleanClause.Occur.MUST); squery = new SectionSearchQuery(bquery); scorer = squery.createWeight(searcher).scorer(reader, true, true); count = 0; while (scorer.nextDoc() != Scorer.NO_MORE_DOCS) count++; assertEquals("section count mismatch", 3, count); }
From source file:com.codecrate.shard.search.HibernateObjectSearcher.java
License:Apache License
public Collection search(Class target, String query) { Searcher searcher = null;//w w w . j ava2 s .c om Collection results = new ArrayList(); try { searcher = new IndexSearcher(directory); BooleanQuery masterQuery = new BooleanQuery(); masterQuery.add(new TermQuery(new Term(FIELD_CLASS, target.getName())), REQUIRED, NOT_PROHIBITED); masterQuery.add(QueryParser.parse(makeWildcardQuery(query), FIELD_TEXT, analyzer), REQUIRED, NOT_PROHIBITED); LOG.debug("Searching for " + masterQuery); Hits hits = searcher.search(masterQuery); LOG.debug("Found " + hits.length() + " matches"); for (int x = 0; x < hits.length(); x++) { Document document = hits.doc(x); String id = document.getField(FIELD_ID).stringValue(); Object result = this.get(target, id); if (null == result) { LOG.warn("Search index is out of synch with database. Unable to find object " + target + " with id " + id); } else { results.add(result); } } } catch (Exception e) { LOG.error("Error searching directory " + directory + " for type " + target + " using query " + query, e); } finally { closeSearcher(searcher); } return results; }
From source file:com.cohesionforce.search.EMFIndex.java
License:Open Source License
/** * Searches the index for an attribute with a matching value and a matching * eclass//from w ww . j a v a 2 s. c o m * * @param eclass * - the EClass to match when searching * @param attr * - the EAttribute to match when searching * @param value * - the value to match when searching * @return a list of search results * @throws IllegalArgumentException * if any parameters are null, or if the attribute is not marked * with the search annotation * @throws IOException * if there are issues reading the index */ public List<SearchResult> search(EClass eclass, EAttribute attr, String value) throws IllegalArgumentException, IOException { if (eclass == null) { throw new IllegalArgumentException("EClass cannot be null"); } if (attr == null) { throw new IllegalArgumentException("Attribute cannot be null"); } if (value == null) { throw new IllegalArgumentException("Value cannot be null"); } EAnnotation annotation = eclass.getEAnnotation(EMFIndexUtil.SOURCE); if (annotation == null) { // Bail out early if this feature should not be indexed throw new IllegalArgumentException("EClass is not annotated to be indexed"); } List<SearchResult> rvalue = new ArrayList<SearchResult>(); boolean tokenize = false; annotation = attr.getEAnnotation(EMFIndexUtil.SOURCE); if (annotation != null) { if (annotation.getDetails().containsKey(EMFIndexUtil.TOKENIZE_KEY)) { tokenize = true; } } else { // Bail out early if this feature should not be indexed throw new IllegalArgumentException("Attribute is not annotated to be indexed"); } String key = EMFIndexUtil.getKey(attr); DirectoryReader reader = DirectoryReader.open(fsDir); IndexSearcher searcher = new IndexSearcher(reader); try { BooleanQuery bquery = new BooleanQuery(); TermQuery classQuery = new TermQuery(new Term(EMFIndexUtil.ETYPE_KEY, eclass.getName())); bquery.add(classQuery, Occur.MUST); Query query = null; if (tokenize) { QueryParser parser = new QueryParser(version, key, analyzer); query = parser.parse(value); } else { Term term = new Term(key, value); query = new TermQuery(term); } bquery.add(query, Occur.MUST); ScoreDoc[] hits = searcher.search(bquery, null, MAX_SEARCH_RESULT).scoreDocs; // Iterate through the results: for (int i = 0; i < hits.length; i++) { Document hitDoc = searcher.doc(hits[i].doc); SearchResult result = new SearchResult(hitDoc); rvalue.add(result); logger.debug(hitDoc.toString()); } } catch (ParseException e) { logger.error(e.getMessage()); } finally { reader.close(); } return rvalue; }
From source file:com.cohesionforce.search.EMFIndex.java
License:Open Source License
/** * Finds a document matching the object and returns the query used * /*from ww w .j ava 2s.c o m*/ * @param object * to search for in the index * @return the query that returns the document * @throws IllegalArgumentException * if the object is null * @throws IOException * if there are problems searching the index */ protected Query findDocument(EObject object) throws IllegalArgumentException, IOException { if (object == null) { throw new IllegalArgumentException("EObject cannot be null"); } Query rvalue = null; DirectoryReader reader = DirectoryReader.open(fsDir); IndexSearcher searcher = new IndexSearcher(reader); try { BooleanQuery bquery = new BooleanQuery(); TermQuery classQuery = new TermQuery(new Term(EMFIndexUtil.ETYPE_KEY, object.eClass().getName())); bquery.add(classQuery, Occur.MUST); TermQuery uriQuery = new TermQuery( new Term(EMFIndexUtil.DOCUMENT_URI_KEY, object.eResource().getURI().toString())); bquery.add(uriQuery, Occur.MUST); TermQuery fragmentQuery = new TermQuery( new Term(EMFIndexUtil.FRAGMENT_URI_KEY, object.eResource().getURIFragment(object))); bquery.add(fragmentQuery, Occur.MUST); ScoreDoc[] hits = searcher.search(bquery, null, 1).scoreDocs; // Iterate through the results: if (hits.length > 0) { rvalue = bquery; } } finally { reader.close(); } return rvalue; }
From source file:com.core.nlp.query.MoreLikeThis.java
License:Apache License
/** * Create the More like query from a PriorityQueue *///from ww w .j a va 2 s . co m private Query createQuery(PriorityQueue<ScoreTerm> q) { BooleanQuery query = new BooleanQuery(); ScoreTerm scoreTerm; float bestScore = -1; while ((scoreTerm = q.pop()) != null) { TermQuery tq = new TermQuery(new Term(scoreTerm.topField, scoreTerm.word)); if (boost) { if (bestScore == -1) { bestScore = (scoreTerm.score); } float myScore = (scoreTerm.score); tq.setBoost(boostFactor * myScore / bestScore); } try { query.add(tq, BooleanClause.Occur.SHOULD); } catch (BooleanQuery.TooManyClauses ignore) { break; } } return query; }
From source file:com.digitalpebble.ngrams.NGramCorpus.java
License:Apache License
/** Returns the frequency of a term given a list of its tokens **/ public long getOccurrences(final List<String> tokens, boolean lowercase) throws IOException { // queries the Lucene index for the occurrences // for the exact sequence of ngrams String ngrams = Integer.toString(tokens.size()); PhraseQuery pq = new PhraseQuery(); pq.setSlop(0);/* w w w .ja va2 s . com*/ String fieldName = "text"; if (lowercase) fieldName = "lowercase"; for (String s : tokens) { if (lowercase) s = s.toLowerCase(); Term t = new Term(fieldName, s); pq.add(t); } TermQuery tq = new TermQuery(new Term("length", ngrams)); BooleanQuery bq = new BooleanQuery(); bq.add(pq, Occur.MUST); bq.add(tq, Occur.MUST); return getOccurrences(bq); }
From source file:com.docdoku.server.IndexSearcherBean.java
License:Open Source License
public Set<DocumentMasterKey> searchInIndex(String pWorkspaceId, String pContent) { try {/* w ww . j a v a2s .co m*/ Set<DocumentMasterKey> indexedKeys = new HashSet<DocumentMasterKey>(); Query fullNameQuery = new WildcardQuery(new Term("fullName", pWorkspaceId + "/*")); Query contentQuery = new TermQuery(new Term("content", pContent)); BooleanQuery mainQuery = new BooleanQuery(); mainQuery.add(fullNameQuery, BooleanClause.Occur.MUST); mainQuery.add(contentQuery, BooleanClause.Occur.MUST); if (!indexReader.isCurrent()) { //TODO use IndexReader.reopen(); when available indexReader.close(); indexReader = IndexReader.open(FSDirectory.open(new File(indexPath))); } IndexSearcher indexSearcher = new IndexSearcher(indexReader); ScoreDoc[] hits = indexSearcher.search(mainQuery, 500).scoreDocs; for (int i = 0; i < hits.length; i++) { org.apache.lucene.document.Document doc = indexReader.document(hits[i].doc); String fullName = doc.get("fullName"); String[] partRefs = BinaryResource.parseOwnerRef(fullName).split("/"); DocumentMasterKey key = new DocumentMasterKey(pWorkspaceId, partRefs[0], partRefs[1]); indexedKeys.add(key); } return indexedKeys; } catch (CorruptIndexException ex) { throw new EJBException(ex); } catch (IOException ex) { throw new EJBException(ex); } }
From source file:com.dp2345.service.impl.SearchServiceImpl.java
License:Open Source License
@SuppressWarnings("unchecked") @Transactional(readOnly = true)//from w w w. ja va 2 s . co m public Page<Article> search(String keyword, Pageable pageable) { if (StringUtils.isEmpty(keyword)) { return new Page<Article>(); } if (pageable == null) { pageable = new Pageable(); } try { String text = QueryParser.escape(keyword); QueryParser titleParser = new QueryParser(Version.LUCENE_35, "title", new IKAnalyzer()); titleParser.setDefaultOperator(QueryParser.AND_OPERATOR); Query titleQuery = titleParser.parse(text); FuzzyQuery titleFuzzyQuery = new FuzzyQuery(new Term("title", text), FUZZY_QUERY_MINIMUM_SIMILARITY); Query contentQuery = new TermQuery(new Term("content", text)); Query isPublicationQuery = new TermQuery(new Term("isPublication", "true")); BooleanQuery textQuery = new BooleanQuery(); BooleanQuery query = new BooleanQuery(); textQuery.add(titleQuery, Occur.SHOULD); textQuery.add(titleFuzzyQuery, Occur.SHOULD); textQuery.add(contentQuery, Occur.SHOULD); query.add(isPublicationQuery, Occur.MUST); query.add(textQuery, Occur.MUST); FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(query, Article.class); fullTextQuery.setSort(new Sort(new SortField[] { new SortField("isTop", SortField.STRING, true), new SortField(null, SortField.SCORE), new SortField("createDate", SortField.LONG, true) })); fullTextQuery.setFirstResult((pageable.getPageNumber() - 1) * pageable.getPageSize()); fullTextQuery.setMaxResults(pageable.getPageSize()); return new Page<Article>(fullTextQuery.getResultList(), fullTextQuery.getResultSize(), pageable); } catch (ParseException e) { e.printStackTrace(); } return new Page<Article>(); }
From source file:com.dp2345.service.impl.SearchServiceImpl.java
License:Open Source License
@SuppressWarnings("unchecked") @Transactional(readOnly = true)/*from ww w. j a va 2 s . co m*/ public Page<Product> search(String keyword, BigDecimal startPrice, BigDecimal endPrice, OrderType orderType, Pageable pageable) { if (StringUtils.isEmpty(keyword)) { return new Page<Product>(); } if (pageable == null) { pageable = new Pageable(); } try { String text = QueryParser.escape(keyword); TermQuery snQuery = new TermQuery(new Term("sn", text)); Query keywordQuery = new QueryParser(Version.LUCENE_35, "keyword", new IKAnalyzer()).parse(text); QueryParser nameParser = new QueryParser(Version.LUCENE_35, "name", new IKAnalyzer()); nameParser.setDefaultOperator(QueryParser.AND_OPERATOR); Query nameQuery = nameParser.parse(text); FuzzyQuery nameFuzzyQuery = new FuzzyQuery(new Term("name", text), FUZZY_QUERY_MINIMUM_SIMILARITY); TermQuery introductionQuery = new TermQuery(new Term("introduction", text)); TermQuery isMarketableQuery = new TermQuery(new Term("isMarketable", "true")); TermQuery isListQuery = new TermQuery(new Term("isList", "true")); TermQuery isGiftQuery = new TermQuery(new Term("isGift", "false")); BooleanQuery textQuery = new BooleanQuery(); BooleanQuery query = new BooleanQuery(); textQuery.add(snQuery, Occur.SHOULD); textQuery.add(keywordQuery, Occur.SHOULD); textQuery.add(nameQuery, Occur.SHOULD); textQuery.add(nameFuzzyQuery, Occur.SHOULD); textQuery.add(introductionQuery, Occur.SHOULD); query.add(isMarketableQuery, Occur.MUST); query.add(isListQuery, Occur.MUST); query.add(isGiftQuery, Occur.MUST); query.add(textQuery, Occur.MUST); if (startPrice != null && endPrice != null && startPrice.compareTo(endPrice) > 0) { BigDecimal temp = startPrice; startPrice = endPrice; endPrice = temp; } if (startPrice != null && startPrice.compareTo(new BigDecimal(0)) >= 0 && endPrice != null && endPrice.compareTo(new BigDecimal(0)) >= 0) { NumericRangeQuery<Double> numericRangeQuery = NumericRangeQuery.newDoubleRange("price", startPrice.doubleValue(), endPrice.doubleValue(), true, true); query.add(numericRangeQuery, Occur.MUST); } else if (startPrice != null && startPrice.compareTo(new BigDecimal(0)) >= 0) { NumericRangeQuery<Double> numericRangeQuery = NumericRangeQuery.newDoubleRange("price", startPrice.doubleValue(), null, true, false); query.add(numericRangeQuery, Occur.MUST); } else if (endPrice != null && endPrice.compareTo(new BigDecimal(0)) >= 0) { NumericRangeQuery<Double> numericRangeQuery = NumericRangeQuery.newDoubleRange("price", null, endPrice.doubleValue(), false, true); query.add(numericRangeQuery, Occur.MUST); } FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(query, Product.class); SortField[] sortFields = null; if (orderType == OrderType.priceAsc) { sortFields = new SortField[] { new SortField("price", SortField.DOUBLE, false), new SortField("createDate", SortField.LONG, true) }; } else if (orderType == OrderType.priceDesc) { sortFields = new SortField[] { new SortField("price", SortField.DOUBLE, true), new SortField("createDate", SortField.LONG, true) }; } else if (orderType == OrderType.salesDesc) { sortFields = new SortField[] { new SortField("sales", SortField.INT, true), new SortField("createDate", SortField.LONG, true) }; } else if (orderType == OrderType.scoreDesc) { sortFields = new SortField[] { new SortField("score", SortField.INT, true), new SortField("createDate", SortField.LONG, true) }; } else if (orderType == OrderType.dateDesc) { sortFields = new SortField[] { new SortField("createDate", SortField.LONG, true) }; } else { sortFields = new SortField[] { new SortField("isTop", SortField.STRING, true), new SortField(null, SortField.SCORE), new SortField("modifyDate", SortField.LONG, true) }; } fullTextQuery.setSort(new Sort(sortFields)); fullTextQuery.setFirstResult((pageable.getPageNumber() - 1) * pageable.getPageSize()); fullTextQuery.setMaxResults(pageable.getPageSize()); return new Page<Product>(fullTextQuery.getResultList(), fullTextQuery.getResultSize(), pageable); } catch (Exception e) { e.printStackTrace(); } return new Page<Product>(); }
From source file:com.dubture.indexing.core.index.QueryBuilder.java
License:Open Source License
public static Query createFileQuery(IFile file, String referenceId) { String filename = file.getName(); IPath path = file.getFullPath();/*from w w w . j av a 2 s . c om*/ IndexingCorePlugin.debug("Getting fileQuery for " + file.getName()); PrefixQuery query = new PrefixQuery(new Term(IndexField.PATH, path.toString())); TermQuery nameQuery = new TermQuery(new Term(IndexField.FILENAME, filename)); TermQuery refQuery = new TermQuery(new Term(IndexField.TYPE, referenceId)); BooleanQuery boolQuery = new BooleanQuery(); boolQuery.add(query, BooleanClause.Occur.MUST); boolQuery.add(nameQuery, BooleanClause.Occur.MUST); boolQuery.add(refQuery, BooleanClause.Occur.MUST); IndexingCorePlugin.debug("query: " + boolQuery.toString()); return boolQuery; }