Search in sources :

Example 1 with SignificanceHeuristicParser

use of org.elasticsearch.search.aggregations.bucket.significant.heuristics.SignificanceHeuristicParser in project elasticsearch by elastic.

the class SignificanceHeuristicTests method testBuilderAndParser.

// test that
// 1. The output of the builders can actually be parsed
// 2. The parser does not swallow parameters after a significance heuristic was defined
public void testBuilderAndParser() throws Exception {
    SearchModule searchModule = new SearchModule(Settings.EMPTY, false, emptyList());
    ParseFieldRegistry<SignificanceHeuristicParser> heuristicParserMapper = searchModule.getSignificanceHeuristicParserRegistry();
    // test jlh with string
    assertTrue(parseFromString(heuristicParserMapper, "\"jlh\":{}") instanceof JLHScore);
    // test gnd with string
    assertTrue(parseFromString(heuristicParserMapper, "\"gnd\":{}") instanceof GND);
    // test mutual information with string
    boolean includeNegatives = randomBoolean();
    boolean backgroundIsSuperset = randomBoolean();
    String mutual = "\"mutual_information\":{\"include_negatives\": " + includeNegatives + ", \"background_is_superset\":" + backgroundIsSuperset + "}";
    assertEquals(new MutualInformation(includeNegatives, backgroundIsSuperset), parseFromString(heuristicParserMapper, mutual));
    String chiSquare = "\"chi_square\":{\"include_negatives\": " + includeNegatives + ", \"background_is_superset\":" + backgroundIsSuperset + "}";
    assertEquals(new ChiSquare(includeNegatives, backgroundIsSuperset), parseFromString(heuristicParserMapper, chiSquare));
    // test with builders
    assertThat(parseFromBuilder(heuristicParserMapper, new JLHScore()), instanceOf(JLHScore.class));
    assertThat(parseFromBuilder(heuristicParserMapper, new GND(backgroundIsSuperset)), instanceOf(GND.class));
    assertEquals(new MutualInformation(includeNegatives, backgroundIsSuperset), parseFromBuilder(heuristicParserMapper, new MutualInformation(includeNegatives, backgroundIsSuperset)));
    assertEquals(new ChiSquare(includeNegatives, backgroundIsSuperset), parseFromBuilder(heuristicParserMapper, new ChiSquare(includeNegatives, backgroundIsSuperset)));
    // test exceptions
    String faultyHeuristicdefinition = "\"mutual_information\":{\"include_negatives\": false, \"some_unknown_field\": false}";
    String expectedError = "unknown field [some_unknown_field]";
    checkParseException(heuristicParserMapper, faultyHeuristicdefinition, expectedError);
    faultyHeuristicdefinition = "\"chi_square\":{\"unknown_field\": true}";
    expectedError = "unknown field [unknown_field]";
    checkParseException(heuristicParserMapper, faultyHeuristicdefinition, expectedError);
    faultyHeuristicdefinition = "\"jlh\":{\"unknown_field\": true}";
    expectedError = "expected an empty object, but found ";
    checkParseException(heuristicParserMapper, faultyHeuristicdefinition, expectedError);
    faultyHeuristicdefinition = "\"gnd\":{\"unknown_field\": true}";
    expectedError = "unknown field [unknown_field]";
    checkParseException(heuristicParserMapper, faultyHeuristicdefinition, expectedError);
}
Also used : JLHScore(org.elasticsearch.search.aggregations.bucket.significant.heuristics.JLHScore) ChiSquare(org.elasticsearch.search.aggregations.bucket.significant.heuristics.ChiSquare) SearchModule(org.elasticsearch.search.SearchModule) MutualInformation(org.elasticsearch.search.aggregations.bucket.significant.heuristics.MutualInformation) Matchers.containsString(org.hamcrest.Matchers.containsString) SignificanceHeuristicParser(org.elasticsearch.search.aggregations.bucket.significant.heuristics.SignificanceHeuristicParser) GND(org.elasticsearch.search.aggregations.bucket.significant.heuristics.GND)

Example 2 with SignificanceHeuristicParser

use of org.elasticsearch.search.aggregations.bucket.significant.heuristics.SignificanceHeuristicParser in project elasticsearch by elastic.

the class SignificantTermsAggregationBuilder method getParser.

public static Aggregator.Parser getParser(ParseFieldRegistry<SignificanceHeuristicParser> significanceHeuristicParserRegistry) {
    ObjectParser<SignificantTermsAggregationBuilder, QueryParseContext> parser = new ObjectParser<>(SignificantTermsAggregationBuilder.NAME);
    ValuesSourceParserHelper.declareAnyFields(parser, true, true);
    parser.declareInt(SignificantTermsAggregationBuilder::shardSize, TermsAggregationBuilder.SHARD_SIZE_FIELD_NAME);
    parser.declareLong(SignificantTermsAggregationBuilder::minDocCount, TermsAggregationBuilder.MIN_DOC_COUNT_FIELD_NAME);
    parser.declareLong(SignificantTermsAggregationBuilder::shardMinDocCount, TermsAggregationBuilder.SHARD_MIN_DOC_COUNT_FIELD_NAME);
    parser.declareInt(SignificantTermsAggregationBuilder::size, TermsAggregationBuilder.REQUIRED_SIZE_FIELD_NAME);
    parser.declareString(SignificantTermsAggregationBuilder::executionHint, TermsAggregationBuilder.EXECUTION_HINT_FIELD_NAME);
    parser.declareObject(SignificantTermsAggregationBuilder::backgroundFilter, (p, context) -> context.parseInnerQueryBuilder(), SignificantTermsAggregationBuilder.BACKGROUND_FILTER);
    parser.declareField((b, v) -> b.includeExclude(IncludeExclude.merge(v, b.includeExclude())), IncludeExclude::parseInclude, IncludeExclude.INCLUDE_FIELD, ObjectParser.ValueType.OBJECT_ARRAY_OR_STRING);
    parser.declareField((b, v) -> b.includeExclude(IncludeExclude.merge(b.includeExclude(), v)), IncludeExclude::parseExclude, IncludeExclude.EXCLUDE_FIELD, ObjectParser.ValueType.STRING_ARRAY);
    for (String name : significanceHeuristicParserRegistry.getNames()) {
        parser.declareObject(SignificantTermsAggregationBuilder::significanceHeuristic, (p, context) -> {
            SignificanceHeuristicParser significanceHeuristicParser = significanceHeuristicParserRegistry.lookupReturningNullIfNotFound(name);
            return significanceHeuristicParser.parse(context);
        }, new ParseField(name));
    }
    return new Aggregator.Parser() {

        @Override
        public AggregationBuilder parse(String aggregationName, QueryParseContext context) throws IOException {
            return parser.parse(context.parser(), new SignificantTermsAggregationBuilder(aggregationName, null), context);
        }
    };
}
Also used : ObjectParser(org.elasticsearch.common.xcontent.ObjectParser) QueryParseContext(org.elasticsearch.index.query.QueryParseContext) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude) SignificanceHeuristicParser(org.elasticsearch.search.aggregations.bucket.significant.heuristics.SignificanceHeuristicParser) ParseField(org.elasticsearch.common.ParseField) SignificanceHeuristicParser(org.elasticsearch.search.aggregations.bucket.significant.heuristics.SignificanceHeuristicParser) ObjectParser(org.elasticsearch.common.xcontent.ObjectParser)

Aggregations

SignificanceHeuristicParser (org.elasticsearch.search.aggregations.bucket.significant.heuristics.SignificanceHeuristicParser)2 ParseField (org.elasticsearch.common.ParseField)1 ObjectParser (org.elasticsearch.common.xcontent.ObjectParser)1 QueryParseContext (org.elasticsearch.index.query.QueryParseContext)1 SearchModule (org.elasticsearch.search.SearchModule)1 ChiSquare (org.elasticsearch.search.aggregations.bucket.significant.heuristics.ChiSquare)1 GND (org.elasticsearch.search.aggregations.bucket.significant.heuristics.GND)1 JLHScore (org.elasticsearch.search.aggregations.bucket.significant.heuristics.JLHScore)1 MutualInformation (org.elasticsearch.search.aggregations.bucket.significant.heuristics.MutualInformation)1 IncludeExclude (org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1