无论才能、知识多么卓著,如果缺乏热情,则无异纸上画饼充饥,无补于事。

分词器中create和incrementToken方法的调用?

Elasticsearch | 作者 private_void | 发布于2017年09月21日 | 阅读数:3483

一直搞不清分词器的调用原理。想问下,一次搜索分别会调用几次create和incrementToken方法?求解答。
 
打印了下堆栈:org.apache.lucene.util.QueryBuilder类里有这么一段
    // 这个analyzer.tokenStream会调用分词器的create
    try (TokenStream source = analyzer.tokenStream(field, queryText);
         CachingTokenFilter stream = new CachingTokenFilter(source)) {
      
      TermToBytesRefAttribute termAtt = stream.getAttribute(TermToBytesRefAttribute.class);
      PositionIncrementAttribute posIncAtt = stream.addAttribute(PositionIncrementAttribute.class);
      
      if (termAtt == null) {
        return null; 
      }
      
      // phase 1: read through the stream and assess the situation:
      // counting the number of tokens/positions and marking if we have any synonyms.
      
      int numTokens = 0;
      int positionCount = 0;
      boolean hasSynonyms = false;

      stream.reset();
      // 这个stream.incrementToken()会调用分词器的incrementToken()
      while (stream.incrementToken()) {
        numTokens++;
        int positionIncrement = posIncAtt.getPositionIncrement();
        if (positionIncrement != 0) {
          positionCount += positionIncrement;
        } else {
          hasSynonyms = true;
        }
      }
已邀请:

要回复问题请先登录注册