You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by eh...@apache.org on 2017/05/01 01:56:43 UTC

[2/4] lucene-solr:jira/SOLR-1485: Add {\!payload_check} qparser

Add {\!payload_check} qparser


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

Branch: refs/heads/jira/SOLR-1485
Commit: 4efa068fc629851b919eef9f028a84935ca43687
Parents: 4e76b1e
Author: Erik Hatcher <eh...@apache.org>
Authored: Sun Apr 30 21:49:03 2017 -0400
Committer: Erik Hatcher <eh...@apache.org>
Committed: Sun Apr 30 21:49:03 2017 -0400

----------------------------------------------------------------------
 .../solr/search/PayloadCheckQParserPlugin.java  | 106 +++++++++++++++++++
 .../search/TestPayloadCheckQParserPlugin.java   |  73 +++++++++++++
 2 files changed, 179 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/4efa068f/solr/core/src/java/org/apache/solr/search/PayloadCheckQParserPlugin.java
----------------------------------------------------------------------
diff --git a/solr/core/src/java/org/apache/solr/search/PayloadCheckQParserPlugin.java b/solr/core/src/java/org/apache/solr/search/PayloadCheckQParserPlugin.java
new file mode 100644
index 0000000..cd58b96
--- /dev/null
+++ b/solr/core/src/java/org/apache/solr/search/PayloadCheckQParserPlugin.java
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.search;
+
+import java.lang.invoke.MethodHandles;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.payloads.FloatEncoder;
+import org.apache.lucene.analysis.payloads.IdentityEncoder;
+import org.apache.lucene.analysis.payloads.IntegerEncoder;
+import org.apache.lucene.analysis.payloads.PayloadEncoder;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.queries.payloads.SpanPayloadCheckQuery;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.spans.SpanNearQuery;
+import org.apache.lucene.search.spans.SpanQuery;
+import org.apache.lucene.search.spans.SpanTermQuery;
+import org.apache.lucene.util.BytesRef;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.SolrParams;
+import org.apache.solr.request.SolrQueryRequest;
+import org.apache.solr.schema.FieldType;
+import org.apache.solr.util.PayloadUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class PayloadCheckQParserPlugin extends QParserPlugin {
+  public static final String NAME = "payload_check";
+
+  private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+  @Override
+  public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
+
+    return new QParser(qstr, localParams, params, req) {
+      @Override
+      public Query parse() throws SyntaxError {
+        String field = localParams.get(QueryParsing.F);
+        String value = localParams.get(QueryParsing.V);
+        String p = localParams.get("payloads");
+
+        if (field == null) {
+          throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "'f' not specified");
+        }
+
+        if (value == null) {
+          throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "query string missing");
+        }
+
+        if (p == null) {
+          throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "'payloads' not specified");
+        }
+
+        FieldType ft = req.getCore().getLatestSchema().getFieldType(field);
+        Analyzer analyzer = ft.getQueryAnalyzer();
+        SpanQuery query = PayloadUtils.createSpanQuery(field, value, analyzer);
+
+        if (query == null) {
+          throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "SpanQuery is null");
+        }
+
+        PayloadEncoder encoder = null;
+        String e = PayloadUtils.getPayloadEncoder(ft);
+        if ("float".equals(e)) {    // TODO: centralize this string->PayloadEncoder logic (see DelimitedPayloadTokenFilterFactory)
+          encoder = new FloatEncoder();
+        } else if ("integer".equals(e)) {
+          encoder = new IntegerEncoder();
+        } else if ("identity".equals(e)) {
+          encoder = new IdentityEncoder();
+        }
+
+        if (encoder == null) {
+          throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "invalid encoder: " + e + " for field: " + field);
+        }
+
+        List<BytesRef> payloads = new ArrayList<>();
+        String[] rawPayloads = p.split(" ");  // since payloads (most likely) came in whitespace delimited, just split
+        for (String rawPayload : rawPayloads) {
+          if (rawPayload.length() > 0)
+            payloads.add(encoder.encode(rawPayload.toCharArray()));
+        }
+
+        return new SpanPayloadCheckQuery(query, payloads);
+      }
+    };
+
+
+  }
+}

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/4efa068f/solr/core/src/test/org/apache/solr/search/TestPayloadCheckQParserPlugin.java
----------------------------------------------------------------------
diff --git a/solr/core/src/test/org/apache/solr/search/TestPayloadCheckQParserPlugin.java b/solr/core/src/test/org/apache/solr/search/TestPayloadCheckQParserPlugin.java
new file mode 100644
index 0000000..14bd833
--- /dev/null
+++ b/solr/core/src/test/org/apache/solr/search/TestPayloadCheckQParserPlugin.java
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.search;
+
+import org.apache.solr.SolrTestCaseJ4;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class TestPayloadCheckQParserPlugin extends SolrTestCaseJ4 {
+  @BeforeClass
+  public static void beforeClass() throws Exception {
+    initCore("solrconfig.xml", "schema11.xml");
+    createIndex();
+  }
+
+  public static void createIndex() {
+    assertU(adoc("id","1", "vals_dpi","A|1 B|2 C|3"));
+    assertU(adoc("id","2", "vals_dpf","one|1.0 two|2.0 three|3.0"));
+    assertU(adoc("id","3", "vals_dps","the|ARTICLE cat|NOUN jumped|VERB"));
+    assertU(commit());
+  }
+
+  @Test
+  public void test() {
+    clearIndex();
+
+    String[] should_matches = new String[] {
+        "{!payload_check f=vals_dpi v=A payloads=1}",
+        "{!payload_check f=vals_dpi v=B payloads=2}",
+        "{!payload_check f=vals_dpi v=C payloads=3}",
+        "{!payload_check f=vals_dpi payloads='1 2'}A B",
+        // "{!payload_check f=vals_dpi payloads='1 2.0'}A B",  // ideally this should pass, but IntegerEncoder can't handle "2.0"
+        "{!payload_check f=vals_dpi payloads='1 2 3'}A B C",
+
+        "{!payload_check f=vals_dpf payloads='1 2'}one two",
+        "{!payload_check f=vals_dpf payloads='1 2.0'}one two", // shows that FloatEncoder can handle "1"
+
+        "{!payload_check f=vals_dps payloads='NOUN VERB'}cat jumped"
+    };
+
+    String[] should_not_matches = new String[] {
+        "{!payload_check f=vals_dpi v=A payloads=2}",
+        "{!payload_check f=vals_dpi payloads='1 2'}B C",
+        "{!payload_check f=vals_dpi payloads='1 2 3'}A B",
+        "{!payload_check f=vals_dpi payloads='1 2'}A B C",
+        "{!payload_check f=vals_dpf payloads='1 2.0'}two three",
+        "{!payload_check f=vals_dps payloads='VERB NOUN'}cat jumped"
+    };
+
+    for(String should_match : should_matches) {
+      assertQ(should_match, req("fl","*,score", "q", should_match), "//result[@numFound='1']");
+    }
+
+    for(String should_not_match : should_not_matches) {
+      assertQ(should_not_match, req("fl","*,score", "q", should_not_match), "//result[@numFound='0']");
+    }
+  }
+}