use of org.apache.lucene.queryparser.classic.MultiFieldQueryParser in project uPortal by Jasig.
the class PortletsSearchStrategy method init.
@PostConstruct
public void init() {
final String[] fields = Arrays.stream(SearchField.values()).map(SearchField::getValue).toArray(String[]::new);
queryParser = new MultiFieldQueryParser(fields, new StandardAnalyzer());
}
use of org.apache.lucene.queryparser.classic.MultiFieldQueryParser in project openolat by klemens.
the class SearchServiceImpl method createQuery.
protected BooleanQuery createQuery(String queryString, List<String> condQueries) throws ParseException {
BooleanQuery query = new BooleanQuery();
if (StringHelper.containsNonWhitespace(queryString)) {
String[] fieldsArr = getFieldsToSearchIn();
QueryParser queryParser = new MultiFieldQueryParser(SearchService.OO_LUCENE_VERSION, fieldsArr, analyzer);
// some add. fields are not tokenized and not lowered case
queryParser.setLowercaseExpandedTerms(false);
Query multiFieldQuery = queryParser.parse(queryString.toLowerCase());
query.add(multiFieldQuery, Occur.MUST);
}
if (condQueries != null && !condQueries.isEmpty()) {
for (String condQueryString : condQueries) {
QueryParser condQueryParser = new QueryParser(SearchService.OO_LUCENE_VERSION, condQueryString, analyzer);
condQueryParser.setLowercaseExpandedTerms(false);
Query condQuery = condQueryParser.parse(condQueryString);
query.add(condQuery, Occur.MUST);
}
}
return query;
}
use of org.apache.lucene.queryparser.classic.MultiFieldQueryParser in project gravitee-management-rest-api by gravitee-io.
the class ApiDocumentSearcher method search.
@Override
public SearchResult search(io.gravitee.rest.api.service.search.query.Query query) throws TechnicalException {
MultiFieldQueryParser apiParser = new MultiFieldQueryParser(new String[] { "id", "name", "name_lowercase", "name_split", "description", "ownerName", "ownerMail", "labels", "labels_split", "tags", "tags_split", "categories", "categories_split", "paths", "paths_split", "hosts", "hosts_split", "metadata", "metadata_split" }, analyzer, API_FIELD_BOOST);
apiParser.setFuzzyMinSim(0.6f);
apiParser.setAllowLeadingWildcard(true);
QueryParser pageParser = new MultiFieldQueryParser(new String[] { "name", "name_lowercase", "name_split", "content" }, analyzer, PAGE_FIELD_BOOST);
pageParser.setFuzzyMinSim(0.6f);
pageParser.setAllowLeadingWildcard(true);
try {
String inputQuery = QueryParserBase.escape(query.getQuery());
Query parse = apiParser.parse(inputQuery);
Query parsePage = pageParser.parse(inputQuery);
Query apisFilter = getApisFilter(FIELD_ID, query.getFilters());
// Search in API fields
BooleanQuery.Builder apiQuery = new BooleanQuery.Builder();
BooleanQuery.Builder apiFieldsQuery = new BooleanQuery.Builder();
apiFieldsQuery.add(parse, BooleanClause.Occur.SHOULD);
apiFieldsQuery.add(new WildcardQuery(new Term("name", '*' + query.getQuery() + '*')), BooleanClause.Occur.SHOULD);
apiFieldsQuery.add(new WildcardQuery(new Term("name_lowercase", '*' + query.getQuery().toLowerCase() + '*')), BooleanClause.Occur.SHOULD);
apiFieldsQuery.add(new WildcardQuery(new Term("paths", '*' + query.getQuery() + '*')), BooleanClause.Occur.SHOULD);
apiFieldsQuery.add(new WildcardQuery(new Term("hosts", '*' + query.getQuery() + '*')), BooleanClause.Occur.SHOULD);
apiFieldsQuery.add(new WildcardQuery(new Term("labels", '*' + query.getQuery() + '*')), BooleanClause.Occur.SHOULD);
apiFieldsQuery.add(new WildcardQuery(new Term("categories", '*' + query.getQuery() + '*')), BooleanClause.Occur.SHOULD);
apiFieldsQuery.add(new WildcardQuery(new Term("tags", '*' + query.getQuery() + '*')), BooleanClause.Occur.SHOULD);
apiFieldsQuery.add(new WildcardQuery(new Term("metadata", '*' + query.getQuery() + '*')), BooleanClause.Occur.SHOULD);
apiQuery.add(apiFieldsQuery.build(), BooleanClause.Occur.MUST);
apiQuery.add(new TermQuery(new Term(FIELD_TYPE, FIELD_API_TYPE_VALUE)), BooleanClause.Occur.MUST);
if (apisFilter != null) {
apiQuery.add(apisFilter, BooleanClause.Occur.MUST);
}
// Search in page fields
BooleanQuery.Builder pageQuery = new BooleanQuery.Builder();
BooleanQuery.Builder pageFieldsQuery = new BooleanQuery.Builder();
pageFieldsQuery.add(parsePage, BooleanClause.Occur.SHOULD);
pageFieldsQuery.add(new WildcardQuery(new Term("name", '*' + query.getQuery() + '*')), BooleanClause.Occur.SHOULD);
pageFieldsQuery.add(new WildcardQuery(new Term("name_lowercase", '*' + query.getQuery().toLowerCase() + '*')), BooleanClause.Occur.SHOULD);
pageFieldsQuery.add(new WildcardQuery(new Term("content", '*' + query.getQuery() + '*')), BooleanClause.Occur.SHOULD);
pageQuery.add(pageFieldsQuery.build(), BooleanClause.Occur.MUST);
pageQuery.add(new TermQuery(new Term(FIELD_TYPE, FIELD_PAGE_TYPE_VALUE)), BooleanClause.Occur.MUST);
apisFilter = getApisFilter(FIELD_API_TYPE_VALUE, query.getFilters());
if (apisFilter != null) {
pageQuery.add(apisFilter, BooleanClause.Occur.MUST);
} else {
pageQuery.add(new DocValuesFieldExistsQuery(FIELD_API_TYPE_VALUE), BooleanClause.Occur.MUST);
}
BooleanQuery.Builder envCriteria = new BooleanQuery.Builder();
envCriteria.add(new TermQuery(new Term(FIELD_REFERENCE_TYPE, GraviteeContext.ReferenceContextType.ENVIRONMENT.name())), BooleanClause.Occur.MUST);
envCriteria.add(new TermQuery(new Term(FIELD_REFERENCE_ID, GraviteeContext.getCurrentEnvironmentOrDefault())), BooleanClause.Occur.MUST);
apiQuery.add(envCriteria.build(), BooleanClause.Occur.FILTER);
pageQuery.add(envCriteria.build(), BooleanClause.Occur.FILTER);
BooleanQuery.Builder mainQuery = new BooleanQuery.Builder();
mainQuery.add(new BoostQuery(apiQuery.build(), 2.0f), BooleanClause.Occur.SHOULD);
// mainQuery.add(new BoostQuery(pathQuery.build(), 4.0f), BooleanClause.Occur.SHOULD);
mainQuery.add(pageQuery.build(), BooleanClause.Occur.SHOULD);
// Manage filters
if (query.getFilters() != null) {
BooleanQuery.Builder filtersQuery = new BooleanQuery.Builder();
final boolean[] hasClause = { false };
query.getFilters().forEach(new BiConsumer<String, Object>() {
@Override
public void accept(String field, Object value) {
if (Collection.class.isAssignableFrom(value.getClass())) {
} else {
filtersQuery.add(new TermQuery(new Term(field, QueryParserBase.escape((String) value))), BooleanClause.Occur.MUST);
hasClause[0] = true;
}
}
});
if (hasClause[0]) {
filtersQuery.add(envCriteria.build(), BooleanClause.Occur.FILTER);
mainQuery.add(filtersQuery.build(), BooleanClause.Occur.MUST);
}
}
return search(mainQuery.build());
} catch (ParseException pe) {
logger.error("Invalid query to search for API documents", pe);
throw new TechnicalException("Invalid query to search for API documents", pe);
}
}
use of org.apache.lucene.queryparser.classic.MultiFieldQueryParser in project gravitee-management-rest-api by gravitee-io.
the class UserDocumentSearcher method search.
@Override
public SearchResult search(io.gravitee.rest.api.service.search.query.Query query) throws TechnicalException {
QueryParser parser = new MultiFieldQueryParser(new String[] { "displayname", "displayname_reverted", "email", "reference" }, analyzer);
parser.setFuzzyMinSim(0.6f);
parser.setAllowLeadingWildcard(true);
try {
parser.parse(QueryParserBase.escape(query.getQuery()));
BooleanQuery.Builder userQuery = new BooleanQuery.Builder();
BooleanQuery.Builder userFieldsQuery = new BooleanQuery.Builder();
final String normalizedQuery = StringUtils.stripAccents(query.getQuery().toLowerCase());
userFieldsQuery.add(new WildcardQuery(new Term("displayname", '*' + normalizedQuery + '*')), BooleanClause.Occur.SHOULD);
userFieldsQuery.add(new WildcardQuery(new Term("displayname_reverted", '*' + normalizedQuery + '*')), BooleanClause.Occur.SHOULD);
userFieldsQuery.add(new WildcardQuery(new Term("email", '*' + normalizedQuery + '*')), BooleanClause.Occur.SHOULD);
userFieldsQuery.add(new WildcardQuery(new Term("reference", '*' + normalizedQuery + '*')), BooleanClause.Occur.SHOULD);
userQuery.add(userFieldsQuery.build(), BooleanClause.Occur.MUST);
userQuery.add(new TermQuery(new Term(FIELD_TYPE, FIELD_TYPE_VALUE)), BooleanClause.Occur.MUST);
BooleanQuery.Builder orgCriteria = new BooleanQuery.Builder();
orgCriteria.add(new TermQuery(new Term(FIELD_REFERENCE_TYPE, GraviteeContext.ReferenceContextType.ORGANIZATION.name())), BooleanClause.Occur.MUST);
orgCriteria.add(new TermQuery(new Term(FIELD_REFERENCE_ID, GraviteeContext.getCurrentOrganization())), BooleanClause.Occur.MUST);
userQuery.add(orgCriteria.build(), BooleanClause.Occur.FILTER);
return search(userQuery.build(), query.getPage());
} catch (ParseException pe) {
logger.error("Invalid query to search for user documents", pe);
throw new TechnicalException("Invalid query to search for user documents", pe);
}
}
use of org.apache.lucene.queryparser.classic.MultiFieldQueryParser in project textdb by TextDB.
the class RegexMatcherSourceOperator method createLuceneQuery.
public static Query createLuceneQuery(RegexSourcePredicate predicate) throws StorageException {
Query luceneQuery;
String queryString;
// Try to apply translator. If it fails, use scan query.
try {
queryString = RegexToGramQueryTranslator.translate(predicate.getRegex()).getLuceneQueryString();
} catch (com.google.re2j.PatternSyntaxException e) {
queryString = DataConstants.SCAN_QUERY;
}
// Try to parse the query string. It if fails, raise an exception.
try {
luceneQuery = new MultiFieldQueryParser(predicate.getAttributeNames().stream().toArray(String[]::new), RelationManager.getRelationManager().getTableAnalyzer(predicate.getTableName())).parse(queryString);
} catch (ParseException e) {
throw new StorageException(e);
}
return luceneQuery;
}
Aggregations