Java Code Examples for org.apache.lucene.search.PhraseQuery#add()

The following examples show how to use org.apache.lucene.search.PhraseQuery#add() . 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: LuceneSearcher.java    From uncc2014watsonsim with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a Lucene query using the bigrams in the given text
 * @param text
 */
public BooleanQuery queryFromSkipBigrams(String text) {
	BooleanQuery q = new BooleanQuery();
	String prev_word = null;
	for (String word : text.split("\\W+")) {
		if (prev_word != null) {
			PhraseQuery pq = new PhraseQuery();
			pq.setSlop(1);
			pq.add(new Term("text", prev_word));
			pq.add(new Term("text", word));
			q.add(pq, BooleanClause.Occur.SHOULD);
		}
		q.add(new TermQuery(new Term("text", word)), BooleanClause.Occur.SHOULD);
		prev_word = word;
	}
	return q;
}
 
Example 2
Source File: DeviceFilterFactory.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * When a @FullTextFilterDef annotation associates this factory class with a given name, and a "FullTextQuery.enableFullTextFilter()" is 
 * called with that name as its input parameter, then this method is used to return a Filter with the actual filtering logic.  It is 
 * the @Factory annotation that designates this method as having that responsibility for this factory class. 
 */
@Factory
public Filter getFilter() {
	StringTokenizer tokenzier = new StringTokenizer(deviceName.toLowerCase());
	PhraseQuery query = new PhraseQuery();
	while(tokenzier.hasMoreTokens()) {
		// By default, field values were converted to lower-case when indexed by Lucene.  So be sure to 
		// convert search terms to lower-case in order to make them match.
		Term term = new Term("supportedDevices.name", tokenzier.nextToken().toLowerCase());
		query.add(term);
	}
	Filter filter = new QueryWrapperFilter(query);
	return new CachingWrapperFilter(filter);
}
 
Example 3
Source File: DeviceFilterFactory.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * When a @FullTextFilterDef annotation associates this factory class with a given name, and a "FullTextQuery.enableFullTextFilter()" is 
 * called with that name as its input parameter, then this method is used to return a Filter with the actual filtering logic.  It is 
 * the @Factory annotation that designates this method as having that responsibility for this factory class. 
 */
@Factory
public Filter getFilter() {
	StringTokenizer tokenzier = new StringTokenizer(deviceName.toLowerCase());
	PhraseQuery query = new PhraseQuery();
	while(tokenzier.hasMoreTokens()) {
		// By default, field values were converted to lower-case when indexed by Lucene.  So be sure to 
		// convert search terms to lower-case in order to make them match.
		Term term = new Term("supportedDevices.name", tokenzier.nextToken().toLowerCase());
		query.add(term);
	}
	Filter filter = new QueryWrapperFilter(query);
	return new CachingWrapperFilter(filter);
}
 
Example 4
Source File: DeviceFilterFactory.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * When a @FullTextFilterDef annotation associates this factory class with a given name, and a "FullTextQuery.enableFullTextFilter()" is 
 * called with that name as its input parameter, then this method is used to return a Filter with the actual filtering logic.  It is 
 * the @Factory annotation that designates this method as having that responsibility for this factory class. 
 */
@Factory
public Filter getFilter() {
	StringTokenizer tokenzier = new StringTokenizer(deviceName.toLowerCase());
	PhraseQuery query = new PhraseQuery();
	while(tokenzier.hasMoreTokens()) {
		// By default, field values were converted to lower-case when indexed by Lucene.  So be sure to 
		// convert search terms to lower-case in order to make them match.
		Term term = new Term("supportedDevices.name", tokenzier.nextToken().toLowerCase());
		query.add(term);
	}
	Filter filter = new QueryWrapperFilter(query);
	return new CachingWrapperFilter(filter);
}
 
Example 5
Source File: LuceneHelper.java    From dexter with Apache License 2.0 5 votes vote down vote up
/**
 * @param query
 *            - a query
 * @param field
 *            - the field where to search the query
 * @return number of documents containing the text in query in the given
 *         fields
 */
public int getFreq(String query, String field) {
	Query q = null;
	searcher = getSearcher();
	TopScoreDocCollector collector = TopScoreDocCollector.create(1, true);

	// try {

	Text t = new Text(query).disableStopwords();
	PhraseQuery pq = new PhraseQuery();
	int i = 0;
	for (String term : t.getTerms()) {
		pq.add(new Term(field, term), i++);
	}
	q = pq;
	logger.debug(q.toString());
	// } catch (ParseException e) {
	// logger.error("querying the index: {} ", e.toString());
	// return -1;
	// }
	try {
		searcher.search(q, collector);
	} catch (IOException e) {
		logger.error("querying the index: {} ", e.toString());
		return -1;
	}
	return collector.getTotalHits();
}
 
Example 6
Source File: FieldInjectorRegistry.java    From SolRDF with Apache License 2.0 4 votes vote down vote up
@Override
public void addFilterConstraint(final List<Query> filters, final String value, SolrQueryRequest request) {
	final PhraseQuery query = new PhraseQuery();
	query.add(new Term(Field.TEXT_OBJECT, value));
	filters.add(query);
}