You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by yo...@apache.org on 2018/05/27 20:22:38 UTC

lucene-solr:master: SOLR-9685: #tagging in JSON query syntax

Repository: lucene-solr
Updated Branches:
  refs/heads/master fd929c1d6 -> e5998fcb5


SOLR-9685: #tagging in JSON query syntax


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

Branch: refs/heads/master
Commit: e5998fcb5df1d1129f0ddefb518411ff2b910912
Parents: fd929c1
Author: yonik <yo...@apache.org>
Authored: Sun May 27 16:22:16 2018 -0400
Committer: yonik <yo...@apache.org>
Committed: Sun May 27 16:22:16 2018 -0400

----------------------------------------------------------------------
 solr/CHANGES.txt                                |   6 +
 .../solr/request/json/JsonQueryConverter.java   |  25 ++-
 .../solr/search/json/TestJsonRequest.java       | 158 +++++++++++++++++--
 3 files changed, 174 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/e5998fcb/solr/CHANGES.txt
----------------------------------------------------------------------
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 4cb89bd..d1398bb 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -128,6 +128,12 @@ New Features
   requests will fail when the coordinating node can't communicate with ZooKeeper, 
   by setting the "shards.tolerant" param to "requireZkConnected".  (Steve Rowe)
 
+* SOLR-9685: #Tagging queries in JSON Query DSL, equivalent to LocalParams based query/filter
+  tagging.  Multiple tags are comma separated.
+  LocalParams Example     : {!tag=colorfilt}color:blue
+  Equivalent JSON Example : { "#colorfilt" : "color:blue" } 
+  (Dmitry Tikhonov, Mikhail Khludnev, yonik)
+
 Bug Fixes
 ----------------------
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/e5998fcb/solr/core/src/java/org/apache/solr/request/json/JsonQueryConverter.java
----------------------------------------------------------------------
diff --git a/solr/core/src/java/org/apache/solr/request/json/JsonQueryConverter.java b/solr/core/src/java/org/apache/solr/request/json/JsonQueryConverter.java
index e732470..8f8d04a 100644
--- a/solr/core/src/java/org/apache/solr/request/json/JsonQueryConverter.java
+++ b/solr/core/src/java/org/apache/solr/request/json/JsonQueryConverter.java
@@ -68,8 +68,21 @@ class JsonQueryConverter {
         throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
             "Error when parsing json query, expect only one query parser here, but found : "+map.keySet());
       }
+
       String qtype = map.keySet().iterator().next();
-      Object subVal = map.get(qtype);
+      String tagName = null;
+      if (qtype.startsWith("#")) {
+        Object taggedQueryObject = map.get(qtype);
+        tagName = qtype.substring(1);
+        if (taggedQueryObject instanceof String) {
+          builder.append("{!tag=").append(tagName).append("}");
+          builder.append(taggedQueryObject);
+          return;
+        } else if (taggedQueryObject instanceof Map) {
+          map = (Map<String, Object>) taggedQueryObject;
+          qtype = map.keySet().iterator().next();
+        }
+      }
 
       // We don't want to introduce unnecessary variable at root level
       boolean useSubBuilder = builder.length() > 0;
@@ -77,11 +90,17 @@ class JsonQueryConverter {
 
       if (useSubBuilder) subBuilder = new StringBuilder();
 
-      subBuilder = subBuilder.append("{!").append(qtype).append(' ');;
+      Object subVal = map.get(qtype);
+      subBuilder = subBuilder.append("{!").append(qtype).append(' ');
+      if (tagName != null) {
+        subBuilder.append("tag=").append(tagName).append(' ');
+      }
       buildLocalParams(subBuilder, subVal, false, additionalParams);
       subBuilder.append("}");
 
-      if (useSubBuilder) builder.append('$').append(putParam(subBuilder.toString(), additionalParams));
+      if (useSubBuilder) {
+        builder.append('$').append(putParam(subBuilder.toString(), additionalParams));
+      }
     } else {
       for (Map.Entry<String, Object> entry : map.entrySet()) {
         String key = entry.getKey();

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/e5998fcb/solr/core/src/test/org/apache/solr/search/json/TestJsonRequest.java
----------------------------------------------------------------------
diff --git a/solr/core/src/test/org/apache/solr/search/json/TestJsonRequest.java b/solr/core/src/test/org/apache/solr/search/json/TestJsonRequest.java
index 4f47f8a..68ee8f0 100644
--- a/solr/core/src/test/org/apache/solr/search/json/TestJsonRequest.java
+++ b/solr/core/src/test/org/apache/solr/search/json/TestJsonRequest.java
@@ -57,26 +57,20 @@ public class TestJsonRequest extends SolrTestCaseHS {
   }
 
   @Test
+  public void testLocalJsonRequestWithTags() throws Exception {
+    doJsonRequestWithTag(Client.localClient);
+  }
+
+  @Test
   public void testDistribJsonRequest() throws Exception {
     initServers();
-    initServers();
     Client client = servers.getClient( random().nextInt() );
     client.queryDefaults().set( "shards", servers.getShards() );
     doJsonRequest(client, true);
   }
 
   public static void doJsonRequest(Client client, boolean isDistrib) throws Exception {
-    client.deleteByQuery("*:*", null);
-    client.add(sdoc("id", "1", "cat_s", "A", "where_s", "NY"), null);
-    client.add(sdoc("id", "2", "cat_s", "B", "where_s", "NJ"), null);
-    client.add(sdoc("id", "3"), null);
-    client.commit();
-    client.add(sdoc("id", "4", "cat_s", "A", "where_s", "NJ"), null);
-    client.add(sdoc("id", "5", "cat_s", "B", "where_s", "NJ"), null);
-    client.commit();
-    client.add(sdoc("id", "6", "cat_s", "B", "where_s", "NY"), null);
-    client.commit();
-
+    addDocs(client);
 
     // test json param
     client.testJQ( params("json","{query:'cat_s:A'}")
@@ -401,4 +395,144 @@ public class TestJsonRequest extends SolrTestCaseHS {
 
   }
 
+  public static void doJsonRequestWithTag(Client client) throws Exception {
+    addDocs(client);
+
+    try {
+      client.testJQ( params("json","{" +
+          " query : '*:*'," +
+          " filter : { \"RCAT\" : \"cat_s:A\" }" +
+          "}", "json.facet", "{" +
+          "categories:{ type:terms, field:cat_s, domain:{excludeTags:\"RCAT\"} }  " +
+          "}"), "facets=={ count:2, " +
+          " categories:{ buckets:[ {val:B, count:3}, {val:A, count:2} ]  }" +
+          "}"
+      );
+      fail("no # no tag");
+    } catch (Exception e) {
+      assertTrue(e.getMessage().contains("expect a json object"));
+    }
+
+    final String taggedQ = "{" +
+            " \"#RCAT\" : " + (random().nextBoolean() ? 
+                "{" +
+                "     term : {" +
+                "       f : cat_s," +
+                "       v : A" +
+                "     } " +
+                "   } " 
+            : "\"cat_s:A\"")+
+            " } ";
+    boolean queryAndFilter = random().nextBoolean() ;
+    client.testJQ(params("json", "{" +
+        " query :" + ( queryAndFilter ? " '*:*', filter : " : "")
+        + (!queryAndFilter || random().nextBoolean() ? taggedQ : "["+taggedQ+"]" )+
+        "}", "json.facet", "{" +
+        "categories:{ type:terms, field:cat_s, domain:{excludeTags:\"RCAT\"} }  " +
+        "}"), "facets=={ count:2, " +
+        " categories:{ buckets:[ {val:B, count:3}, {val:A, count:2} ]  }" +
+        "}"
+    );
+
+    client.testJQ( params("json","{" +
+        " query : '*:*'," +
+        " filter : {" +
+        "  term : {" +
+        "   f : cat_s," +
+        "   v : A" +
+        "  } " +
+        " } " +
+        "}", "json.facet", "{" +
+        "categories:{ type:terms, field:cat_s"
+        +( random().nextBoolean() ? ", domain:{excludeTags:\"RCAT\"} ": " ")
+        + "}  " +
+        "}"), "facets=={ count:2, " +
+        " categories:{ buckets:[ {val:A, count:2} ] }" +
+        "}"
+    );
+
+    client.testJQ( params("json","{" +
+        " query : '*:*'," +
+        " filter : {" +
+        "   \"#RCAT\" : {" +
+        "     term : {" +
+        "       f : cat_s," +
+        "       v : A" +
+        "     } " +
+        "   } " +
+        " } " +
+        "}", "json.facet", "{" +
+        "categories:{ type:terms, field:cat_s }  " +
+        "}"), "facets=={ count:2, " +
+        " categories:{ buckets:[ {val:A, count:2} ] }" +
+        "}"
+    );
+
+    boolean multiTag = random().nextBoolean();
+    client.testJQ(params("json", "{" +
+            " query : '*:*'," +
+            " filter : [" +
+            "{ \"#RCAT"+(multiTag ? ",RCATSECONDTAG":"") + "\" :  \"cat_s:A\" }," +
+            "{ \"#RWHERE\" : {" +
+            "     term : {" +
+            "       f : where_s," +
+            "       v : NY" +
+            "     } " +
+            "   }" +
+            "}]}"
+        , "json.facet", "{" +
+            "categories:{ type:terms, field:cat_s, domain:{excludeTags:\"RCAT\"} }  " +
+            "countries:{ type:terms, field:where_s, domain:{excludeTags:\"RWHERE\"} }  " +
+            "ids:{ type:terms, field:id, domain:{excludeTags:[\""+ (multiTag ? "RCATSECONDTAG":"RCAT")+ "\", \"RWHERE\"]} }  " +
+            "}"), "facets==" + "{\n" +
+        "    \"count\":1,\n" +
+        "    \"categories\":{\n" +
+        "      \"buckets\":[{\n" +
+        "          \"val\":\"A\",\n" +
+        "          \"count\":1},\n" +
+        "        {\n" +
+        "          \"val\":\"B\",\n" +
+        "          \"count\":1}]},\n" +
+        "    \"countries\":{\n" +
+        "      \"buckets\":[{\n" +
+        "          \"val\":\"NJ\",\n" +
+        "          \"count\":1},\n" +
+        "        {\n" +
+        "          \"val\":\"NY\",\n" +
+        "          \"count\":1}]},\n" +
+        "    \"ids\":{\n" +
+        "      \"buckets\":[{\n" +
+        "          \"val\":\"1\",\n" +
+        "          \"count\":1},\n" +
+        "        {\n" +
+        "          \"val\":\"2\",\n" +
+        "          \"count\":1},\n" +
+        "        {\n" +
+        "          \"val\":\"3\",\n" +
+        "          \"count\":1},\n" +
+        "        {\n" +
+        "          \"val\":\"4\",\n" +
+        "          \"count\":1},\n" +
+        "        {\n" +
+        "          \"val\":\"5\",\n" +
+        "          \"count\":1},\n" +
+        "        {\n" +
+        "          \"val\":\"6\",\n" +
+        "          \"count\":1}]}}}"
+    );
+  }
+
+  private static void addDocs(Client client) throws Exception {
+    client.deleteByQuery("*:*", null);
+    client.add(sdoc("id", "1", "cat_s", "A", "where_s", "NY"), null);
+    client.add(sdoc("id", "2", "cat_s", "B", "where_s", "NJ"), null);
+    client.add(sdoc("id", "3"), null);
+    client.commit();
+    client.add(sdoc("id", "4", "cat_s", "A", "where_s", "NJ"), null);
+    client.add(sdoc("id", "5", "cat_s", "B", "where_s", "NJ"), null);
+    client.commit();
+    client.add(sdoc("id", "6", "cat_s", "B", "where_s", "NY"), null);
+    client.commit();
+  }
+
 }