Suppose a document has a multi-valued "author" field. Like this:
author: John Doeauthor: Bob Smith
With a position increment gap of 0, a phrase query of "doe bob" wouldbe a match. But often it is undesirable for that kind of match acrossdifferent field values. A position increment gap controls the virtualspace between the last token of one field instance and the first tokenof the next instance. With a gap of 100, this prevents phrase queries(even with a modest slop factor) from matching across instances.
protected Query getFieldQuery(String field, String queryText, boolean quoted) throws ParseException { String myField = field == null ? defaultField : field; if (myField != null) { FieldType ft = schema.getField(myField).getType(); if (ft instanceof TextField && "keywordtext".equals(myField)) { // 只对keywordtext字段做特殊处理 try { System.out.println("queryText:" + queryText); //此字段的分词器 Analyzer analyzer = ft.getQueryAnalyzer() == null ? ft.getAnalyzer() : ft.getQueryAnalyzer(); System.out.println("getPositionIncrementGap:" + analyzer.getPositionIncrementGap("keywordtext")); if (analyzer != null) { // 对同义词进行处理 // queryText = SynonymFilterUtil.getSynonymWord(queryText); // BooleanQuery bq = new BooleanQuery();//(true); // TokenStream ts = analyzer.tokenStream(field, new StringReader(queryText)); // int endOffset = 0; // while (ts.incrementToken()) { // CharTermAttribute ta = (CharTermAttribute) ts.getAttribute(CharTermAttribute.class); // OffsetAttribute oa = (OffsetAttribute) ts.getAttribute(OffsetAttribute.class); // //顺序增用 and 关系 // if (oa.startOffset() >= endOffset) { // //sb.append(t.term()).append(' '); // bq.add(new TermQuery(new Term(myField, ta.toString())), Occur.MUST); // endOffset = oa.endOffset(); // } else { // //可以用分词相交用 or 关系 // //这里也使用 and 关系 // bq.add(new TermQuery(new Term(myField, ta.toString())), Occur.MUST); // } // } // System.out.println("BooleanQuery:" + bq); // return bq; MultiPhraseQuery mq = new MultiPhraseQuery(); queryText = com.netease.autosolr.solr.Synonym.SynonymFilterUtil.getSynonymWord(queryText); TokenStream ts = analyzer.tokenStream(field, new StringReader(queryText)); int endOffset = 0; while (ts.incrementToken()) { CharTermAttribute ta = (CharTermAttribute) ts.getAttribute(CharTermAttribute.class); OffsetAttribute oa = (OffsetAttribute) ts.getAttribute(OffsetAttribute.class); //顺序增用 and 关系 if (oa.startOffset() >= endOffset) { //sb.append(t.term()).append(' '); mq.add(new Term(myField, ta.toString())); endOffset = oa.endOffset(); } else { //可以用分词相交用 or 关系 //这里也使用 and 关系 mq.add(new Term(myField, ta.toString())); } } mq.setSlop(50); System.out.println("MultiQuery:" + mq); return mq; // PhraseQuery pq = new PhraseQuery(); // pq.add(new Term(myField,queryText)); // return pq; } } catch (Exception e) { throw new ParseException(e.getMessage()); } }//TextField }// myField != null return super.getFieldQuery(field, queryText, quoted); }