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

The following examples show how to use org.apache.lucene.queryparser.classic.QueryParser#setDefaultOperator() . 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: LuceneHelperImpl.java    From tephra with MIT License 6 votes vote down vote up
@Override
public List<String> query(String key, String string, boolean and, int size) {
    if (validator.isEmpty(string))
        return null;

    List<String> list = new ArrayList<>();
    if (size <= 0)
        return list;

    try (IndexReader indexReader = DirectoryReader.open(get(key))) {
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        QueryParser queryParser = new QueryParser("data", newAnalyzer());
        if (and)
            queryParser.setDefaultOperator(QueryParser.Operator.AND);
        TopDocs topDocs = indexSearcher.search(queryParser.parse(replace(string)), size);
        for (ScoreDoc scoreDoc : topDocs.scoreDocs)
            list.add(indexSearcher.doc(scoreDoc.doc).get("id"));
    } catch (Throwable throwable) {
        logger.warn(throwable, "检索Lucene数据[{}:{}]时发生异常!", key, string);
    }

    return list;
}
 
Example 2
Source File: FullTextIndex.java    From jease with GNU General Public License v3.0 6 votes vote down vote up
public FullTextIndex() {
    try {
        objects = new ArrayList<>();

        Analyzer analyzer = new StandardAnalyzer();
        IndexWriterConfig config =  new IndexWriterConfig(new LimitTokenCountAnalyzer(analyzer, Integer.MAX_VALUE));

        indexDirectory = new RAMDirectory();
        indexWriter = new IndexWriter(indexDirectory, config);

        queryParser = new QueryParser("text", analyzer);
        queryParser.setDefaultOperator(QueryParser.AND_OPERATOR);

        fulltext = new TextField("text", "", Field.Store.NO);

        // Used as base-set for a NOT-Query
        Field inverse = new TextField("true", "yes", Field.Store.NO);

        document = new Document();
        document.add(fulltext);
        document.add(inverse);
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
 
Example 3
Source File: QueryStringQueryConstructor.java    From linden with Apache License 2.0 5 votes vote down vote up
@Override
protected Query construct(LindenQuery lindenQuery, LindenConfig config) throws IOException {
  LindenQueryStringQuery stringQuery = lindenQuery.getQueryString();
  QueryParser.Operator op = QueryParser.Operator.OR;
  if (stringQuery.isSetOperator() && stringQuery.getOperator() == Operator.AND) {
    op = QueryParser.Operator.AND;
  }
  QueryParser queryParser = new LindenQueryParser(config);
  String content = stringQuery.getQuery();
  try {
    queryParser.setDefaultOperator(op);
    Query query = queryParser.parse(content);
    // disable coord
    if (query instanceof BooleanQuery) {
      BooleanQuery bQuery = (BooleanQuery) query;
      BooleanQuery booleanQuery = new BooleanQuery(stringQuery.isDisableCoord());
      BooleanClause[] clauses = bQuery.getClauses();
      for (BooleanClause clause : clauses) {
        booleanQuery.add(clause);
      }
      booleanQuery.setBoost(query.getBoost());
      query = booleanQuery;
    }
    return query;
  } catch (ParseException e) {
    throw new IOException(Throwables.getStackTraceAsString(e));
  }
}
 
Example 4
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 5
Source File: TestExtendableQueryParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public QueryParser getParser(Analyzer a, Extensions extensions)
    throws Exception {
  if (a == null)
    a = new MockAnalyzer(random(), MockTokenizer.SIMPLE, true);
  QueryParser qp = extensions == null ? new ExtendableQueryParser(
      getDefaultField(), a) : new ExtendableQueryParser(
      getDefaultField(), a, extensions);
  qp.setDefaultOperator(QueryParserBase.OR_OPERATOR);
  qp.setSplitOnWhitespace(splitOnWhitespace);
  return qp;
}
 
Example 6
Source File: UserProfileDataQueryMapper.java    From adam with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Query convertToLuceneQuery(@Nonnull UserProfileDataQuery query) {
    final QueryParser parser = new MultiFieldQueryParser(LUCENE_VERSION, toFieldsArray(query), _luceneAnalyzerFactory.createAnalyzer());
    parser.setDefaultOperator(AND);
    final String searchTerm = query.getSearchTerm();
    try {
        return parser.parse(searchTerm != null ? searchTerm : "");
    } catch (final ParseException e) {
        throw new RuntimeException("Unable to parse query: " + searchTerm, e);
    }
}
 
Example 7
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 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);
        }
    }