org.apache.lucene.queryParser.ParseException Java Examples
The following examples show how to use
org.apache.lucene.queryParser.ParseException.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NGramQueryParser.java From uyuni with GNU General Public License v2.0 | 6 votes |
protected Query getFieldQuery(String defaultField, String queryText) throws ParseException { Query orig = super.getFieldQuery(defaultField, queryText); if (!(orig instanceof PhraseQuery)) { log.debug("Returning default query. No phrase query translation."); return orig; } /** * A ngram when parsed will become a series of smaller search terms, * these terms are grouped together into a PhraseQuery. We are taking * that PhraseQuery and breaking out each ngram term then combining all * ngrams together to form a BooleanQuery. */ PhraseQuery pq = (PhraseQuery)orig; return new NGramQuery(pq, useMust); }
Example #2
Source File: NGramQueryParser.java From spacewalk with GNU General Public License v2.0 | 6 votes |
protected Query getFieldQuery(String defaultField, String queryText) throws ParseException { Query orig = super.getFieldQuery(defaultField, queryText); if (!(orig instanceof PhraseQuery)) { log.debug("Returning default query. No phrase query translation."); return orig; } /** * A ngram when parsed will become a series of smaller search terms, * these terms are grouped together into a PhraseQuery. We are taking * that PhraseQuery and breaking out each ngram term then combining all * ngrams together to form a BooleanQuery. */ PhraseQuery pq = (PhraseQuery)orig; return new NGramQuery(pq, useMust); }
Example #3
Source File: SearchDemo.java From document-management-system with GNU General Public License v2.0 | 6 votes |
/** * Search in documents */ private static void search(Directory index, Analyzer analyzer, String str) throws ParseException, CorruptIndexException, IOException { IndexReader reader = IndexReader.open(index); IndexSearcher searcher = new IndexSearcher(reader); TopScoreDocCollector collector = TopScoreDocCollector.create(NUM_HITS, true); //Query q = new QueryParser(Config.LUCENE_VERSION, DOC_FIELD, analyzer).parse(str); Query q = new WildcardQuery(new Term(DOC_FIELD, str)); System.out.println("Query: " + q); searcher.search(q, collector); ScoreDoc[] hits = collector.topDocs().scoreDocs; System.out.println("Found " + hits.length + " hits."); for (int i = 0; i < hits.length; ++i) { int docId = hits[i].doc; Document d = searcher.doc(docId); System.out.println((i + 1) + ". " + d.get(DOC_FIELD)); } searcher.close(); }
Example #4
Source File: LuceneContentSvcImpl.java From Lottery with GNU General Public License v2.0 | 6 votes |
@Transactional(readOnly = true) public Pagination searchPage(Directory dir, String queryString,String category,String workplace, Integer siteId, Integer channelId, Date startDate, Date endDate, int pageNo, int pageSize) throws CorruptIndexException, IOException, ParseException { Searcher searcher = new IndexSearcher(dir); try { Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30); Query query = LuceneContent.createQuery(queryString,category,workplace, siteId, channelId, startDate, endDate, analyzer); TopDocs docs = searcher.search(query, pageNo * pageSize); Pagination p = LuceneContent.getResultPage(searcher, docs, pageNo, pageSize); List<?> ids = p.getList(); List<Content> contents = new ArrayList<Content>(ids.size()); for (Object id : ids) { contents.add(contentMng.findById((Integer) id)); } p.setList(contents); return p; } finally { searcher.close(); } }
Example #5
Source File: LuceneContentSvcImpl.java From Lottery with GNU General Public License v2.0 | 6 votes |
@Transactional(readOnly = true) public Integer createIndex(Integer siteId, Integer channelId, Date startDate, Date endDate, Integer startId, Integer max, Directory dir) throws IOException, ParseException { boolean exist = IndexReader.indexExists(dir); IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer( Version.LUCENE_30), !exist, IndexWriter.MaxFieldLength.LIMITED); try { if (exist) { LuceneContent.delete(siteId, channelId, startDate, endDate, writer); } Integer lastId = luceneContentDao.index(writer, siteId, channelId, startDate, endDate, startId, max); writer.optimize(); return lastId; } finally { writer.close(); } }
Example #6
Source File: AnchorIndexer.java From tagme with Apache License 2.0 | 6 votes |
static int freq(Set<String> anchors, IndexSearcher index, QueryParser queryParser) throws IOException { //int sum = 0; BitSet bits = new BitSet(index.maxDoc()); for(String a : anchors) { try { Query q = queryParser.parse(String.format(QUERY_PATTERN, QueryParser.escape(a))); TotalHitCountCollectorSet results = new TotalHitCountCollectorSet(bits); index.search(q, results); //sum += results.getTotalHits(); } catch (ParseException e) { } } return bits.cardinality(); }
Example #7
Source File: AbstractEntitySearcher.java From webdsl with Apache License 2.0 | 5 votes |
private String getLuceneQueryAsString( ) { if ( updateLuceneQuery ) { try { luceneQuery = createLuceneQuery( rootQD ); luceneQueryNoFacetFilters = luceneQuery; } catch ( ParseException e ) { // TODO Auto-generated catch block Logger.error(e); return luceneQuery.toString( "Error occurred during query parsing" ); } updateLuceneQuery = false; updateHighlightQuery = updateBoboBrowseResult = updateFullTextQuery = updateLuceneQueryWithFacetSelection = true; } return luceneQueryNoFacetFilters.toString( ); }
Example #8
Source File: NGramQueryParser.java From uyuni with GNU General Public License v2.0 | 5 votes |
/** * This will look to see if "part1" or "part2" are strings of all digits, * if they are, then they will be converted to a lexicographically safe string * representation, then passed into the inherited getRangeQuery(). This is needed when * comparing something like "4" to be less than "10". * If the strings don't fit the pattern of all digits, then they get passed through * to the inherited getRangeQuery(). */ protected Query getRangeQuery(String field, String part1, String part2, boolean inclusive) throws ParseException { if (isDate(part1) && isDate(part2)) { if (log.isDebugEnabled()) { log.debug("Detected passed in terms are dates, creating " + "ConstantScoreRangeQuery(" + field + ", " + part1 + ", " + part2 + ", " + inclusive + ", " + inclusive); } return new ConstantScoreRangeQuery(field, part1, part2, inclusive, inclusive); } String newPart1 = part1; String newPart2 = part2; String regEx = "(\\d)*"; Pattern pattern = Pattern.compile(regEx); Matcher matcher1 = pattern.matcher(part1); Matcher matcher2 = pattern.matcher(part2); if (matcher1.matches() && matcher2.matches()) { newPart1 = NumberTools.longToString(Long.parseLong(part1)); newPart2 = NumberTools.longToString(Long.parseLong(part2)); if (log.isDebugEnabled()) { log.debug("NGramQueryParser.getRangeQuery() Converted " + part1 + " to " + newPart1 + ", Converted " + part2 + " to " + newPart2); } } return super.getRangeQuery(field, newPart1, newPart2, inclusive); }
Example #9
Source File: LuceneContentSvcImpl.java From Lottery with GNU General Public License v2.0 | 5 votes |
@Transactional(readOnly = true) public void deleteIndex(Integer contentId, Directory dir) throws IOException, ParseException { boolean exist = IndexReader.indexExists(dir); if (exist) { IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer( Version.LUCENE_30), false, IndexWriter.MaxFieldLength.LIMITED); try { LuceneContent.delete(contentId, writer); } finally { writer.close(); } } }
Example #10
Source File: LuceneContentSvcImpl.java From Lottery with GNU General Public License v2.0 | 5 votes |
@Transactional(readOnly = true) public void deleteIndex(Integer contentId) throws IOException, ParseException { String path = realPathResolver.get(Constants.LUCENE_PATH); Directory dir = new SimpleFSDirectory(new File(path)); deleteIndex(contentId, dir); }
Example #11
Source File: AbstractEntitySearcher.java From webdsl with Apache License 2.0 | 5 votes |
private Query createLuceneQuery( QueryDef qd ) throws ParseException { // log("*********createLuceneQuery on query def with children: " + qd.children.size()); // log("going to parse querydef:" + qd.toString()); Query toReturn; switch ( qd.queryType ){ case PARSED_LUCENE : toReturn = qd.parsedQuery; break; case PARSED_STRING : toReturn = getParsedQuery( qd ); break; case RANGE : toReturn = createRangeQuery( qd ); break; case PHRASE : toReturn = createMultiFieldPhraseQuery( qd ); break; // case FUZZY : toReturn = createMultiFieldcQuery( qd ); break; case TEXT : toReturn = createMultiFieldQuery( qd ); break; case MATCH_ALL : toReturn = createMatchAllQuery( ); break; case REGEX : toReturn = createRegexQuery( qd ); break; default : toReturn = null; break; } // if(toReturn == null) // log("toReturn after switch: NULL"); // else // log("toReturn after switch: " + toReturn.toString()); if( !qd.children.isEmpty( ) ) { BooleanQuery booleanQuery = new BooleanQuery( ); if( toReturn != null ) booleanQuery.add( toReturn, qd.occur ); // int i=0; for ( QueryDef child : qd.children ){ // log("createLuceneQuery on child" + i++ ); booleanQuery.add( createLuceneQuery( child ), child.occur ); } toReturn = booleanQuery; } return toReturn; }
Example #12
Source File: AbstractEntitySearcher.java From webdsl with Apache License 2.0 | 5 votes |
private Query createRangeQuery( QueryDef qd ) throws ParseException { QueryBuilder builder = getFullTextSession( ).getSearchFactory( ).buildQueryBuilder( ).forEntity( entityClass ).get( ); RangeMatchingContext fieldContext = builder.range( ).onField( qd.fields[0] ); for (int i = 1; i < qd.fields.length; i++) { fieldContext = fieldContext.andField(qd.fields[i]); } FromRangeContext<Object> fromContext = fieldContext.from(qd.min); RangeTerminationExcludable toContext = qd.includeMin? fromContext.to( qd.max ) : fromContext.excludeLimit( ).to( qd.max ); return qd.includeMax ? toContext.createQuery( ) : toContext.excludeLimit( ).createQuery( ); }
Example #13
Source File: AbstractEntitySearcher.java From webdsl with Apache License 2.0 | 5 votes |
private Query getBoboQuery( ) { Query boboQuery = luceneQueryNoFacetFilters; boolean hasNamespaceConstraint = ( namespaceConstraint != null && !namespaceConstraint.isEmpty( ) ); boolean hasFieldConstraint = ( fieldConstraints != null && !fieldConstraints.isEmpty( ) ); boolean hasRangeFacetSelection = (rangeFacetQuery != null && !rangeFacetQuery.clauses().isEmpty() ); if ( hasNamespaceConstraint || hasFieldConstraint || hasRangeFacetSelection ) { //Apply field constraints and namespace constraints through query, when enabled BooleanQuery bq = new BooleanQuery( ); bq.add( luceneQueryNoFacetFilters, Occur.MUST ); if ( hasFieldConstraint ) { for ( Entry<String, String> kv : fieldConstraints.entrySet( ) ) { QueryParser qp = new QueryParser( LUCENEVERSION, kv.getKey( ), analyzer ); try { if ( allowLuceneSyntax ) bq.add( qp.parse( kv.getValue( ) ), Occur.MUST ); else bq.add( qp.parse( QueryParser.escape( kv.getValue( ) ) ), Occur.MUST ); } catch ( ParseException e ) { Logger.error(e); } } } if ( hasNamespaceConstraint ) { bq.add( new TermQuery( new Term( SearchHelper.NAMESPACEFIELD, namespaceConstraint ) ), Occur.MUST ); } if ( hasRangeFacetSelection ) { for( BooleanClause clause : rangeFacetQuery){ bq.add( clause ); } } boboQuery = bq; } return boboQuery; }
Example #14
Source File: SearchInputController.java From olat with Apache License 2.0 | 5 votes |
protected SearchResults doFuzzySearch(final UserRequest ureq, final String searchString, final List<String> condSearchStrings, final String parentCtxt, final String docType, final String rsrcUrl, final int firstResult, final int maxReturns) throws QueryException, ParseException, ServiceNotAvailableException { hideDidYouMeanWords(); final String query = getQueryString(searchString, true); final List<String> condQueries = getCondQueryStrings(condSearchStrings, parentCtxt, docType, rsrcUrl); SearchResults searchResults = searchCache.get(getQueryCacheKey(firstResult, query, condQueries)); if (searchResults == null) { searchResults = searchClient.doSearch(query, condQueries, ureq.getIdentity(), ureq.getUserSession().getRoles(), firstResult, maxReturns, true); searchCache.put(getQueryCacheKey(firstResult, query, condQueries), searchResults); } return searchResults; }
Example #15
Source File: LuceneContentSvcImpl.java From Lottery with GNU General Public License v2.0 | 5 votes |
@Transactional(readOnly = true) public Integer createIndex(Integer siteId, Integer channelId, Date startDate, Date endDate, Integer startId, Integer max) throws IOException, ParseException { String path = realPathResolver.get(Constants.LUCENE_PATH); Directory dir = new SimpleFSDirectory(new File(path)); return createIndex(siteId, channelId, startDate, endDate, startId, max, dir); }
Example #16
Source File: JmsSearchProvider.java From olat with Apache License 2.0 | 5 votes |
/** * Delegates execution to the searchService. * */ public SearchResults doSearch(final String queryString, final List<String> condQueries, final Identity identity, final Roles roles, final int firstResult, final int maxResults, final boolean doHighlighting) throws ServiceNotAvailableException, ParseException, QueryException { if (searchService == null) { throw new AssertException("searchService in ClusteredSearchProvider is null, please check the search configuration!"); } return searchService.doSearch(queryString, condQueries, identity, roles, firstResult, maxResults, doHighlighting); }
Example #17
Source File: JmsSearchProvider.java From olat with Apache License 2.0 | 5 votes |
/** * Delegates execution to the searchService. * */ public SearchResults doSearch(final String queryString, final List<String> condQueries, final Identity identity, final Roles roles, final int firstResult, final int maxResults, final boolean doHighlighting) throws ServiceNotAvailableException, ParseException, QueryException { if (searchService == null) { throw new AssertException("searchService in ClusteredSearchProvider is null, please check the search configuration!"); } return searchService.doSearch(queryString, condQueries, identity, roles, firstResult, maxResults, doHighlighting); }
Example #18
Source File: NGramQueryParser.java From spacewalk with GNU General Public License v2.0 | 5 votes |
/** * This will look to see if "part1" or "part2" are strings of all digits, * if they are, then they will be converted to a lexicographically safe string * representation, then passed into the inherited getRangeQuery(). This is needed when * comparing something like "4" to be less than "10". * If the strings don't fit the pattern of all digits, then they get passed through * to the inherited getRangeQuery(). */ protected Query getRangeQuery(String field, String part1, String part2, boolean inclusive) throws ParseException { if (isDate(part1) && isDate(part2)) { if (log.isDebugEnabled()) { log.debug("Detected passed in terms are dates, creating " + "ConstantScoreRangeQuery(" + field + ", " + part1 + ", " + part2 + ", " + inclusive + ", " + inclusive); } return new ConstantScoreRangeQuery(field, part1, part2, inclusive, inclusive); } String newPart1 = part1; String newPart2 = part2; String regEx = "(\\d)*"; Pattern pattern = Pattern.compile(regEx); Matcher matcher1 = pattern.matcher(part1); Matcher matcher2 = pattern.matcher(part2); if (matcher1.matches() && matcher2.matches()) { newPart1 = NumberTools.longToString(Long.parseLong(part1)); newPart2 = NumberTools.longToString(Long.parseLong(part2)); if (log.isDebugEnabled()) { log.debug("NGramQueryParser.getRangeQuery() Converted " + part1 + " to " + newPart1 + ", Converted " + part2 + " to " + newPart2); } } return super.getRangeQuery(field, newPart1, newPart2, inclusive); }
Example #19
Source File: SpecialMultiFieldQueryParser.java From webdsl with Apache License 2.0 | 5 votes |
@Override public Query parse(String query) throws ParseException{ Query q = super.parse(query); if(defaultAndnStopword){ inQueryFix = true; Query fix = super.parse(query); List<BooleanClause> clauses = new ArrayList<BooleanClause>(); clauses.add(new BooleanClause(q,Occur.SHOULD)); clauses.add(new BooleanClause(fix,Occur.SHOULD)); return getBooleanQuery(clauses); } return q; }
Example #20
Source File: SearchDemo.java From document-management-system with GNU General Public License v2.0 | 5 votes |
/** * Add documents */ private static void add(Directory index, Analyzer analyzer, String str) throws IOException, ParseException { IndexWriterConfig config = new IndexWriterConfig(Config.LUCENE_VERSION, analyzer); IndexWriter w = new IndexWriter(index, config); Document doc = new Document(); doc.add(new Field(DOC_FIELD, str, Field.Store.YES, Field.Index.ANALYZED)); w.addDocument(doc); w.close(); }
Example #21
Source File: WikiPageIdx.java From document-management-system with GNU General Public License v2.0 | 5 votes |
/** * Perform search */ public static TopDocs performSearch(String field, String qs) throws IOException, ParseException { IndexSearcher searcher = Indexer.getIndexSearcher(); QueryParser parser = new QueryParser(Config.LUCENE_VERSION, field, Indexer.getAnalyzer()); Query query = parser.parse(qs); TopDocs result = searcher.search(query, Indexer.HITS_PER_PAGE); return result; }
Example #22
Source File: LuceneContentSvcImpl.java From Lottery with GNU General Public License v2.0 | 5 votes |
public void updateIndex(Content content, Directory dir) throws IOException, ParseException { boolean exist = IndexReader.indexExists(dir); IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer( Version.LUCENE_30), !exist, IndexWriter.MaxFieldLength.LIMITED); try { if (exist) { LuceneContent.delete(content.getId(), writer); } writer.addDocument(LuceneContent.createDocument(content)); } finally { writer.close(); } }
Example #23
Source File: DefaultIndexExecutor.java From boubei-tss with Apache License 2.0 | 5 votes |
protected Query createIndexQuery(String filedName, String searchStr) throws ParseException { Analyzer analyzer = AnalyzerFactory.createAnalyzer(searchStr); Query query1 = new QueryParser(filedName, analyzer).parse(searchStr); //TermQuery query2 = new TermQuery(new Term(FIELD_ISSUEDATE, searchStr)); TermQuery query3 = new TermQuery(new Term(FIELD_CREATETIME, searchStr)); BooleanQuery booleanQuery = new BooleanQuery(); booleanQuery.add(query1, BooleanClause.Occur.SHOULD); //booleanQuery.add(query2, BooleanClause.Occur.SHOULD); booleanQuery.add(query3, BooleanClause.Occur.SHOULD); return booleanQuery; }
Example #24
Source File: LuceneContentSvcImpl.java From Lottery with GNU General Public License v2.0 | 5 votes |
@Transactional(readOnly = true) public Pagination searchPage(String path, String queryString,String category,String workplace, Integer siteId, Integer channelId, Date startDate, Date endDate, int pageNo, int pageSize) throws CorruptIndexException, IOException, ParseException { Directory dir = new SimpleFSDirectory(new File(path)); return searchPage(dir, queryString, category,workplace,siteId, channelId, startDate, endDate, pageNo, pageSize); }
Example #25
Source File: LuceneContentSvcImpl.java From Lottery with GNU General Public License v2.0 | 5 votes |
@Transactional(readOnly = true) public List<Content> searchList(String path, String queryString,String category,String workplace, Integer siteId, Integer channelId, Date startDate, Date endDate, int first, int max) throws CorruptIndexException, IOException, ParseException { Directory dir = new SimpleFSDirectory(new File(path)); return searchList(dir, queryString,category,workplace, siteId, channelId, startDate, endDate, first, max); }
Example #26
Source File: LuceneContentSvcImpl.java From Lottery with GNU General Public License v2.0 | 5 votes |
@Transactional(readOnly = true) public List<Content> searchList(Directory dir, String queryString,String category,String workplace, Integer siteId, Integer channelId, Date startDate, Date endDate, int first, int max) throws CorruptIndexException, IOException, ParseException { Searcher searcher = new IndexSearcher(dir); try { Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30); Query query = LuceneContent.createQuery(queryString,category,workplace, siteId, channelId, startDate, endDate, analyzer); if (first < 0) { first = 0; } if (max < 0) { max = 0; } TopDocs docs = searcher.search(query, first + max); List<Integer> ids = LuceneContent.getResultList(searcher, docs, first, max); List<Content> contents = new ArrayList<Content>(ids.size()); for (Object id : ids) { contents.add(contentMng.findById((Integer) id)); } return contents; } finally { searcher.close(); } }
Example #27
Source File: AbstractEntitySearcher.java From webdsl with Apache License 2.0 | 5 votes |
private Query createMultiFieldQuery( QueryDef qd ) throws ParseException { if ( allowLuceneSyntax ) return getQueryParser( qd ).parse( qd.query ); else { return getQueryParser( qd ).parse( SpecialMultiFieldQueryParser.escape( qd.query ) ); } }
Example #28
Source File: IndexSearcher.java From marathonv5 with Apache License 2.0 | 5 votes |
public Map<DocumentType, List<SearchResult>> search(String searchString) throws ParseException { Map<DocumentType, List<SearchResult>> resultMap = new TreeMap<DocumentType, List<SearchResult>>(); try { Query query = parser.parse(searchString); final SecondPassGroupingCollector collector = new SecondPassGroupingCollector("documentType", searchGroups, Sort.RELEVANCE, null, 5, true, false, true); searcher.search(query, collector); final TopGroups groups = collector.getTopGroups(0); for (GroupDocs groupDocs : groups.groups) { DocumentType docType = DocumentType.valueOf(groupDocs.groupValue); List<SearchResult> results = new ArrayList<SearchResult>(); for (ScoreDoc scoreDoc : groupDocs.scoreDocs) { Document doc = searcher.doc(scoreDoc.doc); SearchResult result = new SearchResult( docType, doc.get("name"), doc.get("url"), doc.get("className"), doc.get("package"), doc.get("ensemblePath"), doc.get("shortDescription") ); results.add(result); } resultMap.put(docType, results); } } catch (IOException e) { e.printStackTrace(); } return resultMap; }
Example #29
Source File: IndexSearcher.java From marathonv5 with Apache License 2.0 | 5 votes |
public Map<DocumentType, List<SearchResult>> search(String searchString) throws ParseException { Map<DocumentType, List<SearchResult>> resultMap = new TreeMap<DocumentType, List<SearchResult>>(); try { Query query = parser.parse(searchString); final SecondPassGroupingCollector collector = new SecondPassGroupingCollector("documentType", searchGroups, Sort.RELEVANCE, null, 5, true, false, true); searcher.search(query, collector); final TopGroups groups = collector.getTopGroups(0); for (GroupDocs groupDocs : groups.groups) { DocumentType docType = DocumentType.valueOf(groupDocs.groupValue); List<SearchResult> results = new ArrayList<SearchResult>(); for (ScoreDoc scoreDoc : groupDocs.scoreDocs) { Document doc = searcher.doc(scoreDoc.doc); SearchResult result = new SearchResult( docType, doc.get("name"), doc.get("url"), doc.get("className"), doc.get("package"), doc.get("ensemblePath"), doc.get("shortDescription") ); results.add(result); } resultMap.put(docType, results); } } catch (IOException e) { e.printStackTrace(); } return resultMap; }
Example #30
Source File: LuceneContentSvc.java From Lottery with GNU General Public License v2.0 | 4 votes |
public List<Content> searchList(String path, String queryString,String category,String workplace, Integer siteId, Integer channelId, Date startDate, Date endDate, int pageNo, int pageSize) throws CorruptIndexException, IOException, ParseException;