Search in sources :

Example 1 with ScoreMode

use of org.apache.lucene.search.join.ScoreMode in project elasticsearch by elastic.

the class ChildQuerySearchIT method testHasChildQueryWithNestedInnerObjects.

public void testHasChildQueryWithNestedInnerObjects() throws Exception {
    assertAcked(prepareCreate("test").addMapping("parent", "objects", "type=nested").addMapping("child", "_parent", "type=parent"));
    ensureGreen();
    client().prepareIndex("test", "parent", "p1").setSource(jsonBuilder().startObject().field("p_field", "1").startArray("objects").startObject().field("i_field", "1").endObject().startObject().field("i_field", "2").endObject().startObject().field("i_field", "3").endObject().startObject().field("i_field", "4").endObject().startObject().field("i_field", "5").endObject().startObject().field("i_field", "6").endObject().endArray().endObject()).get();
    client().prepareIndex("test", "parent", "p2").setSource(jsonBuilder().startObject().field("p_field", "2").startArray("objects").startObject().field("i_field", "1").endObject().startObject().field("i_field", "2").endObject().endArray().endObject()).get();
    client().prepareIndex("test", "child", "c1").setParent("p1").setSource("c_field", "blue").get();
    client().prepareIndex("test", "child", "c2").setParent("p1").setSource("c_field", "red").get();
    client().prepareIndex("test", "child", "c3").setParent("p2").setSource("c_field", "red").get();
    refresh();
    ScoreMode scoreMode = randomFrom(ScoreMode.values());
    SearchResponse searchResponse = client().prepareSearch("test").setQuery(boolQuery().must(QueryBuilders.hasChildQuery("child", termQuery("c_field", "blue"), scoreMode)).filter(boolQuery().mustNot(termQuery("p_field", "3")))).get();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
    searchResponse = client().prepareSearch("test").setQuery(boolQuery().must(QueryBuilders.hasChildQuery("child", termQuery("c_field", "red"), scoreMode)).filter(boolQuery().mustNot(termQuery("p_field", "3")))).get();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L));
}
Also used : ScoreMode(org.apache.lucene.search.join.ScoreMode) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 2 with ScoreMode

use of org.apache.lucene.search.join.ScoreMode in project elasticsearch by elastic.

the class NestedQueryBuilder method fromXContent.

public static NestedQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    ScoreMode scoreMode = ScoreMode.Avg;
    String queryName = null;
    QueryBuilder query = null;
    String path = null;
    String currentFieldName = null;
    InnerHitBuilder innerHitBuilder = null;
    boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (QUERY_FIELD.match(currentFieldName)) {
                query = parseContext.parseInnerQueryBuilder();
            } else if (INNER_HITS_FIELD.match(currentFieldName)) {
                innerHitBuilder = InnerHitBuilder.fromXContent(parseContext);
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[nested] query does not support [" + currentFieldName + "]");
            }
        } else if (token.isValue()) {
            if (PATH_FIELD.match(currentFieldName)) {
                path = parser.text();
            } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                boost = parser.floatValue();
            } else if (IGNORE_UNMAPPED_FIELD.match(currentFieldName)) {
                ignoreUnmapped = parser.booleanValue();
            } else if (SCORE_MODE_FIELD.match(currentFieldName)) {
                scoreMode = HasChildQueryBuilder.parseScoreMode(parser.text());
            } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                queryName = parser.text();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[nested] query does not support [" + currentFieldName + "]");
            }
        }
    }
    NestedQueryBuilder queryBuilder = new NestedQueryBuilder(path, query, scoreMode).ignoreUnmapped(ignoreUnmapped).queryName(queryName).boost(boost);
    if (innerHitBuilder != null) {
        queryBuilder.innerHit(innerHitBuilder, ignoreUnmapped);
    }
    return queryBuilder;
}
Also used : ScoreMode(org.apache.lucene.search.join.ScoreMode) ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 3 with ScoreMode

use of org.apache.lucene.search.join.ScoreMode in project elasticsearch by elastic.

the class HasChildQueryBuilder method fromXContent.

public static HasChildQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String childType = null;
    ScoreMode scoreMode = ScoreMode.None;
    int minChildren = HasChildQueryBuilder.DEFAULT_MIN_CHILDREN;
    int maxChildren = HasChildQueryBuilder.DEFAULT_MAX_CHILDREN;
    boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;
    String queryName = null;
    InnerHitBuilder innerHitBuilder = null;
    String currentFieldName = null;
    XContentParser.Token token;
    QueryBuilder iqb = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (parseContext.isDeprecatedSetting(currentFieldName)) {
        // skip
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (QUERY_FIELD.match(currentFieldName)) {
                iqb = parseContext.parseInnerQueryBuilder();
            } else if (INNER_HITS_FIELD.match(currentFieldName)) {
                innerHitBuilder = InnerHitBuilder.fromXContent(parseContext);
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[has_child] query does not support [" + currentFieldName + "]");
            }
        } else if (token.isValue()) {
            if (TYPE_FIELD.match(currentFieldName)) {
                childType = parser.text();
            } else if (SCORE_MODE_FIELD.match(currentFieldName)) {
                scoreMode = parseScoreMode(parser.text());
            } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                boost = parser.floatValue();
            } else if (MIN_CHILDREN_FIELD.match(currentFieldName)) {
                minChildren = parser.intValue(true);
            } else if (MAX_CHILDREN_FIELD.match(currentFieldName)) {
                maxChildren = parser.intValue(true);
            } else if (IGNORE_UNMAPPED_FIELD.match(currentFieldName)) {
                ignoreUnmapped = parser.booleanValue();
            } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                queryName = parser.text();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[has_child] query does not support [" + currentFieldName + "]");
            }
        }
    }
    HasChildQueryBuilder hasChildQueryBuilder = new HasChildQueryBuilder(childType, iqb, scoreMode);
    hasChildQueryBuilder.minMaxChildren(minChildren, maxChildren);
    hasChildQueryBuilder.queryName(queryName);
    hasChildQueryBuilder.boost(boost);
    hasChildQueryBuilder.ignoreUnmapped(ignoreUnmapped);
    if (innerHitBuilder != null) {
        hasChildQueryBuilder.innerHit(innerHitBuilder, ignoreUnmapped);
    }
    return hasChildQueryBuilder;
}
Also used : ScoreMode(org.apache.lucene.search.join.ScoreMode) ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 4 with ScoreMode

use of org.apache.lucene.search.join.ScoreMode in project lucene-solr by apache.

the class TestScoreJoinQPScore method not.

private ScoreMode not(ScoreMode s) {
    Random r = random();
    final List<ScoreMode> l = new ArrayList(Arrays.asList(ScoreMode.values()));
    l.remove(s);
    return l.get(r.nextInt(l.size()));
}
Also used : ScoreMode(org.apache.lucene.search.join.ScoreMode) Random(java.util.Random) ArrayList(java.util.ArrayList)

Example 5 with ScoreMode

use of org.apache.lucene.search.join.ScoreMode in project lucene-solr by apache.

the class TestScoreJoinQPScore method testBoost.

@Ignore("SOLR-7814, also don't forget cover boost at testCacheHit()")
public void testBoost() throws Exception {
    indexDataForScorring();
    ScoreMode score = ScoreMode.values()[random().nextInt(ScoreMode.values().length)];
    final SolrQueryRequest req = req("q", "{!join from=movieId_s to=id score=" + score + " b=200}title:movie", "fl", "id,score", "omitHeader", "true");
    SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, new SolrQueryResponse()));
    final Query luceneQ = QParser.getParser(req.getParams().get("q"), req).getQuery().rewrite(req.getSearcher().getSlowAtomicReader());
    assertTrue(luceneQ instanceof BoostQuery);
    float boost = ((BoostQuery) luceneQ).getBoost();
    assertEquals("" + luceneQ, Float.floatToIntBits(200), Float.floatToIntBits(boost));
    SolrRequestInfo.clearRequestInfo();
    req.close();
}
Also used : ScoreMode(org.apache.lucene.search.join.ScoreMode) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) Query(org.apache.lucene.search.Query) BoostQuery(org.apache.lucene.search.BoostQuery) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) BoostQuery(org.apache.lucene.search.BoostQuery) Ignore(org.junit.Ignore)

Aggregations

ScoreMode (org.apache.lucene.search.join.ScoreMode)6 Query (org.apache.lucene.search.Query)2 ParsingException (org.elasticsearch.common.ParsingException)2 XContentParser (org.elasticsearch.common.xcontent.XContentParser)2 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 BoostQuery (org.apache.lucene.search.BoostQuery)1 SolrException (org.apache.solr.common.SolrException)1 CoreContainer (org.apache.solr.core.CoreContainer)1 SolrCore (org.apache.solr.core.SolrCore)1 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)1 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)1 SolrRequestInfo (org.apache.solr.request.SolrRequestInfo)1 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)1 QParser (org.apache.solr.search.QParser)1 SolrIndexSearcher (org.apache.solr.search.SolrIndexSearcher)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 Ignore (org.junit.Ignore)1