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

The following examples show how to use org.apache.lucene.queryparser.classic.QueryParser#setAllowLeadingWildcard() . 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: QueryAttributes.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
public boolean setCriteria( String _critera, String type, boolean _allowLeadingWildcard ) throws ParseException {
	if ( _critera == null || _critera.length() == 0 )
		return false;

	// If this is 'simple' then we OR up all parameters
	if ( ( type == null || type.equalsIgnoreCase("simple") ) && _critera.indexOf(",") != -1 ){
		String[] tokens	= _critera.split(",");
		_critera	= tokens[0];
		for ( int x=1; x < tokens.length; x++ )
			_critera += " OR " + tokens[x];
	}
	
	if (_critera.equals("*")) // Special Case, this allows you to retrieve all results from the collection
		query = new MatchAllDocsQuery();
	else {
		QueryParser qp = new QueryParser( DocumentWrap.CONTENTS, AnalyzerFactory.get( collectionsList.get(0).getLanguage() ) );
		qp.setAllowLeadingWildcard( _allowLeadingWildcard );
		query = qp.parse(_critera);
	}
	
	return true;
}
 
Example 2
Source File: LuceneCondition.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public Query query(Schema schema) {

    if (query == null) {
        throw new IllegalArgumentException("Query statement required");
    }

    try {
        Analyzer analyzer = schema.getAnalyzer();
        QueryParser queryParser = new QueryParser(defaultField, analyzer);
        queryParser.setAllowLeadingWildcard(true);
        queryParser.setLowercaseExpandedTerms(false);
        Query luceneQuery = queryParser.parse(query);
        luceneQuery.setBoost(boost);
        return luceneQuery;
    } catch (ParseException e) {
        throw new RuntimeException("Error while parsing lucene syntax query", e);
    }
}
 
Example 3
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 4
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 5
Source File: QueryFactory.java    From incubator-atlas with Apache License 2.0 5 votes vote down vote up
private QueryExpression create(Request request, ResourceDefinition resourceDefinition) throws InvalidQueryException {
    String queryString;
    if (request.getCardinality() == Request.Cardinality.INSTANCE) {
        String idPropertyName = resourceDefinition.getIdPropertyName();
        queryString = String.format("%s:%s", idPropertyName, request.<String>getProperty(idPropertyName));
    } else {
        queryString = request.getQueryString();
    }

    QueryExpression queryExpression;
    if (queryString != null && !queryString.isEmpty()) {
        QueryParser queryParser = new QueryParser(Version.LUCENE_48, "name", new KeywordAnalyzer());
        queryParser.setLowercaseExpandedTerms(false);
        queryParser.setAllowLeadingWildcard(true);
        Query query;
        try {
            query = queryParser.parse((String) escape(queryString));
        } catch (ParseException e) {
            throw new InvalidQueryException(e.getMessage());
        }
        LOG.info("LuceneQuery: {}", query);
        queryExpression = create(query, resourceDefinition);
    } else {
        queryExpression = new AlwaysQueryExpression();
    }
    // add query properties to request so that they are returned
    request.addAdditionalSelectProperties(queryExpression.getProperties());
    return queryExpression;
}
 
Example 6
Source File: DocumentSearcher.java    From meghanada-server with GNU General Public License v3.0 4 votes vote down vote up
private Query getQuery(final String field, final String query) throws ParseException {
  final QueryParser queryParser = new QueryParser(field, analyzer);
  queryParser.setAllowLeadingWildcard(true);
  queryParser.setDefaultOperator(QueryParser.Operator.OR);
  return queryParser.parse(query);
}
 
Example 7
Source File: FileBasedQueryMaker.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
protected Query[] prepareQueries() throws Exception {

  Analyzer anlzr = NewAnalyzerTask.createAnalyzer(config.get("analyzer",
          "org.apache.lucene.analysis.standard.StandardAnalyzer"));
  String defaultField = config.get("file.query.maker.default.field", DocMaker.BODY_FIELD);
  QueryParser qp = new QueryParser(defaultField, anlzr);
  qp.setAllowLeadingWildcard(true);

  List<Query> qq = new ArrayList<>();
  String fileName = config.get("file.query.maker.file", null);
  if (fileName != null)
  {
    Path path = Paths.get(fileName);
    Reader reader = null;
    // note: we use a decoding reader, so if your queries are screwed up you know
    if (Files.exists(path)) {
      reader = Files.newBufferedReader(path, StandardCharsets.UTF_8);
    } else {
      //see if we can find it as a resource
      InputStream asStream = FileBasedQueryMaker.class.getClassLoader().getResourceAsStream(fileName);
      if (asStream != null) {
        reader = IOUtils.getDecodingReader(asStream, StandardCharsets.UTF_8);
      }
    }
    if (reader != null) {
      try {
        BufferedReader buffered = new BufferedReader(reader);
        String line = null;
        int lineNum = 0;
        while ((line = buffered.readLine()) != null) {
          line = line.trim();
          if (line.length() != 0 && !line.startsWith("#")) {
            try {
              qq.add(qp.parse(line));
            } catch (ParseException e) {
              System.err.println("Exception: " + e.getMessage() + " occurred while parsing line: " + lineNum + " Text: " + line);
            }
          }
          lineNum++;
        }
      } finally {
        reader.close();
      }
    } else {
      System.err.println("No Reader available for: " + fileName);
    }
    
  }
  return qq.toArray(new Query[qq.size()]) ;
}
 
Example 8
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 9
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);
    }
}
 
Example 10
Source File: BasicStorageTest.java    From lumongo with Apache License 2.0 4 votes vote down vote up
@Test
public void test2Query() throws IOException, ParseException {
	IndexReader indexReader = DirectoryReader.open(directory);

	StandardAnalyzer analyzer = new StandardAnalyzer();
	QueryParser qp = new QueryParser("title", analyzer) {

		@Override
		protected Query getRangeQuery(final String fieldName, final String start, final String end, final boolean startInclusive,
				final boolean endInclusive) throws ParseException {

			if ("testIntField".equals(fieldName)) {
				int startInt = Integer.parseInt(start);
				int endInt = Integer.parseInt(end);
				if (!startInclusive) {
					startInt += 1;
				}
				if (!endInclusive) {
					endInt -= 1;
				}
				return IntPoint.newRangeQuery(fieldName, startInt, endInt);

			}

			// return default
			return super.getRangeQuery(fieldName, start, end, startInclusive, endInclusive);

		}

		@Override
		protected Query newTermQuery(org.apache.lucene.index.Term term) {
			String field = term.field();
			String text = term.text();
			if ("testIntField".equals(field)) {
				int value = Integer.parseInt(text);
				return IntPoint.newExactQuery(field, value);
			}
			return super.newTermQuery(term);
		}
	};
	qp.setAllowLeadingWildcard(true);

	int hits;

	hits = runQuery(indexReader, qp, "java", 10);
	assertEquals("Expected 2 hits", 2, hits);
	hits = runQuery(indexReader, qp, "perl", 10);
	assertEquals("Expected 0 hits", 0, hits);
	hits = runQuery(indexReader, qp, "treatment", 10);
	assertEquals("Expected 0 hits", 0, hits);
	hits = runQuery(indexReader, qp, "long", 10);
	assertEquals("Expected 2 hits", 2, hits);
	hits = runQuery(indexReader, qp, "MongoDB", 10);
	assertEquals("Expected 1 hit", 1, hits);
	hits = runQuery(indexReader, qp, "java AND awesome", 10);
	assertEquals("Expected 1 hit", 1, hits);
	hits = runQuery(indexReader, qp, "testIntField:1", 10);
	assertEquals("Expected 0 hits", 0, hits);
	hits = runQuery(indexReader, qp, "testIntField:3", 10);
	assertEquals("Expected 5 hits", 5, hits);
	hits = runQuery(indexReader, qp, "testIntField:[1 TO 10]", 10);
	assertEquals("Expected 5 hits", 5, hits);


	indexReader.close();
}