Java Code Examples for org.apache.lucene.queryParser.QueryParser#parse()

The following examples show how to use org.apache.lucene.queryParser.QueryParser#parse() . 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: CrawlerTask.java    From JPPF with Apache License 2.0 6 votes vote down vote up
/**
 * Search for the user-specified query expression in the current page.
 * @throws Exception if an error occurs.
 */
private void search() throws Exception {
  final QueryParser parser = new QueryParser("contents", new StandardAnalyzer());
  final Query q = parser.parse(query);

  final MemoryIndex index = new MemoryIndex();
  final Link link = new Link(url);
  final PageData pageData = new SimpleHttpClientParser().load(link);
  index.addField("contents", pageData.getData().toString(), new StandardAnalyzer());
  final IndexSearcher searcher = index.createSearcher();
  final Hits hits = searcher.search(q);
  @SuppressWarnings("rawtypes")
  final Iterator it = hits.iterator();
  float relevance = 0f;
  if (it.hasNext()) {
    while (it.hasNext()) {
      final Hit hit = (Hit) it.next();
      relevance += ((float) Math.round(hit.getScore() * 1000)) / 10;
    }
    matchedLinks.add(new LinkMatch(url, relevance));
  }
}
 
Example 2
Source File: CrawlerTest.java    From JPPF with Apache License 2.0 6 votes vote down vote up
/**
 * Test searching with Lucene.
 * @param search the Lucene query text.
 * @param max the maximum number of results to show.
 * @throws Exception if an error is thrown while executing.
 */
public static void luceneSearch(final String search, final int max) throws Exception {
  print("Searching for: " + search);
  print("  max results: " + max);

  final IndexSearcher is = new IndexSearcher(index);
  final QueryParser parser = new QueryParser("contents", new StandardAnalyzer());

  final Query query = parser.parse(search);
  final Hits hits = is.search(query);

  print("    results: " + hits.length());

  for (int i = 0; i < Math.min(hits.length(), max); i++) {
    final float relevance = ((float) Math.round(hits.score(i) * 1000)) / 10;
    final String url = hits.doc(i).getField("url").stringValue();
    print("No " + (i + 1) + " with relevance " + relevance + "% : " + url);
  }

  is.close();
}
 
Example 3
Source File: AnchorIndexer.java    From tagme with Apache License 2.0 6 votes vote down vote up
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 4
Source File: NGramQueryParserTest.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
public void testQueryParseWithSpecialChars() throws Exception {
    String queryString = new String("spell* virt- manager+");
    log.info("testQueryParserWithSpecialChars(): query string is: " + queryString);
    NGramQueryParser parser = new NGramQueryParser("name", new NGramAnalyzer(min_ngram, max_ngram));
    Query q = parser.parse(queryString);
    log.info("Using NGramQueryParser query = " + q.toString());

    QueryParser origParser = new QueryParser("name", new StandardAnalyzer());
    q = origParser.parse(queryString);
    log.info("Using QueryParser query = " + q.toString());
}
 
Example 5
Source File: NGramQueryParserTest.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
public void testFreeFormQueryParse() throws Exception {
    String queryString = new String("name:spell -description:another");
    log.info("Original query: "  + queryString);

    NGramQueryParser parser = new NGramQueryParser("name",
            new NGramAnalyzer(min_ngram, max_ngram), true);
    Query q = parser.parse(queryString);
    log.info("NGramQueryParser parsed query:  " + q.toString());

    QueryParser origParser = new QueryParser("name", new StandardAnalyzer());
    q = origParser.parse(queryString);
    log.info("QueryParser parsed query = " + q.toString());
}
 
Example 6
Source File: NGramTestSetup.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
public Hits performSearch(Directory dir, Analyzer alyz, String query) throws Exception {
    QueryParser parser = new QueryParser("name", alyz);
    IndexSearcher searcher = new IndexSearcher(dir);
    Query q = parser.parse(query);
    Hits hits = searcher.search(q);
    return hits;
}
 
Example 7
Source File: DbSearchModule.java    From document-management-system with GNU General Public License v2.0 5 votes vote down vote up
@Override
public ResultSet findByQueryPaginated(String token, String query, int offset, int limit) throws ParseException,
		AccessDeniedException, RepositoryException, DatabaseException {
	log.debug("findByQueryPaginated({}, {}, {}, {})", new Object[]{token, query, offset, limit});
	Authentication auth = null, oldAuth = null;

	try {
		if (token == null) {
			auth = PrincipalUtils.getAuthentication();
		} else {
			oldAuth = PrincipalUtils.getAuthentication();
			auth = PrincipalUtils.getAuthenticationByToken(token);
		}

		QueryParser qp = new QueryParser(Config.LUCENE_VERSION, "text", SearchDAO.analyzer);
		Query q = qp.parse(query);
		ResultSet rs = findByStatementPaginated(auth, q, offset, limit);
		log.debug("findByQueryPaginated: {}", rs);
		return rs;
	} catch (org.apache.lucene.queryParser.ParseException e) {
		throw new ParseException(e.getMessage(), e);
	} finally {
		if (token != null) {
			PrincipalUtils.setAuthentication(oldAuth);
		}
	}
}
 
Example 8
Source File: WikiPageIdx.java    From document-management-system with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 9
Source File: NGramQueryParserTest.java    From spacewalk with GNU General Public License v2.0 5 votes vote down vote up
public void testQueryParseWithSpecialChars() throws Exception {
    String queryString = new String("spell* virt- manager+");
    log.info("testQueryParserWithSpecialChars(): query string is: " + queryString);
    NGramQueryParser parser = new NGramQueryParser("name", new NGramAnalyzer(min_ngram, max_ngram));
    Query q = parser.parse(queryString);
    log.info("Using NGramQueryParser query = " + q.toString());

    QueryParser origParser = new QueryParser("name", new StandardAnalyzer());
    q = origParser.parse(queryString);
    log.info("Using QueryParser query = " + q.toString());
}
 
Example 10
Source File: NGramQueryParserTest.java    From spacewalk with GNU General Public License v2.0 5 votes vote down vote up
public void testFreeFormQueryParse() throws Exception {
    String queryString = new String("name:spell -description:another");
    log.info("Original query: "  + queryString);

    NGramQueryParser parser = new NGramQueryParser("name",
            new NGramAnalyzer(min_ngram, max_ngram), true);
    Query q = parser.parse(queryString);
    log.info("NGramQueryParser parsed query:  " + q.toString());

    QueryParser origParser = new QueryParser("name", new StandardAnalyzer());
    q = origParser.parse(queryString);
    log.info("QueryParser parsed query = " + q.toString());
}
 
Example 11
Source File: NGramTestSetup.java    From spacewalk with GNU General Public License v2.0 5 votes vote down vote up
public Hits performSearch(Directory dir, Analyzer alyz, String query) throws Exception {
    QueryParser parser = new QueryParser("name", alyz);
    IndexSearcher searcher = new IndexSearcher(dir);
    Query q = parser.parse(query);
    Hits hits = searcher.search(q);
    return hits;
}
 
Example 12
Source File: SearchInTypeShortName.java    From gAnswer with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public  ArrayList<String> searchType(String s, double thres1, double thres2, int k) throws Exception
{		
	Hits hits = null;
	String queryString = null;
	Query query = null;
	
	IndexSearcher searcher = new IndexSearcher(Globals.localPath+"data/DBpedia2016/lucene/type_fragment_index");
	
	ArrayList<String> typeNames = new ArrayList<String>(); 
	
	//String[] array = s.split(" ");
	//queryString = array[array.length-1];
	queryString = s;

	Analyzer analyzer = new StandardAnalyzer();
	try {
		QueryParser qp = new QueryParser("SplittedTypeShortName", analyzer);
		query = qp.parse(queryString);
	} catch (ParseException e) {
		e.printStackTrace();
	}
	
	if (searcher != null) {
		hits = searcher.search(query);
		
		System.out.println("find " + hits.length() + " answars!");
		if (hits.length() > 0) {
			for (int i=0; i<hits.length(); i++) {
				if (i < k) {
					System.out.println("<<<<---" + hits.doc(i).get("TypeShortName") + " : " + hits.score(i));
				    if(hits.score(i) >= thres1){
				    	System.out.println("Score>=thres1("+thres1+") ---" + hits.doc(i).get("TypeShortName") + " : " + hits.score(i));
				    	typeNames.add(hits.doc(i).get("TypeShortName"));
				    	//if (satisfiedStrictly(hits.doc(i).get("SplittedTypeShortName"), queryString)) typeNames.add(hits.doc(i).get("TypeShortName"));
				    }
				    else {
				    	//break;
				    }
				}
				else {
				    if(hits.score(i) >= thres2){
				    	System.out.println("<<<<---" + hits.doc(i).get("TypeShortName") + " : " + hits.score(i));
				    	typeNames.add(hits.doc(i).get("TypeShortName"));
				    	//if (satisfiedStrictly(hits.doc(i).get("SplittedTypeShortName"), queryString)) typeNames.add(hits.doc(i).get("TypeShortName"));
				    }
				    else {
				    	break;
				    }						
				}
			}				
		}
	}		
	return typeNames;	
}
 
Example 13
Source File: SearchServiceImpl.java    From olat with Apache License 2.0 4 votes vote down vote up
/**
 * Do search a certain query. The results will be filtered for the identity and roles.
 * 
 * @param queryString
 *            Search query-string.
 * @param identity
 *            Filter results for this identity (user).
 * @param roles
 *            Filter results for this roles (role of user).
 * @return SearchResults object for this query
 */
@Override
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 {
    try {
        if (!existIndex()) {
            log.warn("Index does not exist, can't search for queryString: " + queryString);
            throw new ServiceNotAvailableException("Index does not exist");
        }
        synchronized (createIndexSearcherLock) {// o_clusterOK by:fj if service is only configured on one vm, which is recommended way
            if (searcher == null) {
                try {
                    createIndexSearcher(indexPath);
                    checkIsIndexUpToDate();
                } catch (final IOException ioEx) {
                    log.warn("Can not create searcher", ioEx);
                    throw new ServiceNotAvailableException("Index is not available");
                }
            }
            if (hasNewerIndexFile()) {
                reopenIndexSearcher();
                checkIsIndexUpToDate();
            }
        }
        log.info("queryString=" + queryString);

        final BooleanQuery query = new BooleanQuery();
        if (StringHelper.containsNonWhitespace(queryString)) {
            final QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_CURRENT, fields, analyzer);
            queryParser.setLowercaseExpandedTerms(false);// some add. fields are not tokenized and not lowered case
            final Query multiFieldQuery = queryParser.parse(queryString.toLowerCase());
            query.add(multiFieldQuery, Occur.MUST);
        }

        if (condQueries != null && !condQueries.isEmpty()) {
            for (final String condQueryString : condQueries) {
                final QueryParser condQueryParser = new QueryParser(Version.LUCENE_CURRENT, condQueryString, analyzer);
                condQueryParser.setLowercaseExpandedTerms(false);
                final Query condQuery = condQueryParser.parse(condQueryString);
                query.add(condQuery, Occur.MUST);
            }
        }

        if (log.isDebugEnabled()) {
            log.debug("query=" + query);
        }
        // TODO: 14.06.2010/cg : fellowig cide fragment can be removed later, do no longer call rewrite(query) because wildcard-search problem (OLAT-5359)
        // Query query = null;
        // try {
        // query = searcher.rewrite(query);
        // log.debug("after 'searcher.rewrite(query)' query=" + query);
        // } catch (Exception ex) {
        // throw new QueryException("Rewrite-Exception query because too many clauses. Query=" + query);
        // }
        final long startTime = System.currentTimeMillis();
        final int n = SearchServiceFactory.getService().getSearchModuleConfig().getMaxHits();
        final TopDocs docs = searcher.search(query, n);
        final long queryTime = System.currentTimeMillis() - startTime;
        if (log.isDebugEnabled()) {
            log.debug("hits.length()=" + docs.totalHits);
        }
        final SearchResultsImpl searchResult = new SearchResultsImpl(mainIndexer, searcher, docs, query, analyzer, identity, roles, firstResult, maxResults,
                doHighlighting);
        searchResult.setQueryTime(queryTime);
        searchResult.setNumberOfIndexDocuments(searcher.maxDoc());
        queryCount++;
        return searchResult;
    } catch (final ServiceNotAvailableException naex) {
        // pass exception
        throw new ServiceNotAvailableException(naex.getMessage());
    } catch (final ParseException pex) {
        throw new ParseException("can not parse query=" + queryString);
    } catch (final Exception ex) {
        log.warn("Exception in search", ex);
        throw new ServiceNotAvailableException(ex.getMessage());
    }
}
 
Example 14
Source File: Index.java    From olat with Apache License 2.0 4 votes vote down vote up
/**
 * @see org.olat.lms.search.SearchService#doSearch(String, List, Identity, Roles, int, int, boolean)
 */
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, QueryException {

    synchronized (createIndexSearcherLock) {// o_clusterOK by:fj if service is only configured on one vm, which is recommended way
        if (searcher == null) {
            log.warn("Index does not exist, can't search for queryString: " + queryString);
            throw new ServiceNotAvailableException("Index does not exist");
        }
    }

    try {
        log.info("queryString=" + queryString);

        final BooleanQuery query = new BooleanQuery();
        if (StringHelper.containsNonWhitespace(queryString)) {
            final QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_30, fields, analyzer);
            queryParser.setLowercaseExpandedTerms(false);// some add. fields are not tokenized and not lowered case
            final Query multiFieldQuery = queryParser.parse(queryString.toLowerCase());
            query.add(multiFieldQuery, Occur.MUST);
        }

        if (condQueries != null && !condQueries.isEmpty()) {
            for (final String condQueryString : condQueries) {
                final QueryParser condQueryParser = new QueryParser(Version.LUCENE_30, condQueryString, analyzer);
                condQueryParser.setLowercaseExpandedTerms(false);
                final Query condQuery = condQueryParser.parse(condQueryString);
                query.add(condQuery, Occur.MUST);
            }
        }

        if (log.isDebugEnabled()) {
            log.debug("query=" + query);
        }
        // TODO: 14.06.2010/cg : fellowig cide fragment can be removed later, do no longer call rewrite(query) because wildcard-search problem (OLAT-5359)
        // Query query = null;
        // try {
        // query = searcher.rewrite(query);
        // log.debug("after 'searcher.rewrite(query)' query=" + query);
        // } catch (Exception ex) {
        // throw new QueryException("Rewrite-Exception query because too many clauses. Query=" + query);
        // }
        final long startTime = System.currentTimeMillis();
        final int n = SearchServiceFactory.getService().getSearchModuleConfig().getMaxHits();
        final TopDocs docs = searcher.search(query, n);
        final long queryTime = System.currentTimeMillis() - startTime;
        if (log.isDebugEnabled()) {
            log.debug("hits.length()=" + docs.totalHits);
        }
        final SearchResultsImpl searchResult = new SearchResultsImpl(mainIndexer, searcher, docs, query, analyzer, identity, roles, firstResult, maxResults,
                doHighlighting);
        searchResult.setQueryTime(queryTime);
        searchResult.setNumberOfIndexDocuments(searcher.maxDoc());
        queryCount++;
        return searchResult;
    } catch (final ParseException pex) {
        throw new QueryException("can not parse query=" + queryString);
    } catch (final Exception ex) {
        log.warn("Exception in search", ex);
        throw new ServiceNotAvailableException(ex.getMessage());
    }
}
 
Example 15
Source File: 387581_IndexTaskTest_0_s.java    From coming with MIT License 3 votes vote down vote up
public void testSearch() throws Exception {
   Query query = QueryParser.parse("test", "contents", analyzer);
 
    Hits hits = searcher.search(query);
 
    assertEquals("Find document(s)", 2, hits.length());
}