com.sindicetech.siren.solr.qparser.TestNestedQuery.java Source code

Java tutorial

Introduction

Here is the source code for com.sindicetech.siren.solr.qparser.TestNestedQuery.java

Source

/**
 * Copyright (c) 2014, Sindice Limited. All Rights Reserved.
 *
 * This file is part of the SIREn project.
 *
 * Licensed 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 com.sindicetech.siren.solr.qparser;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.junit.Test;

import com.sindicetech.siren.solr.BaseSolrServerTestCase;

import java.io.IOException;

public class TestNestedQuery extends BaseSolrServerTestCase {

    /**
     * When no local parameters are defined, then it should rely on the default
     * solr parser (i.e., lucene).
     */
    @Test
    public void testNoLocalParamater() throws IOException, SolrServerException {
        this.addJsonString("1", "{ \"aaa\" : \"bbb\" }");
        final SolrQuery query = new SolrQuery();
        query.setQuery("aaa : bbb");
        query.setParam("nested", URL_FIELD + ":1");
        query.setRequestHandler("keyword");
        final String[] results = this.search(query, ID_FIELD);
        assertEquals(1, results.length);
    }

    @Test
    public void testSimpleNestedLuceneQuery() throws IOException, SolrServerException {
        this.addJsonString("1", "{ \"aaa\" : \"bbb\" }");
        SolrQuery query = new SolrQuery();
        query.setQuery("aaa : bbb");
        query.setParam("nested", "{!lucene}" + URL_FIELD + ":1");
        query.setRequestHandler("keyword");
        String[] results = this.search(query, ID_FIELD);
        assertEquals(1, results.length);

        query = new SolrQuery();
        query.setQuery("aaa : bbb");
        // non existing document id
        query.setParam("nested", "{!lucene}" + URL_FIELD + ":2");
        query.setRequestHandler("keyword");
        results = this.search(query, ID_FIELD);
        assertEquals(0, results.length);
    }

    @Test
    public void testEmptyMainQueryWithNestedMatchAll() throws IOException, SolrServerException {
        this.addJsonString("1", "{ \"aaa\" : \"bbb\" }");
        SolrQuery query = new SolrQuery();
        query.setParam("nested", "{!lucene} *:*");
        query.setRequestHandler("keyword");
        String[] results = this.search(query, ID_FIELD);
        assertEquals(1, results.length);
    }

    @Test
    public void testMultipleNestedQuery() throws IOException, SolrServerException {
        this.addJsonString("1", "{ \"aaa\" : [\"bbb\", \"ccc\"] }");
        SolrQuery query = new SolrQuery();
        query.setQuery("aaa : bbb");
        query.setParam("nested", "{!lucene}" + URL_FIELD + ":1", "{!keyword} aaa : ccc");
        query.setRequestHandler("keyword");
        String[] results = this.search(query, ID_FIELD);
        assertEquals(1, results.length);

        query = new SolrQuery();
        query.setQuery("aaa : bbb");
        query.setParam("nested", "{!lucene}" + URL_FIELD + ":1", "{!keyword} aaa : ddd");
        query.setRequestHandler("keyword");
        results = this.search(query, ID_FIELD);
        assertEquals(0, results.length);
    }

    @Test
    public void testCurlyBracketInNestedQuery() throws IOException, SolrServerException {
        this.addJsonString("1", "{ \"aaa\" : { \"bbb\" : \"ccc\" } }");
        SolrQuery query = new SolrQuery();
        query.setParam("nested", "{!keyword} aaa : { bbb : ccc } ");
        query.setRequestHandler("keyword");
        String[] results = this.search(query, ID_FIELD);
        assertEquals(1, results.length);

        query = new SolrQuery();
        query.setParam("nested", "{!tree} { \"node\" : { \"query\" : \"ccc\" } }");
        query.setRequestHandler("keyword");
        results = this.search(query, ID_FIELD);
        assertEquals(1, results.length);
    }

}