Java Code Examples for org.apache.lucene.queryparser.classic.QueryParserBase#escape()
The following examples show how to use
org.apache.lucene.queryparser.classic.QueryParserBase#escape() .
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: QueryParserTestBase.java From lucene-solr with Apache License 2.0 | 5 votes |
public void assertEscapedQueryEquals(String query, Analyzer a, String result) throws Exception { String escapedQuery = QueryParserBase.escape(query); if (!escapedQuery.equals(result)) { fail("Query /" + query + "/ yielded /" + escapedQuery + "/, expecting /" + result + "/"); } }
Example 2
Source File: SearchContributionSupport.java From nexus-public with Eclipse Public License 1.0 | 5 votes |
public String escape(String value) { if (null == value) { return null; } String escaped = QueryParserBase.escape(value); boolean shouldLeaveDoubleQuotesEscaped = StringUtils.countMatches(value, "\"") % 2 != 0; String escapedCharactersRegex = shouldLeaveDoubleQuotesEscaped ? "\\\\([?*])" : "\\\\([?*\"])"; // unescape supported special characters return escaped.replaceAll(escapedCharactersRegex, "$1"); }
Example 3
Source File: ApiDocumentSearcher.java From gravitee-management-rest-api with Apache License 2.0 | 4 votes |
@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); } }
Example 4
Source File: Extensions.java From lucene-solr with Apache License 2.0 | 2 votes |
/** * Escapes an extension field. The default implementation is equivalent to * {@link QueryParser#escape(String)}. * * @param extfield * the extension field identifier * @return the extension field identifier with all special chars escaped with * a backslash character. */ public String escapeExtensionField(String extfield) { return QueryParserBase.escape(extfield); }