<commit_before>/*
 * Copyright 2012 MarkLogic Corporation
 *
 * 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.marklogic.client.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.IOException;

import javax.xml.namespace.QName;
import javax.xml.parsers.ParserConfigurationException;

import com.marklogic.client.io.BytesHandle;
import com.marklogic.client.io.Format;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;

import com.marklogic.client.admin.QueryOptionsManager;
import com.marklogic.client.admin.config.QueryOptions.QueryRange;
import com.marklogic.client.admin.config.QueryOptions.QueryTransformResults;
import com.marklogic.client.admin.config.QueryOptionsBuilder;
import com.marklogic.client.io.QueryOptionsHandle;
import com.marklogic.client.io.SearchHandle;
import com.marklogic.client.query.FacetResult;
import com.marklogic.client.query.MatchDocumentSummary;
import com.marklogic.client.query.MatchLocation;
import com.marklogic.client.query.QueryManager;
import com.marklogic.client.query.QueryManager.QueryView;
import com.marklogic.client.query.StringQueryDefinition;

public class StringSearchTest {
	@SuppressWarnings("unused")
	private static final Logger logger = (Logger) LoggerFactory
			.getLogger(QueryOptionsHandleTest.class);
	
    @BeforeClass
    public static void beforeClass() {
        Common.connectAdmin();
    }

    @AfterClass
    public static void afterClass() {
        Common.release();
    }

    @Test
    public void testStringSearch() throws IOException, ParserConfigurationException, SAXException {
        String optionsName = writeOptions();

        QueryManager queryMgr = Common.client.newQueryManager();

        StringQueryDefinition qdef = queryMgr.newStringDefinition(optionsName);
        qdef.setCriteria("grandchild1 OR grandchild4");

        SearchHandle results = queryMgr.search(qdef, new SearchHandle());
        assertNotNull(results);
        assertFalse(results.getMetrics().getTotalTime() == -1);

        FacetResult[] facets = results.getFacetResults();
        assertNotNull(facets);
        assertTrue(facets.length > 0);

        MatchDocumentSummary[] summaries = results.getMatchResults();
        assertTrue(summaries.length > 0);
        for (MatchDocumentSummary summary : summaries) {
        	MatchLocation[] locations = summary.getMatchLocations();
            for (MatchLocation location : locations) {
                assertNotNull(location.getAllSnippetText());
            }
        }

        assertNotNull(summaries);
    }

    @Test
    public void testStringSearch2() throws IOException {
        QueryManager queryMgr = Common.client.newQueryManager();

        // "em-1"
        StringQueryDefinition qdef = queryMgr.newStringDefinition();
        qdef.setCriteria("10");

        SearchHandle handle = new SearchHandle();
        handle = queryMgr.search(qdef, handle);

        assertNotNull(handle);
    }

    @Test
    public void testStringSearch3() throws IOException {
        QueryManager queryMgr = Common.client.newQueryManager();
        // "metatest-1"
        StringQueryDefinition qdef = queryMgr.newStringDefinition();
        qdef.setCriteria("10");

        SearchHandle handle = new SearchHandle();
        handle = queryMgr.search(qdef, handle);

        assertNotNull(handle);
    }

    @Test
    public void testStringSearch4() throws IOException {
        String optionsName = writeOptions();

        QueryManager queryMgr = Common.client.newQueryManager();

        StringQueryDefinition qdef = queryMgr.newStringDefinition(optionsName);
        qdef.setCriteria("grandchild1 OR grandchild4");

        queryMgr.setView(QueryView.FACETS);
        SearchHandle results = queryMgr.search(qdef, new SearchHandle());
        assertNotNull(results);

        FacetResult[] facets = results.getFacetResults();
        assertNotNull(facets);
        assertTrue(facets.length > 0);

        MatchDocumentSummary[] summaries = results.getMatchResults();
        assertTrue(summaries == null || summaries.length == 0);

        queryMgr.setView(QueryView.RESULTS);
        results = queryMgr.search(qdef, new SearchHandle());
        assertNotNull(results);

        facets = results.getFacetResults();
        assertTrue(facets == null || facets.length == 0);

        summaries = results.getMatchResults();
        assertNotNull(summaries);
        assertTrue(summaries.length > 0);
    }

    private String writeOptions() {
        String optionsName = "facets";

        // Get back facets...
        QueryOptionsBuilder builder = new QueryOptionsBuilder();
		QueryRange grandchildRange = builder.range(
				builder.elementRangeIndex(
						new QName("grandchild"),
						builder.stringRangeType("http://marklogic.com/collation/")
						));
		grandchildRange.setDoFacets(true);
		QueryOptionsHandle options = new QueryOptionsHandle().withConstraints(
        		builder.constraint("grandchild",grandchildRange)
        		);

        QueryRange range = options.getConstraint("grandchild").getSource();
        assertEquals(range.getElement(), new QName("grandchild"));

        QueryTransformResults tresults = builder.emptySnippets();
        options.withTransformResults(tresults);<commit_msg>Added JSON result test<commit_after>/*
 * Copyright 2012 MarkLogic Corporation
 *
 * 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.marklogic.client.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.IOException;

import javax.xml.namespace.QName;
import javax.xml.parsers.ParserConfigurationException;

import com.marklogic.client.io.BytesHandle;
import com.marklogic.client.io.Format;
import com.marklogic.client.io.StringHandle;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;

import com.marklogic.client.admin.QueryOptionsManager;
import com.marklogic.client.admin.config.QueryOptions.QueryRange;
import com.marklogic.client.admin.config.QueryOptions.QueryTransformResults;
import com.marklogic.client.admin.config.QueryOptionsBuilder;
import com.marklogic.client.io.QueryOptionsHandle;
import com.marklogic.client.io.SearchHandle;
import com.marklogic.client.query.FacetResult;
import com.marklogic.client.query.MatchDocumentSummary;
import com.marklogic.client.query.MatchLocation;
import com.marklogic.client.query.QueryManager;
import com.marklogic.client.query.QueryManager.QueryView;
import com.marklogic.client.query.StringQueryDefinition;

public class StringSearchTest {
	@SuppressWarnings("unused")
	private static final Logger logger = (Logger) LoggerFactory
			.getLogger(QueryOptionsHandleTest.class);
	
    @BeforeClass
    public static void beforeClass() {
        Common.connectAdmin();
    }

    @AfterClass
    public static void afterClass() {
        Common.release();
    }

    @Test
    public void testStringSearch() throws IOException, ParserConfigurationException, SAXException {
        String optionsName = writeOptions();

        QueryManager queryMgr = Common.client.newQueryManager();

        StringQueryDefinition qdef = queryMgr.newStringDefinition(optionsName);
        qdef.setCriteria("grandchild1 OR grandchild4");

        SearchHandle results = queryMgr.search(qdef, new SearchHandle());
        assertNotNull(results);
        assertFalse(results.getMetrics().getTotalTime() == -1);

        FacetResult[] facets = results.getFacetResults();
        assertNotNull(facets);
        assertTrue(facets.length > 0);

        MatchDocumentSummary[] summaries = results.getMatchResults();
        assertTrue(summaries.length > 0);
        for (MatchDocumentSummary summary : summaries) {
        	MatchLocation[] locations = summary.getMatchLocations();
            for (MatchLocation location : locations) {
                assertNotNull(location.getAllSnippetText());
            }
        }

        assertNotNull(summaries);
    }

    @Test
    public void testStringSearch2() throws IOException {
        QueryManager queryMgr = Common.client.newQueryManager();

        // "em-1"
        StringQueryDefinition qdef = queryMgr.newStringDefinition();
        qdef.setCriteria("10");

        SearchHandle handle = new SearchHandle();
        handle = queryMgr.search(qdef, handle);

        assertNotNull(handle);
    }

    @Test
    public void testStringSearch3() throws IOException {
        QueryManager queryMgr = Common.client.newQueryManager();
        // "metatest-1"
        StringQueryDefinition qdef = queryMgr.newStringDefinition();
        qdef.setCriteria("10");

        SearchHandle handle = new SearchHandle();
        handle = queryMgr.search(qdef, handle);

        assertNotNull(handle);
    }

    @Test
    public void testStringSearch4() throws IOException {
        String optionsName = writeOptions();

        QueryManager queryMgr = Common.client.newQueryManager();

        StringQueryDefinition qdef = queryMgr.newStringDefinition(optionsName);
        qdef.setCriteria("grandchild1 OR grandchild4");

        queryMgr.setView(QueryView.FACETS);
        SearchHandle results = queryMgr.search(qdef, new SearchHandle());
        assertNotNull(results);

        FacetResult[] facets = results.getFacetResults();
        assertNotNull(facets);
        assertTrue(facets.length > 0);

        MatchDocumentSummary[] summaries = results.getMatchResults();
        assertTrue(summaries == null || summaries.length == 0);

        queryMgr.setView(QueryView.RESULTS);
        results = queryMgr.search(qdef, new SearchHandle());
        assertNotNull(results);

        facets = results.getFacetResults();
        assertTrue(facets == null || facets.length == 0);

        summaries = results.getMatchResults();
        assertNotNull(summaries);
        assertTrue(summaries.length > 0);
    }

    @Test
    public void testJSON() {
        String optionsName = writeOptions();

        QueryManager queryMgr = Common.client.newQueryManager();

        StringQueryDefinition qdef = queryMgr.newStringDefinition(optionsName);
        qdef.setCriteria("grandchild1 OR grandchild4");

        queryMgr.setView(QueryView.FACETS);

        // create a handle for the search results
        StringHandle resultsHandle = new StringHandle().withFormat(Format.JSON);

        // run the search
        queryMgr.search(qdef, resultsHandle);

        assertEquals("{", resultsHandle.get().substring(0, 1)); // It's JSON, right?
    }

    private String writeOptions() {
        String optionsName = "facets";

        // Get back facets...
        QueryOptionsBuilder builder = new QueryOptionsBuilder();
		QueryRange grandchildRange = builder.range(
				builder.elementRangeIndex(
						new QName("grandchild"),
						builder.stringRangeType("http://marklogic.com/collation/")
						));
		grandchildRange.setDoFacets(true);
		QueryOptionsHandle options = new QueryOptionsHandle().withConstraints(
        		builder.constraint("grandchild",grandchildRange)
        		);

        QueryRange range = options.getConstraint("grandchild").getSource();
        assertEquals(range.getElement(), new QName("grandchild"));

        QueryTransformResults tresults = builder.emptySnippets();
        options.withTransformResults(tresults);
{'fraction_non_alphanumeric': 0.09884912501970676, 'fraction_numerical': 0.005044931420463503, 'mean_word_length': 3.5278372591006426, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4938678', 'n_tokens_mistral': 3442, 'n_tokens_neox': 3306, 'n_words': 751}