Search in sources :

Example 16 with MultiFieldQueryParser

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());
}
Also used : MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) PostConstruct(javax.annotation.PostConstruct)

Example 17 with MultiFieldQueryParser

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;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery)

Example 18 with MultiFieldQueryParser

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);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) Term(org.apache.lucene.index.Term) MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Collection(java.util.Collection) ParseException(org.apache.lucene.queryparser.classic.ParseException)

Example 19 with MultiFieldQueryParser

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);
    }
}
Also used : MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) Term(org.apache.lucene.index.Term) ParseException(org.apache.lucene.queryparser.classic.ParseException)

Example 20 with MultiFieldQueryParser

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;
}
Also used : Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) ParseException(org.apache.lucene.queryparser.classic.ParseException) StorageException(edu.uci.ics.textdb.api.exception.StorageException)

Aggregations

MultiFieldQueryParser (org.apache.lucene.queryparser.classic.MultiFieldQueryParser)37 ParseException (org.apache.lucene.queryparser.classic.ParseException)24 Query (org.apache.lucene.search.Query)17 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)14 QueryParser (org.apache.lucene.queryparser.classic.QueryParser)14 Term (org.apache.lucene.index.Term)11 IndexSearcher (org.apache.lucene.search.IndexSearcher)11 TermQuery (org.apache.lucene.search.TermQuery)11 IOException (java.io.IOException)10 WildcardQuery (org.apache.lucene.search.WildcardQuery)10 ModuleException (it.vige.rubia.ModuleException)8 ResultPage (it.vige.rubia.search.ResultPage)8 Searching (it.vige.rubia.search.Searching)8 SortBy (it.vige.rubia.search.SortBy)8 SortOrder (it.vige.rubia.search.SortOrder)8 EntityManager (javax.persistence.EntityManager)8 Builder (org.apache.lucene.search.BooleanQuery.Builder)8 TopDocs (org.apache.lucene.search.TopDocs)8 FullTextQuery (org.hibernate.search.FullTextQuery)8 FullTextSession (org.hibernate.search.FullTextSession)8