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

The following examples show how to use org.apache.lucene.queryparser.classic.QueryParser#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: LuceneSearcher.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Page<Article> search(String keyword, int pageNum, int pageSize) {
    IndexReader indexReader = null;
    try {
        //Bug fix ,查询关键字使用一下 QueryParser.escape(keyword),例如:keyword=I/O,否则buildQuery时会出现异常
        keyword = QueryParser.escape(keyword);
        indexReader = DirectoryReader.open(directory);
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        Query query = buildQuery(keyword);

        ScoreDoc lastScoreDoc = getLastScoreDoc(pageNum, pageSize, query, indexSearcher);
        TopDocs topDocs = indexSearcher.searchAfter(lastScoreDoc, query, pageSize);

        SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<font class=\"" + HIGH_LIGHT_CLASS + "\">", "</font>");
        Highlighter highlighter = new Highlighter(formatter, new QueryScorer(query));
        highlighter.setTextFragmenter(new SimpleFragmenter(100));

        List<Article> articles = toArticleList(indexSearcher, topDocs, highlighter, keyword);
        int totalRow = getTotalRow(indexSearcher, query);
        return newPage(pageNum, pageSize, totalRow, articles);
    } catch (Exception e) {
        LOG.error(e.toString(), e);
    } finally {
        CommonsUtils.quietlyClose(indexReader);
    }
    return null;
}
 
Example 2
Source File: LuceneSearcher.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Page<Product> search(String keyword, int pageNum, int pageSize) {
    IndexReader indexReader = null;
    try {
        //Bug fix ,查询关键字使用一下 QueryParser.escape(keyword),例如:keyword=I/O,否则buildQuery时会出现异常
        keyword = QueryParser.escape(keyword);
        indexReader = DirectoryReader.open(directory);
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        Query query = buildQuery(keyword);

        ScoreDoc lastScoreDoc = getLastScoreDoc(pageNum, pageSize, query, indexSearcher);
        TopDocs topDocs = indexSearcher.searchAfter(lastScoreDoc, query, pageSize);

        SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<font class=\"" + HIGH_LIGHT_CLASS + "\">", "</font>");
        Highlighter highlighter = new Highlighter(formatter, new QueryScorer(query));
        highlighter.setTextFragmenter(new SimpleFragmenter(100));

        List<Product> products = toProductList(indexSearcher, topDocs, highlighter, keyword);
        int totalRow = getTotalRow(indexSearcher, query);
        return newPage(pageNum, pageSize, totalRow, products);
    } catch (Exception e) {
        LOG.error(e.toString(), e);
    } finally {
        CommonsUtils.quietlyClose(indexReader);
    }
    return null;
}
 
Example 3
Source File: VocabularyNeo4jImpl.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
@Override
public List<Concept> searchConcepts(Query query) {
  QueryParser parser = getQueryParser();
  // BooleanQuery finalQuery = new BooleanQuery();
  Builder finalQueryBuilder = new BooleanQuery.Builder();
  try {
    if (query.isIncludeSynonyms() || query.isIncludeAbbreviations() || query.isIncludeAcronyms()) {
      // BooleanQuery subQuery = new BooleanQuery();
      Builder subQueryBuilder = new BooleanQuery.Builder();
      subQueryBuilder.add(LuceneUtils.getBoostedQuery(parser, query.getInput(), 10.0f),
          Occur.SHOULD);
      String escapedQuery = QueryParser.escape(query.getInput());
      if (query.isIncludeSynonyms()) {
        subQueryBuilder.add(parser.parse(Concept.SYNONYM + ":" + escapedQuery), Occur.SHOULD);
      }
      if (query.isIncludeAbbreviations()) {
        subQueryBuilder.add(parser.parse(Concept.ABREVIATION + ":" + escapedQuery), Occur.SHOULD);
      }
      if (query.isIncludeAcronyms()) {
        subQueryBuilder.add(parser.parse(Concept.ACRONYM + ":" + escapedQuery), Occur.SHOULD);
      }
      finalQueryBuilder.add(subQueryBuilder.build(), Occur.MUST);
    } else {
      finalQueryBuilder.add(parser.parse(query.getInput()), Occur.MUST);
    }
  } catch (ParseException e) {
    logger.log(Level.WARNING, "Failed to parse query", e);
  }
  addCommonConstraints(finalQueryBuilder, query);
  IndexHits<Node> hits = null;
  BooleanQuery finalQuery = finalQueryBuilder.build();

  try (Transaction tx = graph.beginTx()) {
    hits = graph.index().getNodeAutoIndexer().getAutoIndex().query(finalQuery);
    tx.success();
    return limitHits(hits, query);
  }
}
 
Example 4
Source File: UnitResolverImpl.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
String convertNumberToOntologyTermStyle(String term) {
  term = UnitHelper.superscriptToNumber(term.replaceAll("\\^", StringUtils.EMPTY));
  Pattern pattern = Pattern.compile("\\w+(\\d+)");
  Matcher matcher = pattern.matcher(term);

  if (matcher.find()) {
    String group = matcher.group(1);
    String modifiedPart = group.trim();
    modifiedPart = "^[" + modifiedPart + "]";
    term = term.replaceAll(group, modifiedPart);
  }
  return QueryParser.escape(term);
}
 
Example 5
Source File: LuceneQueryStringConstructor.java    From bioasq with Apache License 2.0 4 votes vote down vote up
@Override
public String formatAtomicQueryText(String text, String originalText) {
  return QueryParser.escape(text);
}
 
Example 6
Source File: LuceneInMemoryPassageScorer.java    From bioasq with Apache License 2.0 4 votes vote down vote up
private static String normalizeQuoteName(String name) {
  return "\"" + QueryParser.escape(name) + "\"";
}
 
Example 7
Source File: LuceneConceptScorer.java    From bioasq with Apache License 2.0 4 votes vote down vote up
private static String normalizeQuoteName(String name) {
  return "\"" + QueryParser.escape(name) + "\"";
}
 
Example 8
Source File: LuceneDocumentScorer.java    From bioasq with Apache License 2.0 4 votes vote down vote up
private static String normalizeQuoteName(String name) {
  return "\"" + QueryParser.escape(name) + "\"";
}
 
Example 9
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);
        }
    }