lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jim...@apache.org
Subject lucene-solr:branch_7x: LUCENE-8531: QueryBuilder#analyzeGraphPhrase now creates one phrase query per finite strings in the graph if the slop is greater than 0. Span queries cannot be used in this case because they don't handle slop the same way than phra
Date Fri, 19 Oct 2018 18:54:51 GMT
Repository: lucene-solr
Updated Branches:
  refs/heads/branch_7x 2f61f96bf -> 36ce83bc9


LUCENE-8531: QueryBuilder#analyzeGraphPhrase now creates one phrase query per finite strings
in the graph if the slop is greater than 0.
Span queries cannot be used in this case because they don't handle slop the same way than
phrase queries.


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/36ce83bc
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/36ce83bc
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/36ce83bc

Branch: refs/heads/branch_7x
Commit: 36ce83bc9add02a900e38b396b42c3c729846598
Parents: 2f61f96
Author: Jim Ferenczi <jimczi@apache.org>
Authored: Fri Oct 19 20:45:16 2018 +0200
Committer: Jim Ferenczi <jimczi@apache.org>
Committed: Fri Oct 19 20:46:47 2018 +0200

----------------------------------------------------------------------
 lucene/CHANGES.txt                              |  4 +++
 .../org/apache/lucene/util/QueryBuilder.java    | 29 ++++++++++++++++----
 .../apache/lucene/util/TestQueryBuilder.java    | 11 ++++++++
 3 files changed, 39 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/36ce83bc/lucene/CHANGES.txt
----------------------------------------------------------------------
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 57630e7..dbba5ad 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -22,6 +22,10 @@ Bug fixes:
 * LUCENE-8522: throw InvalidShapeException when constructing a polygon and
   all points are coplanar. (Ignacio Vera)
 
+* LUCENE-8531: QueryBuilder#analyzeGraphPhrase now creates one phrase query per finite strings
+  in the graph if the slop is greater than 0. Span queries cannot be used in this case because
+  they don't handle slop the same way than phrase queries. (Steve Rowe, Uwe Schindler, Jim
Ferenczi)
+
 New Features
 
 * LUCENE-8496: Selective indexing - modify BKDReader/BKDWriter to allow users

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/36ce83bc/lucene/core/src/java/org/apache/lucene/util/QueryBuilder.java
----------------------------------------------------------------------
diff --git a/lucene/core/src/java/org/apache/lucene/util/QueryBuilder.java b/lucene/core/src/java/org/apache/lucene/util/QueryBuilder.java
index 37b7e3e..1b1c414 100644
--- a/lucene/core/src/java/org/apache/lucene/util/QueryBuilder.java
+++ b/lucene/core/src/java/org/apache/lucene/util/QueryBuilder.java
@@ -16,7 +16,6 @@
  */
 package org.apache.lucene.util;
 
-
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Iterator;
@@ -542,13 +541,33 @@ public class QueryBuilder {
   }
 
   /**
-   * Creates a span near (phrase) query from a graph token stream. The articulation points
of the graph are visited in
-   * order and the queries created at each point are merged in the returned near query.
+   * Creates graph phrase query from the tokenstream contents
    */
-  protected SpanQuery analyzeGraphPhrase(TokenStream source, String field, int phraseSlop)
+  protected Query analyzeGraphPhrase(TokenStream source, String field, int phraseSlop)
       throws IOException {
     source.reset();
     GraphTokenStreamFiniteStrings graph = new GraphTokenStreamFiniteStrings(source);
+    if (phraseSlop > 0) {
+      /**
+       * Creates a boolean query from the graph token stream by extracting all the finite
strings from the graph
+       * and using them to create phrase queries with the appropriate slop.
+       */
+      BooleanQuery.Builder builder = new BooleanQuery.Builder();
+      Iterator<TokenStream> it = graph.getFiniteStrings();
+      while (it.hasNext()) {
+        Query query = createFieldQuery(it.next(), BooleanClause.Occur.MUST, field, true,
phraseSlop);
+        if (query != null) {
+          builder.add(query, BooleanClause.Occur.SHOULD);
+        }
+      }
+      return builder.build();
+    }
+
+    /**
+     * Creates a span near (phrase) query from a graph token stream.
+     * The articulation points of the graph are visited in order and the queries
+     * created at each point are merged in the returned near query.
+     */
     List<SpanQuery> clauses = new ArrayList<>();
     int[] articulationPoints = graph.articulationPoints();
     int lastState = 0;
@@ -610,7 +629,7 @@ public class QueryBuilder {
     } else if (clauses.size() == 1) {
       return clauses.get(0);
     } else {
-      return new SpanNearQuery(clauses.toArray(new SpanQuery[0]), phraseSlop, true);
+      return new SpanNearQuery(clauses.toArray(new SpanQuery[0]), 0, true);
     }
   }
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/36ce83bc/lucene/core/src/test/org/apache/lucene/util/TestQueryBuilder.java
----------------------------------------------------------------------
diff --git a/lucene/core/src/test/org/apache/lucene/util/TestQueryBuilder.java b/lucene/core/src/test/org/apache/lucene/util/TestQueryBuilder.java
index 1604f51..afc1238 100644
--- a/lucene/core/src/test/org/apache/lucene/util/TestQueryBuilder.java
+++ b/lucene/core/src/test/org/apache/lucene/util/TestQueryBuilder.java
@@ -170,6 +170,17 @@ public class TestQueryBuilder extends LuceneTestCase {
         queryBuilder.createPhraseQuery("field", "guinea pig"));
   }
 
+  public void testMultiWordSynonymsPhraseWithSlop() throws Exception {
+    BooleanQuery expected = new BooleanQuery.Builder()
+        .add(new PhraseQuery.Builder().setSlop(4)
+                .add(new Term("field", "guinea")).add(new Term("field", "pig")).build(),
BooleanClause.Occur.SHOULD)
+        .add(new TermQuery(new Term("field", "cavy")), BooleanClause.Occur.SHOULD)
+        .build();
+    QueryBuilder queryBuilder = new QueryBuilder(new MockSynonymAnalyzer());
+    assertEquals(expected,
+        queryBuilder.createPhraseQuery("field", "guinea pig", 4));
+  }
+
   /** forms graph query */
   public void testMultiWordSynonymsBoolean() throws Exception {
     for (BooleanClause.Occur occur : new BooleanClause.Occur[] {BooleanClause.Occur.SHOULD,
BooleanClause.Occur.MUST}) {


Mime
View raw message