List of usage examples for org.apache.lucene.search BooleanQuery BooleanQuery
BooleanQuery
From source file:com.ibm.watson.developer_cloud.professor_languo.pipeline.primary_search.SpanQueryGenerator.java
License:Open Source License
/** * Create a boosted unigram query for the {@link Question}. The boost query matches any Lucene * Documents whose fields specified in {@link #fieldsToSearch} contain any unigram tokens in the * {@link Question}'s title field. The {@link #boostValues} represent the importance of certain * fields over others./*from w w w .j a va 2s . co m*/ * * @param question The {@link Question} from the pipeline. * @param analyzer The Lucene {@link Analyzer} used for tokenizing text fields. * @return The corresponding Lucene {@link Query} * @throws SearchException */ protected Query createBoostUnigramQuery(Question question, Analyzer analyzer) throws SearchException { List<String> tokens; StackExchangeQuestion queryQuestion = (StackExchangeQuestion) question; String title = queryQuestion.getTitleText(); BooleanQuery query = new BooleanQuery(); try { tokens = AnalyzerUtils.collectTokens(analyzer, title); // Loop through all tokens for (int idx = 0; idx < tokens.size(); idx++) { // For each field to search for for (int fieldIdx = 0; fieldIdx < fieldsToSearch.length; fieldIdx++) { TermQuery stq = new TermQuery(new Term(fieldsToSearch[fieldIdx], tokens.get(idx).trim())); stq.setBoost(boostValues[fieldIdx]); query.add(stq, Occur.SHOULD); } // Repeat for every field in fieldsToSearch } // Repeat for each token } catch (IOException e) { throw new SearchException(e); } return query; }
From source file:com.ibm.watson.developer_cloud.professor_languo.pipeline.primary_search.SpanQueryGenerator.java
License:Open Source License
private Query createNgramQueryRecursive(Question question, Analyzer analyzer, int n) throws SearchException { List<String> tokens; StackExchangeQuestion queryQuestion = (StackExchangeQuestion) question; String title = queryQuestion.getTitleText(); List<SpanTermQuery> termQueries = new ArrayList<SpanTermQuery>(); BooleanQuery query = new BooleanQuery(); try {//from w w w . j av a2 s.com tokens = AnalyzerUtils.collectTokens(analyzer, title); // Loop through all tokens for (int idx = 0; idx < tokens.size() - (n - 1); idx++) { // For each field to search for for (int fieldIdx = 0; fieldIdx < fieldsToSearch.length; fieldIdx++) { // Make n-gram term query for (int t = 0; t < n; t++) { SpanTermQuery stq = new SpanTermQuery( new Term(fieldsToSearch[fieldIdx], tokens.get(idx + t).trim())); stq.setBoost((float) (boostValues[fieldIdx] / 2.0)); termQueries.add(stq); } SpanNearQuery spanTitleQuery = new SpanNearQuery( (SpanTermQuery[]) termQueries.toArray(new SpanTermQuery[termQueries.size()]), fuzzFactor, inOrder); query.add(spanTitleQuery, Occur.SHOULD); termQueries.clear(); } // Repeat for every field in fieldsToSearch } // Repeat for each token } catch (IOException e) { throw new SearchException(e); } return query; }
From source file:com.ideabase.repository.common.Query.java
License:Open Source License
/** * Convert this query expression to {@see org.apache.lucene.search.Query} * object.<br> if base query object is defined, the current conditions * will be added with the base query./*from w w w . ja v a 2 s . c om*/ * @return lucene query object is returned. */ public org.apache.lucene.search.Query buildQuery() { if (mBuiltQuery == null) { if (mAndQuries.isEmpty() && mOrQuries.isEmpty() && mBaseQuery == null) { throw new ServiceException(null, "No Query (AND, OR, PREFIX or " + "BaseQuery) is defined."); } // all quries are merged here. final BooleanQuery query = new BooleanQuery(); // All required queries. if (!mAndQuries.isEmpty()) { for (final org.apache.lucene.search.Query andQuery : mAndQuries) { query.add(andQuery, BooleanClause.Occur.MUST); } } // All optional quries. if (!mOrQuries.isEmpty()) { for (final org.apache.lucene.search.Query orQuery : mOrQuries) { query.add(orQuery, BooleanClause.Occur.SHOULD); } } // set score for boosting search result if (mScore != 0.0f) { query.setBoost(mScore); } if (mBaseQuery != null) { // TODO: it set to required. // Add defined based query query.add(mBaseQuery, BooleanClause.Occur.MUST); } mBuiltQuery = query; } return mBuiltQuery; }
From source file:com.ikon.dao.SearchDAO.java
License:Open Source License
/** * Return a list of similar documents.// ww w.jav a 2s. co m */ public NodeResultSet moreLikeThis(String uuid, int maxResults) throws DatabaseException, PathNotFoundException { log.info("moreLikeThis({}, {})", new Object[] { uuid, maxResults }); String[] moreLikeFields = new String[] { "text" }; FullTextSession ftSession = null; Session session = null; Transaction tx = null; try { session = HibernateUtil.getSessionFactory().openSession(); ftSession = Search.getFullTextSession(session); tx = ftSession.beginTransaction(); NodeResultSet result = new NodeResultSet(); MoreLikeThis mlt = new MoreLikeThis(getReader(ftSession, NodeDocument.class)); mlt.setFieldNames(moreLikeFields); mlt.setMaxQueryTerms(10); mlt.setMinDocFreq(1); mlt.setAnalyzer(analyzer); mlt.setMaxWordLen(8); mlt.setMinWordLen(7); mlt.setMinTermFreq(1); String str = NodeDocumentDAO.getInstance().getExtractedText(session, uuid); if (str != null && !str.isEmpty()) { StringReader sr = new StringReader(str); Query likeThisQuery = mlt.like(sr); BooleanQuery query = new BooleanQuery(); query.add(likeThisQuery, Occur.SHOULD); query.add(new TermQuery(new Term("uuid", uuid)), Occur.MUST_NOT); log.info("moreLikeThis.Query: {}", query); if (SEARCH_LUCENE.equals(Config.SECURITY_SEARCH_EVALUATION)) { result = runQueryLucene(ftSession, query, 0, maxResults); } else if (SEARCH_ACCESS_MANAGER_MORE.equals(Config.SECURITY_SEARCH_EVALUATION)) { result = runQueryAccessManagerMore(ftSession, query, 0, maxResults); } else if (SEARCH_ACCESS_MANAGER_WINDOW.equals(Config.SECURITY_SEARCH_EVALUATION)) { result = runQueryAccessManagerWindow(ftSession, query, 0, maxResults); } else if (SEARCH_ACCESS_MANAGER_LIMITED.equals(Config.SECURITY_SEARCH_EVALUATION)) { result = runQueryAccessManagerLimited(ftSession, query, 0, maxResults); } } else { log.warn("Document has not text extracted: {}", uuid); } HibernateUtil.commit(tx); log.debug("moreLikeThis: {}", result); return result; } catch (IOException e) { HibernateUtil.rollback(tx); throw new DatabaseException(e.getMessage(), e); } catch (InvalidTokenOffsetsException e) { HibernateUtil.rollback(tx); throw new DatabaseException(e.getMessage(), e); } catch (HibernateException e) { HibernateUtil.rollback(tx); throw new DatabaseException(e.getMessage(), e); } finally { HibernateUtil.close(ftSession); HibernateUtil.close(session); } }
From source file:com.ikon.module.db.DbSearchModule.java
License:Open Source License
/** * Prepare statement//from w w w .ja v a 2 s . c om */ public Query prepareStatement(QueryParams params) throws IOException, ParseException, RepositoryException, DatabaseException { log.debug("prepareStatement({})", params); BooleanQuery query = new BooleanQuery(); // Clean params params.setName(params.getName() != null ? params.getName().trim() : ""); params.setContent(params.getContent() != null ? params.getContent().trim() : ""); params.setKeywords(params.getKeywords() != null ? params.getKeywords() : new HashSet<String>()); params.setCategories(params.getCategories() != null ? params.getCategories() : new HashSet<String>()); params.setMimeType(params.getMimeType() != null ? params.getMimeType().trim() : ""); params.setAuthor(params.getAuthor() != null ? params.getAuthor().trim() : ""); params.setPath(params.getPath() != null ? params.getPath().trim() : ""); params.setMailSubject(params.getMailSubject() != null ? params.getMailSubject().trim() : ""); params.setMailFrom(params.getMailFrom() != null ? params.getMailFrom().trim() : ""); params.setMailTo(params.getMailTo() != null ? params.getMailTo().trim() : ""); params.setProperties( params.getProperties() != null ? params.getProperties() : new HashMap<String, String>()); // Domains boolean document = (params.getDomain() & QueryParams.DOCUMENT) != 0; boolean folder = (params.getDomain() & QueryParams.FOLDER) != 0; boolean mail = (params.getDomain() & QueryParams.MAIL) != 0; log.debug("doc={}, fld={}, mail={}", new Object[] { document, folder, mail }); // Path to UUID conversion and in depth recursion List<String> pathInDepth = new ArrayList<String>(); if (!params.getPath().equals("") && !params.getPath().equals("/" + Repository.ROOT) && !params.getPath().equals("/" + Repository.CATEGORIES) && !params.getPath().equals("/" + Repository.TEMPLATES) && !params.getPath().equals("/" + Repository.PERSONAL) && !params.getPath().equals("/" + Repository.MAIL) && !params.getPath().equals("/" + Repository.TRASH)) { try { String uuid = NodeBaseDAO.getInstance().getUuidFromPath(params.getPath()); log.debug("Path in depth: {} => {}", uuid, NodeBaseDAO.getInstance().getPathFromUuid(uuid)); pathInDepth.add(uuid); for (String uuidChild : SearchDAO.getInstance().findFoldersInDepth(uuid)) { log.debug("Path in depth: {} => {}", uuidChild, NodeBaseDAO.getInstance().getPathFromUuid(uuidChild)); pathInDepth.add(uuidChild); } } catch (PathNotFoundException e) { throw new RepositoryException("Path Not Found: " + e.getMessage()); } } /** * DOCUMENT */ if (document) { BooleanQuery queryDocument = new BooleanQuery(); Term tEntity = new Term("_hibernate_class", NodeDocument.class.getCanonicalName()); queryDocument.add(new TermQuery(tEntity), BooleanClause.Occur.MUST); if (!params.getContent().equals("")) { for (StringTokenizer st = new StringTokenizer(params.getContent(), " "); st.hasMoreTokens();) { Term t = new Term("text", st.nextToken().toLowerCase()); queryDocument.add(new WildcardQuery(t), BooleanClause.Occur.MUST); } } if (!params.getName().equals("")) { if (!params.getName().contains("*") && !params.getName().contains("?")) { params.setName("*" + params.getName() + "*"); } Term t = new Term("name", params.getName().toLowerCase()); queryDocument.add(new WildcardQuery(t), BooleanClause.Occur.MUST); } if (!params.getPath().equals("")) { if (pathInDepth.isEmpty()) { Term t = new Term("context", PathUtils.fixContext(params.getPath())); queryDocument.add(new WildcardQuery(t), BooleanClause.Occur.MUST); } else { BooleanQuery parent = new BooleanQuery(); for (String uuid : pathInDepth) { Term tChild = new Term("parent", uuid); parent.add(new TermQuery(tChild), BooleanClause.Occur.SHOULD); } queryDocument.add(parent, BooleanClause.Occur.MUST); } } if (!params.getMimeType().equals("")) { Term t = new Term("mimeType", params.getMimeType()); queryDocument.add(new TermQuery(t), BooleanClause.Occur.MUST); } if (!params.getAuthor().equals("")) { Term t = new Term("author", params.getAuthor()); queryDocument.add(new TermQuery(t), BooleanClause.Occur.MUST); } if (params.getLastModifiedFrom() != null && params.getLastModifiedTo() != null) { Date from = params.getLastModifiedFrom().getTime(); String sFrom = DAY_FORMAT.format(from); Date to = params.getLastModifiedTo().getTime(); String sTo = DAY_FORMAT.format(to); queryDocument.add(new TermRangeQuery("lastModified", sFrom, sTo, true, true), BooleanClause.Occur.MUST); } appendCommon(params, queryDocument); query.add(queryDocument, BooleanClause.Occur.SHOULD); } /** * FOLDER */ if (folder) { BooleanQuery queryFolder = new BooleanQuery(); Term tEntity = new Term("_hibernate_class", NodeFolder.class.getCanonicalName()); queryFolder.add(new TermQuery(tEntity), BooleanClause.Occur.MUST); if (!params.getName().equals("")) { Term t = new Term("name", params.getName().toLowerCase()); queryFolder.add(new WildcardQuery(t), BooleanClause.Occur.MUST); } if (!params.getPath().equals("")) { if (pathInDepth.isEmpty()) { Term t = new Term("context", PathUtils.fixContext(params.getPath())); queryFolder.add(new WildcardQuery(t), BooleanClause.Occur.MUST); } else { BooleanQuery parent = new BooleanQuery(); for (String uuid : pathInDepth) { Term tChild = new Term("parent", uuid); parent.add(new TermQuery(tChild), BooleanClause.Occur.SHOULD); } queryFolder.add(parent, BooleanClause.Occur.MUST); } } appendCommon(params, queryFolder); query.add(queryFolder, BooleanClause.Occur.SHOULD); } /** * MAIL */ if (mail) { BooleanQuery queryMail = new BooleanQuery(); Term tEntity = new Term("_hibernate_class", NodeMail.class.getCanonicalName()); queryMail.add(new TermQuery(tEntity), BooleanClause.Occur.MUST); if (!params.getPath().equals("")) { if (pathInDepth.isEmpty()) { Term t = new Term("context", PathUtils.fixContext(params.getPath())); queryMail.add(new WildcardQuery(t), BooleanClause.Occur.MUST); } else { BooleanQuery parent = new BooleanQuery(); for (String uuid : pathInDepth) { Term tChild = new Term("parent", uuid); parent.add(new TermQuery(tChild), BooleanClause.Occur.SHOULD); } queryMail.add(parent, BooleanClause.Occur.MUST); } } if (!params.getContent().equals("")) { for (StringTokenizer st = new StringTokenizer(params.getContent(), " "); st.hasMoreTokens();) { Term t = new Term("content", st.nextToken().toLowerCase()); queryMail.add(new WildcardQuery(t), BooleanClause.Occur.MUST); } } if (!params.getMailSubject().equals("")) { Term t = new Term("subject", params.getMailSubject().toLowerCase()); queryMail.add(new WildcardQuery(t), BooleanClause.Occur.MUST); } if (!params.getMailFrom().equals("")) { Term t = new Term("from", params.getMailFrom().toLowerCase()); queryMail.add(new WildcardQuery(t), BooleanClause.Occur.MUST); } if (!params.getMailTo().equals("")) { Term t = new Term("to", params.getMailTo().toLowerCase()); queryMail.add(new WildcardQuery(t), BooleanClause.Occur.MUST); } if (!params.getMimeType().equals("")) { Term t = new Term("mimeType", params.getMimeType()); queryMail.add(new TermQuery(t), BooleanClause.Occur.MUST); } appendCommon(params, queryMail); query.add(queryMail, BooleanClause.Occur.SHOULD); } log.debug("prepareStatement: {}", query.toString()); return query; }
From source file:com.ikon.module.db.stuff.ReadAccessFilterFactory.java
License:Open Source License
@Factory public Filter buildFilter() { log.debug("buildFilter()"); if (SearchDAO.SEARCH_LUCENE.equals(Config.SECURITY_SEARCH_EVALUATION)) { String user = PrincipalUtils.getUser(); Set<String> roles = PrincipalUtils.getRoles(); if (roles.contains(Config.DEFAULT_ADMIN_ROLE)) { // An user with AdminRole has total access return null; } else if (Config.ADMIN_USER.equals(user) || Config.SYSTEM_USER.equals(user)) { // An "tAdmin" or "system" user has total access return null; } else {//from w ww .j a v a2s .c o m BooleanQuery query = new BooleanQuery(); Term termUser = new Term("userPermission", user); query.add(new TermQuery(termUser), BooleanClause.Occur.SHOULD); for (String role : roles) { Term termRole = new Term("rolePermission", role); query.add(new TermQuery(termRole), BooleanClause.Occur.SHOULD); } log.info("buildFilter: {}", query); Filter filter = new QueryWrapperFilter(query); return filter; } } else { return null; } }
From source file:com.intuit.tank.search.lucene.LuceneServiceTest.java
License:Open Source License
/** * Run the List<Document> search(Query,boolean) method test. * * @throws Exception//from ww w .j av a 2s . c o m * * @generatedBy CodePro at 12/16/14 3:36 PM */ @Test public void testSearch_1() throws Exception { LuceneService fixture = new LuceneService(); Query query = new BooleanQuery(); boolean prefixWildCard = true; List<Document> result = fixture.search(query, prefixWildCard); // An unexpected exception was thrown in user code while executing this test: // java.lang.NullPointerException // at org.apache.lucene.search.Query.mergeBooleanQueries(Query.java:182) assertNotNull(result); }
From source file:com.intuit.tank.search.lucene.LuceneServiceTest.java
License:Open Source License
/** * Run the List<Document> search(Query,boolean) method test. * * @throws Exception/*from w ww. jav a2s . co m*/ * * @generatedBy CodePro at 12/16/14 3:36 PM */ @Test public void testSearch_2() throws Exception { LuceneService fixture = new LuceneService(); Query query = new BooleanQuery(); boolean prefixWildCard = true; List<Document> result = fixture.search(query, prefixWildCard); // An unexpected exception was thrown in user code while executing this test: // java.lang.NullPointerException // at org.apache.lucene.search.Query.mergeBooleanQueries(Query.java:182) assertNotNull(result); }
From source file:com.intuit.tank.search.util.MultiSearchParam.java
License:Open Source License
/** * {@inheritDoc}//from ww w. ja v a 2s. co m */ public Query getLuceneQuery() { BooleanQuery ret = new BooleanQuery(); for (SearchParam param : params) { if (param != null) { ret.add(new BooleanClause(param.getLuceneQuery(), operator.occur)); } } return ret; }
From source file:com.intuit.tank.search.util.MustNotFieldSearchParam.java
License:Open Source License
/** * {@inheritDoc}//from w ww. ja v a 2s. co m */ public Query getLuceneQuery() { BooleanQuery booleanQuery = new BooleanQuery(); booleanQuery.add(new TermQuery(new Term(fieldName, value)), Occur.MUST_NOT); return booleanQuery; }