Java Code Examples for org.apache.lucene.queryparser.classic.QueryParser#setFuzzyMinSim()

The following examples show how to use org.apache.lucene.queryparser.classic.QueryParser#setFuzzyMinSim() . 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: PageDocumentSearcher.java    From gravitee-management-rest-api with Apache License 2.0 6 votes vote down vote up
@Override
public SearchResult search(io.gravitee.rest.api.service.search.query.Query query) throws TechnicalException {
    QueryParser parser = new MultiFieldQueryParser(new String[]{
            "name",
            "content"
    }, analyzer);
    parser.setFuzzyMinSim(0.6f);

    try {
        final Query parse = parser.parse(QueryParserBase.escape(query.getQuery()));

        BooleanQuery.Builder bq = new BooleanQuery.Builder();
        bq.add(parse, BooleanClause.Occur.MUST);
        bq.add(new TermQuery(new Term(FIELD_TYPE, FIELD_TYPE_VALUE)), BooleanClause.Occur.MUST);

        return search(bq.build());
    } catch (ParseException pe) {
        logger.error("Invalid query to search for page documents", pe);
        throw new TechnicalException("Invalid query to search for page documents", pe);
    }
}
 
Example 2
Source File: SearchImpl.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private Query parseByClassicParser(String expression, String defField, Analyzer analyzer,
                                   QueryParserConfig config) {
  QueryParser parser = new QueryParser(defField, analyzer);

  switch (config.getDefaultOperator()) {
    case OR:
      parser.setDefaultOperator(QueryParser.Operator.OR);
      break;
    case AND:
      parser.setDefaultOperator(QueryParser.Operator.AND);
      break;
  }

  parser.setSplitOnWhitespace(config.isSplitOnWhitespace());
  parser.setAutoGenerateMultiTermSynonymsPhraseQuery(config.isAutoGenerateMultiTermSynonymsPhraseQuery());
  parser.setAutoGeneratePhraseQueries(config.isAutoGeneratePhraseQueries());
  parser.setEnablePositionIncrements(config.isEnablePositionIncrements());
  parser.setAllowLeadingWildcard(config.isAllowLeadingWildcard());
  parser.setDateResolution(config.getDateResolution());
  parser.setFuzzyMinSim(config.getFuzzyMinSim());
  parser.setFuzzyPrefixLength(config.getFuzzyPrefixLength());
  parser.setLocale(config.getLocale());
  parser.setTimeZone(config.getTimeZone());
  parser.setPhraseSlop(config.getPhraseSlop());

  try {
    return parser.parse(expression);
  } catch (ParseException e) {
    throw new LukeException(String.format(Locale.ENGLISH, "Failed to parse query expression: %s", expression), e);
  }

}
 
Example 3
Source File: UserDocumentSearcher.java    From gravitee-management-rest-api with Apache License 2.0 5 votes vote down vote up
@Override
public SearchResult search(io.gravitee.rest.api.service.search.query.Query query) throws TechnicalException {
    QueryParser parser = new MultiFieldQueryParser(new String[]{
            "firstname",
            "lastname",
            "displayname",
            "displayname_split",
            "email"
    }, analyzer);
    parser.setFuzzyMinSim(0.6f);
    parser.setAllowLeadingWildcard(true);

    try {
        Query parse = parser.parse(QueryParserBase.escape(query.getQuery()));

        BooleanQuery.Builder userQuery = new BooleanQuery.Builder();
        BooleanQuery.Builder userFieldsQuery = new BooleanQuery.Builder();

        userFieldsQuery.add(parse, BooleanClause.Occur.SHOULD);
        userFieldsQuery.add(new WildcardQuery(new Term("firstname", '*' + query.getQuery() + '*')), BooleanClause.Occur.SHOULD);
        userFieldsQuery.add(new WildcardQuery(new Term("lastname", '*' + query.getQuery() + '*')), BooleanClause.Occur.SHOULD);
        userFieldsQuery.add(new WildcardQuery(new Term("displayname", '*' + query.getQuery() + '*')), BooleanClause.Occur.SHOULD);
        userFieldsQuery.add(new WildcardQuery(new Term("email", '*' + query.getQuery() + '*')), BooleanClause.Occur.SHOULD);

        userQuery.add(userFieldsQuery.build(), BooleanClause.Occur.MUST);
        userQuery.add(new TermQuery(new Term(FIELD_TYPE, FIELD_TYPE_VALUE)), BooleanClause.Occur.MUST);

        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);
    }
}
 
Example 4
Source File: SearchQuery.java    From HongsCORE with MIT License 4 votes vote down vote up
@Override
    public Query wdr(String k, Object v) {
        if (null  ==  v ) {
            throw new NullPointerException("Query for "+k+" must be string, but null");
        }
        if ("".equals(v)) {
            throw new NullPointerException("Query for "+k+" can not be empty string" );
        }

        QueryParser qp = new QueryParser("$" + k, ana != null ? ana : new StandardAnalyzer());

        String s = v.toString( );

        // 是否转义
        if (des == null || !des) {
            s = QueryParser.escape(s);
        }

        // 词间关系
        if (dor == null || !dor) {
            qp.setDefaultOperator(QueryParser.AND_OPERATOR);
        } else {
            qp.setDefaultOperator(QueryParser. OR_OPERATOR);
        }

        // 其他设置
        if (phr != null) qp.setPhraseSlop       (phr);
        if (fms != null) qp.setFuzzyMinSim      (fms);
        if (fpl != null) qp.setFuzzyPrefixLength(fpl);
//      if (art != null) qp.setAnalyzeRangeTerms(art);
        if (sow != null) qp.setSplitOnWhitespace(sow);
        if (alw != null) qp.setAllowLeadingWildcard     (alw);
//      if (let != null) qp.setLowercaseExpandedTerms   (let);
        if (epi != null) qp.setEnablePositionIncrements (epi);
        if (agp != null) qp.setAutoGeneratePhraseQueries(agp);

        try {
            Query  q2 = qp.parse(s);
            return q2 ;
        } catch ( ParseException e) {
            throw new HongsExemption(e);
        }
    }
 
Example 5
Source File: ApiDocumentSearcher.java    From gravitee-management-rest-api with Apache License 2.0 4 votes vote down vote up
@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",
            "tags",
            "categories",
            "paths",
            "paths_split",
            "hosts",
            "hosts_split",
    }, analyzer, API_FIELD_BOOST);
    apiParser.setFuzzyMinSim(0.6f);
    apiParser.setAllowLeadingWildcard(true);

    QueryParser pageParser = new MultiFieldQueryParser(new String[]{
            "name",
            "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);

        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();
        pageQuery.add(parsePage, 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 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]) {
                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);
    }
}