|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.lucene.queryparser.flexible.aqp.builders; |
| 18 | + |
| 19 | +import java.util.LinkedList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.TreeMap; |
| 22 | + |
| 23 | +import org.apache.lucene.index.Term; |
| 24 | +import org.apache.lucene.queryparser.flexible.core.QueryNodeException; |
| 25 | +import org.apache.lucene.queryparser.flexible.core.builders.QueryTreeBuilder; |
| 26 | +import org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode; |
| 27 | +import org.apache.lucene.queryparser.flexible.core.nodes.QueryNode; |
| 28 | +import org.apache.lucene.queryparser.flexible.standard.builders.StandardQueryBuilder; |
| 29 | +import org.apache.lucene.queryparser.flexible.standard.nodes.MultiPhraseQueryNode; |
| 30 | +import org.apache.lucene.queryparser.flexible.standard.builders.MultiPhraseQueryNodeBuilder; |
| 31 | +import org.apache.lucene.search.MultiPhraseQuery; |
| 32 | +import org.apache.lucene.search.TermQuery; |
| 33 | + |
| 34 | +/** |
| 35 | + * Builds a {@link MultiPhraseQuery} object from a {@link MultiPhraseQueryNode} |
| 36 | + * object. |
| 37 | + * |
| 38 | + * Modified {@link MultiPhraseQueryNodeBuilder} - when we encounter a token |
| 39 | + * with position increment=0 we assume that it should be placed into the same |
| 40 | + * position as the previous token (i.e. it is a synonym). Make sure that |
| 41 | + * the tokens submitted are in proper order! |
| 42 | + */ |
| 43 | +public class AqpMultiPhraseQueryNodeBuilder implements StandardQueryBuilder { |
| 44 | + |
| 45 | + public AqpMultiPhraseQueryNodeBuilder() { |
| 46 | + // empty constructor |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public MultiPhraseQuery build(QueryNode queryNode) throws QueryNodeException { |
| 51 | + MultiPhraseQueryNode phraseNode = (MultiPhraseQueryNode) queryNode; |
| 52 | + |
| 53 | + MultiPhraseQuery.Builder phraseQueryBuilder = new MultiPhraseQuery.Builder(); |
| 54 | + |
| 55 | + List<QueryNode> children = phraseNode.getChildren(); |
| 56 | + |
| 57 | + if (children != null) { |
| 58 | + TreeMap<Integer, List<Term>> positionTermMap = new TreeMap<>(); |
| 59 | + Integer lastPos = null; |
| 60 | + |
| 61 | + for (QueryNode child : children) { |
| 62 | + FieldQueryNode termNode = (FieldQueryNode) child; |
| 63 | + TermQuery termQuery = (TermQuery) termNode |
| 64 | + .getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID); |
| 65 | + |
| 66 | + int pos = termNode.getPositionIncrement(); |
| 67 | + if (pos == 0 && lastPos != null) { |
| 68 | + pos = lastPos; |
| 69 | + } |
| 70 | + else { |
| 71 | + lastPos = pos; |
| 72 | + } |
| 73 | + |
| 74 | + List<Term> termList = positionTermMap.get(pos); |
| 75 | + |
| 76 | + if (termList == null) { |
| 77 | + termList = new LinkedList<>(); |
| 78 | + positionTermMap.put(pos, termList); |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + termList.add(termQuery.getTerm()); |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + for (int positionIncrement : positionTermMap.keySet()) { |
| 87 | + List<Term> termList = positionTermMap.get(positionIncrement); |
| 88 | + |
| 89 | + phraseQueryBuilder.add(termList.toArray(new Term[termList.size()]), |
| 90 | + positionIncrement); |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + return phraseQueryBuilder.build(); |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments