Skip to content

Commit

Permalink
SOLR-17477: rename, javadoc, tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
mkhludnev committed Oct 24, 2024
1 parent 259572b commit 0e2d148
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 32 deletions.
14 changes: 12 additions & 2 deletions solr/core/src/java/org/apache/solr/search/facet/FacetParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,18 @@ public Object parseFacetOrStat(String key, Object o) throws SyntaxError {
return parseFacetOrStat(key, type, args);
}

/** Extension point for custom facets and aggs */
public interface ParseHandler {
Object doParse(FacetParser<?> parent, String key, Object args) throws SyntaxError;
/**
* Parse a facet or stat based on the type and arguments provided.
*
* @param parent parent parser
* @param key facet or agg key
* @param args value of facet to parse
* @return either FacetRequest of AggValueSource subclass instance
* @throws SyntaxError on unexpected values
*/
Object parse(FacetParser<?> parent, String key, Object args) throws SyntaxError;
}

private static final Map<String, ParseHandler> REGISTERED_TYPES = new ConcurrentHashMap<>();
Expand All @@ -162,7 +172,7 @@ public static void registerParseHandler(String type, ParseHandler parseHandler)
public Object parseFacetOrStat(String key, String type, Object args) throws SyntaxError {
ParseHandler parseHandler = REGISTERED_TYPES.get(type);
if (parseHandler != null) {
return parseHandler.doParse(this, key, args);
return parseHandler.parse(this, key, args);
}

throw err("Unknown facet or stat. key=" + key + " type=" + type + " args=" + args);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,62 @@
/*
* 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.function;

import java.util.Collections;
import java.util.Map;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.handler.component.ResponseBuilder;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.search.SyntaxError;
import org.apache.solr.search.facet.FacetField;
import org.apache.solr.search.facet.FacetParser;
import org.apache.solr.search.facet.FacetRequest;
import org.junit.BeforeClass;

import java.util.Collections;
import java.util.Map;


public class CustomParseHandlerTest extends SolrTestCaseJ4 {

public static final FacetField FACET_FIELD_STUB = new FacetField();

static class CustomParseHandler implements FacetParser.ParseHandler {
@Override
public Object doParse(FacetParser<?> parent, String key, Object args) {
assertEquals("arg", args);
return FACET_FIELD_STUB;
}
}

@BeforeClass
public static void beforeClass() throws Exception {
initCore("solrconfig.xml", "schema.xml");
}
public static final FacetField FACET_FIELD_STUB = new FacetField();

static class CustomParseHandler implements FacetParser.ParseHandler {
@Override
public void setUp() throws Exception {
super.setUp();
FacetParser.registerParseHandler("custom", new CustomParseHandler());
}

public void testCustomParseHandler() {
SolrQueryRequest req = req();
ResponseBuilder rsp = new ResponseBuilder(req, new SolrQueryResponse(), Collections.emptyList());

FacetRequest facetRequest = FacetRequest.parse(rsp.req, Map.of("bogus",Map.of("custom","arg")));
assertEquals(FACET_FIELD_STUB, facetRequest.getSubFacets().get("bogus"));
public Object parse(FacetParser<?> parent, String key, Object args) {
assertEquals("arg", args);
return FACET_FIELD_STUB;
}
}

@BeforeClass
public static void beforeClass() throws Exception {
initCore("solrconfig.xml", "schema.xml");
}

@Override
public void setUp() throws Exception {
super.setUp();
FacetParser.registerParseHandler("custom", new CustomParseHandler());
}

public void testCustomParseHandler() {
SolrQueryRequest req = req();
ResponseBuilder rsp =
new ResponseBuilder(req, new SolrQueryResponse(), Collections.emptyList());

FacetRequest facetRequest =
FacetRequest.parse(rsp.req, Map.of("bogus", Map.of("custom", "arg")));
assertEquals(FACET_FIELD_STUB, facetRequest.getSubFacets().get("bogus"));
}
}

0 comments on commit 0e2d148

Please sign in to comment.