本文整理汇总了Java中org.apache.lucene.search.BooleanQuery类的典型用法代码示例。如果您正苦于以下问题:Java BooleanQuery类的具体用法?Java BooleanQuery怎么用?Java BooleanQuery使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
BooleanQuery类属于org.apache.lucene.search包,在下文中一共展示了BooleanQuery类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: buildSearchTermQuery
import org.apache.lucene.search.BooleanQuery; //导入依赖的package包/类
/**
* Returns a {@link BooleanQuery} for the given search text.
*
* @param indexType the type in which should be searched.
* @param search the search text.
* @param baseBoost highest possible boost of the query. The more the match is exact
* than a bigger boost will be used.
*/
private static BooleanQuery buildSearchTermQuery(final IIndexTypeConf indexType, final String search,
final float baseBoost) {
final BooleanQuery subQuery = new BooleanQuery();
final String lowerCase = StringUtils.lowerCase(search);
final String capitalized = StringUtils.capitalize(search);
addSearchTermQueries(indexType, search, subQuery, baseBoost);
if(!lowerCase.equals(search)) {
addSearchTermQueries(indexType, lowerCase, subQuery, 0.8f*baseBoost);
}
if(!capitalized.equals(search)) {
addSearchTermQueries(indexType, capitalized, subQuery, 0.8f*baseBoost);
}
return subQuery;
}
示例2: binaryNameQuery
import org.apache.lucene.search.BooleanQuery; //导入依赖的package包/类
static Query binaryNameQuery (final String resourceName) {
final BooleanQuery query = new BooleanQuery ();
int index = resourceName.lastIndexOf(BinaryName.PKG_SEPARATOR); // NOI18N
String pkgName, sName;
if (index < 0) {
pkgName = ""; // NOI18N
sName = resourceName;
}
else {
pkgName = resourceName.substring(0,index);
sName = resourceName.substring(index+1);
}
sName = sName + WILDCARD_QUERY_WILDCARD; //Type of type element (Enum, Class, Interface, Annotation)
query.add (new TermQuery (new Term (FIELD_PACKAGE_NAME, pkgName)),BooleanClause.Occur.MUST);
query.add (new WildcardQuery (new Term (FIELD_BINARY_NAME, sName)),BooleanClause.Occur.MUST);
return query;
}
示例3: createFQNQuery
import org.apache.lucene.search.BooleanQuery; //导入依赖的package包/类
private static BooleanQuery createFQNQuery(final String resourceName) {
String pkgName;
String sName;
int index = resourceName.lastIndexOf(BinaryName.PKG_SEPARATOR);
if (index < 0) {
pkgName = ""; //NOI18N
sName = resourceName;
} else {
pkgName = resourceName.substring(0, index);
sName = resourceName.substring(index+1);
}
final BooleanQuery snQuery = new BooleanQuery();
snQuery.add (new WildcardQuery (new Term (DocumentUtil.FIELD_BINARY_NAME, sName + DocumentUtil.WILDCARD_QUERY_WILDCARD)),Occur.SHOULD);
snQuery.add (new PrefixQuery (new Term (DocumentUtil.FIELD_BINARY_NAME, sName + '$')),Occur.SHOULD); //NOI18N
if (pkgName.length() == 0) {
return snQuery;
}
final BooleanQuery fqnQuery = new BooleanQuery();
fqnQuery.add(new TermQuery(new Term(DocumentUtil.FIELD_PACKAGE_NAME,pkgName)), Occur.MUST);
fqnQuery.add(snQuery, Occur.MUST);
return fqnQuery;
}
示例4: combineGrouped
import org.apache.lucene.search.BooleanQuery; //导入依赖的package包/类
public Query combineGrouped(List<Query> queries) {
if (queries == null || queries.isEmpty()) {
return null;
}
if (queries.size() == 1) {
return queries.get(0);
}
if (groupDismax) {
return new DisjunctionMaxQuery(queries, tieBreaker);
} else {
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
for (Query query : queries) {
booleanQuery.add(query, BooleanClause.Occur.SHOULD);
}
return booleanQuery.build();
}
}
示例5: prepareWildcardQueryForSingleToken
import org.apache.lucene.search.BooleanQuery; //导入依赖的package包/类
private static BooleanQuery prepareWildcardQueryForSingleToken(String token,
List<String> fieldNames, String locale, String defaultLocale,
boolean isDefaultLocaleHandling) {
BooleanQuery queryPart = new BooleanQuery();
for (String fieldName : fieldNames) {
if (isDefaultLocaleHandling) {
if (locale.equals(defaultLocale)) {
throw new IllegalArgumentException(
"For default locale handling, locale and default locale must be different");
}
BooleanQuery localeHandlingQuery = constructDefaultLocaleHandlingQuery(
fieldName, locale, defaultLocale, token);
queryPart.add(localeHandlingQuery, Occur.SHOULD);
} else {
WildcardQuery wildcardQuery = new WildcardQuery(new Term(
fieldName + locale, "*" + token.toLowerCase() + "*"));
queryPart.add(wildcardQuery, Occur.SHOULD);
}
}
return queryPart;
}
示例6: getDocIdForId
import org.apache.lucene.search.BooleanQuery; //导入依赖的package包/类
/**
* Returns lucene's document id for the given id in the given {@link IIndexTypeConf}
* @param typeConf the 188.166.43.201 to fins.
* @param id the id to find.
* @return the id or 0 if document was not found.
*/
public int getDocIdForId(final IIndexTypeConf typeConf, final String id) {
final SearchOptions params = new SearchOptions();
params.setMaxResults(1);
final BooleanQuery query = new BooleanQuery();
QueryUtil.addTypeConf(query, typeConf);
QueryUtil.addId(query, id);
final TopDocs topDocs = IndexSearch.getInstance().getTopDocs(query, params);
if(topDocs.totalHits == 0) {
throw new IllegalStateException("Can't find news with id " + id + " in news index.");
}
else if(topDocs.totalHits > 1) {
LOGGER.warn("Found more than one result for news with id " + id + " in news index. "
+ "This is an invalid state. Using the first found document.");
}
return topDocs.scoreDocs[0].doc;
}
示例7: addSearchText
import org.apache.lucene.search.BooleanQuery; //导入依赖的package包/类
/**
* Adds a filter for the given search text to the given {@link BooleanQuery}.
* If the search text is blank, than the query will not modified.
* For each part of the search text {@link #buildSearchTermQuery(IIndexTypeConf, String, float)}
* will be called.
*
* @param query the {@link BooleanQuery} to add the sub queries.
* @param searchText the search text. May be blank (null or contain only white spaces).
* @param indexType the type in which should be searched.
* @param baseBoost highest possible boost of the query. The more the match is exact
* than a bigger boost will be used.
*/
public static void addSearchText(final BooleanQuery query,
final String searchText, final IIndexTypeConf indexType, final float baseBoost) {
if(StringUtils.isBlank(searchText)) {
return;
}
query.setMinimumNumberShouldMatch(1); // at least one "should" should match
query.add(buildSearchTermQuery(indexType, searchText, baseBoost), Occur.SHOULD);
final BooleanQuery partsQuery = new BooleanQuery();
query.add(partsQuery, Occur.SHOULD);
final String[] searchTexts = searchText.toLowerCase().split("\\s");
for (final String search : searchTexts) {
partsQuery.add(buildSearchTermQuery(indexType, search, baseBoost), Occur.MUST); // each part has to match
}
}
示例8: addSearchTermQueries
import org.apache.lucene.search.BooleanQuery; //导入依赖的package包/类
/**
* Adds for every field in {@link IIndexTypeConf} a {@link TermQuery}, {@link PrefixQuery} and
* a {@link FuzzyQuery} with a suitable boost relative to the given base boost.
*
* @param indexType the type in which should be searched.
* @param search the search text.
* @param subQuery the {@link BooleanQuery} to add the sub queries.
* @param baseBoost highest possible boost of the query. The more the match is exact
* than a bigger boost will be used.
*/
private static void addSearchTermQueries(final IIndexTypeConf indexType, final String search,
final BooleanQuery subQuery, final float baseBoost) {
for(final IIndexFieldConf<?> field : indexType.getFields()) {
final Term term = new Term(field.getName(), search);
final TermQuery exactQuery = new TermQuery(term);
exactQuery.setBoost(baseBoost);
subQuery.add(exactQuery, Occur.SHOULD);
final PrefixQuery pfQuery = new PrefixQuery(term);
pfQuery.setBoost(0.7f*baseBoost);
subQuery.add(pfQuery, Occur.SHOULD);
final FuzzyQuery fuzzyQuery = new FuzzyQuery(term);
fuzzyQuery.setBoost(0.5f*baseBoost);
subQuery.add(fuzzyQuery, Occur.SHOULD);
}
}
示例9: getNewsOfNewsGroup
import org.apache.lucene.search.BooleanQuery; //导入依赖的package包/类
/**
* Returns all {@link News} of the given news group except the {@link News} with the given id.
* @param newsGroupId the news group id
* @param exceptId the news which should not be returned
* @return a {@link List} with all {@link News} of the requested news group except the {@link News} with the exceptId.
*/
private List<News> getNewsOfNewsGroup(final long newsGroupId, final long exceptId) {
final BooleanQuery query = new BooleanQuery();
QueryUtil.addTypeConf(query, NewsIndexType.getInstance());
final NumericRangeQuery<Long> groupQuery = NumericRangeQuery.newLongRange(
NewsIndexType.FIELD_NEWSGROUPID, newsGroupId, newsGroupId, true, true);
query.add(groupQuery, Occur.MUST);
// exclude news
query.add(new TermQuery(new Term(IIndexElement.FIELD_ID, String.valueOf(exceptId))), Occur.MUST_NOT);
final SearchOptions options = new SearchOptions();
options.setSort(new Sort(ESortField.PUBLISH_DATE.getSortField(ESortOrder.DESC)));
final DocumentsSearchResult result = IndexSearch.getInstance().search(query, options);
return NewsIndexType.docsToNews(result.getResults());
}
示例10: getAmountOfNewsInNewsGroups
import org.apache.lucene.search.BooleanQuery; //导入依赖的package包/类
/**
* Returns the amount of {@link News} which are assigned to news groups (news group id > 0)
* for the given {@link Query}.
*/
private int getAmountOfNewsInNewsGroups(final Query filterQuery) {
final BooleanQuery query = new BooleanQuery();
query.add(filterQuery, Occur.MUST);
// get only news that are in real groups (newsGroupId > 0)
final NumericRangeQuery<Long> newsGroupFilterQuery = NumericRangeQuery.newLongRange(
NewsIndexType.FIELD_NEWSGROUPID, 0l, null, false, true);
query.add(newsGroupFilterQuery, Occur.MUST);
final SearchOptions options = new SearchOptions();
options.setMaxResults(0); // we only want the totalHits, not the results.
final TopDocs topDocs = IndexSearch.getInstance().getTopDocs(query, options);
return topDocs.totalHits;
}
示例11: search
import org.apache.lucene.search.BooleanQuery; //导入依赖的package包/类
@Override
public Collection<IndexedItem> search(IndexSearcher searcher) throws IOException
{
BooleanQuery overall = new BooleanQuery();
BooleanQuery collections = new BooleanQuery();
for( Institution inst : institutions )
{
collections.add(
new TermQuery(new Term(FreeTextQuery.FIELD_INSTITUTION, Long.toString(inst.getUniqueId()))),
Occur.SHOULD);
}
overall.add(collections, Occur.MUST);
overall.add(NumericRangeQuery.newLongRange(FreeTextQuery.FIELD_ID_RANGEABLE, firstId, lastId, true, true),
Occur.MUST);
searcher.search(overall, compareDates);
return compareDates.getModifiedDocs();
}
示例12: testCreateMultiDocumentSearcher
import org.apache.lucene.search.BooleanQuery; //导入依赖的package包/类
public void testCreateMultiDocumentSearcher() throws Exception {
int numDocs = randomIntBetween(2, 8);
List<ParseContext.Document> docs = new ArrayList<>(numDocs);
for (int i = 0; i < numDocs; i++) {
docs.add(new ParseContext.Document());
}
Analyzer analyzer = new WhitespaceAnalyzer();
ParsedDocument parsedDocument = new ParsedDocument(null, null, "_id", "_type", null, docs, null, null, null);
IndexSearcher indexSearcher = PercolateQueryBuilder.createMultiDocumentSearcher(analyzer, parsedDocument);
assertThat(indexSearcher.getIndexReader().numDocs(), equalTo(numDocs));
// ensure that any query get modified so that the nested docs are never included as hits:
Query query = new MatchAllDocsQuery();
BooleanQuery result = (BooleanQuery) indexSearcher.createNormalizedWeight(query, true).getQuery();
assertThat(result.clauses().size(), equalTo(2));
assertThat(result.clauses().get(0).getQuery(), sameInstance(query));
assertThat(result.clauses().get(0).getOccur(), equalTo(BooleanClause.Occur.MUST));
assertThat(result.clauses().get(1).getOccur(), equalTo(BooleanClause.Occur.MUST_NOT));
}
示例13: constructDefaultLocaleHandlingQuery
import org.apache.lucene.search.BooleanQuery; //导入依赖的package包/类
private static BooleanQuery constructDefaultLocaleHandlingQuery(
String fieldName, String locale, String defaultLocale,
String searchPhrase) {
BooleanQuery bq1 = new BooleanQuery();
TermQuery tq1 = new TermQuery(
new Term(fieldName + ProductClassBridge.DEFINED_LOCALES_SUFFIX,
defaultLocale));
TermQuery tq2 = new TermQuery(new Term(
fieldName + ProductClassBridge.DEFINED_LOCALES_SUFFIX, locale));
bq1.add(tq1, Occur.MUST);
bq1.add(tq2, Occur.MUST_NOT);
BooleanQuery bq2 = new BooleanQuery();
WildcardQuery wq1 = new WildcardQuery(
new Term(fieldName + defaultLocale,
"*" + searchPhrase.toLowerCase() + "*"));
bq2.add(wq1, Occur.SHOULD);
BooleanQuery finalQuery = new BooleanQuery();
finalQuery.add(bq1, Occur.MUST);
finalQuery.add(bq2, Occur.MUST);
return finalQuery;
}
示例14: createMinShouldMatchQuery
import org.apache.lucene.search.BooleanQuery; //导入依赖的package包/类
/**
* Creates a minimum-should-match query from the query text.
* <p>
* @param field field name
* @param queryText text to be passed to the analyzer
* @param fraction of query terms {@code [0..1]} that should match
* @return {@code TermQuery} or {@code BooleanQuery}, based on the analysis
* of {@code queryText}
*/
public Query createMinShouldMatchQuery(String field, String queryText, float fraction) {
if (Float.isNaN(fraction) || fraction < 0 || fraction > 1) {
throw new IllegalArgumentException("fraction should be >= 0 and <= 1");
}
// TODO: wierd that BQ equals/rewrite/scorer doesn't handle this?
if (fraction == 1) {
return createBooleanQuery(field, queryText, BooleanClause.Occur.MUST);
}
Query query = createFieldQuery(analyzer, BooleanClause.Occur.SHOULD, field, queryText, false, 0);
if (query instanceof BooleanQuery) {
BooleanQuery bq = (BooleanQuery) query;
bq.setMinimumNumberShouldMatch((int) (fraction * bq.clauses().size()));
}
return query;
}
示例15: addBooleanClauses
import org.apache.lucene.search.BooleanQuery; //导入依赖的package包/类
private static void addBooleanClauses(QueryShardContext context, BooleanQuery.Builder booleanQueryBuilder,
List<QueryBuilder> clauses, Occur occurs) throws IOException {
for (QueryBuilder query : clauses) {
Query luceneQuery = null;
switch (occurs) {
case MUST:
case SHOULD:
luceneQuery = query.toQuery(context);
break;
case FILTER:
case MUST_NOT:
luceneQuery = query.toFilter(context);
break;
}
booleanQueryBuilder.add(new BooleanClause(luceneQuery, occurs));
}
}