Search in sources :

Example 11 with SearchContext

use of org.alfresco.rest.api.search.context.SearchContext in project alfresco-remote-api by Alfresco.

the class ResultMapperTests method facetFormatTest.

@Test
public /**
 * Validates that when facetFormat is specified then all facets are returned
 * in the generic facet response format AKA V2.
 */
void facetFormatTest() throws Exception {
    String jsonQuery = "{\"query\": {\"query\": \"alfresco\"}," + "\"facetQueries\": [" + "{\"query\": \"content.size:[0 TO 102400]\", \"label\": \"small\"}," + "{\"query\": \"content.size:[102400 TO 1048576]\", \"label\": \"medium\"}," + "{\"query\": \"content.size:[1048576 TO 16777216]\", \"label\": \"large\"}]" + ",\"facetFormat\":\"V2\"" + "}";
    String expectedResponse = "{\"responseHeader\":{\"status\":0,\"QTime\":9},\"_original_parameters_\":\"org.apache.solr.common.params.DefaultSolrParams:{params(df=TEXT&alternativeDic=DEFAULT_DICTIONARY&fl=DBID,score&start=0&fq={!afts}AUTHORITY_FILTER_FROM_JSON&fq={!afts}TENANT_FILTER_FROM_JSON&rows=1000&locale=en_US&wt=json),defaults(carrot.url=id&spellcheck.collateExtendedResults=true&carrot.produceSummary=true&spellcheck.maxCollations=3&spellcheck.maxCollationTries=5&spellcheck.alternativeTermCount=2&spellcheck.extendedResults=false&defType=afts&spellcheck.maxResultsForSuggest=5&spellcheck=false&carrot.outputSubClusters=false&spellcheck.count=5&carrot.title=mltext@m___t@{http://www.alfresco.org/model/content/1.0}title&carrot.snippet=content@s___t@{http://www.alfresco.org/model/content/1.0}content&spellcheck.collate=true)}\",\"_field_mappings_\":{},\"_date_mappings_\":{},\"_range_mappings_\":{},\"_pivot_mappings_\":{},\"_interval_mappings_\":{},\"_stats_field_mappings_\":{},\"_stats_facet_mappings_\":{},\"_facet_function_mappings_\":{},\"response\":{\"numFound\":6,\"start\":0,\"maxScore\":0.7849362,\"docs\":[{\"DBID\":565,\"score\":0.7849362},{\"DBID\":566,\"score\":0.7849362},{\"DBID\":521,\"score\":0.3540957},{\"DBID\":514,\"score\":0.33025497},{\"DBID\":420,\"score\":0.32440513},{\"DBID\":415,\"score\":0.2780319}]}," + "\"spellcheck\":{\"searchInsteadFor\":\"alfresco\"}," + "\"facet_counts\":{\"facet_queries\": {\"small\": 52,\"large\": 0,\"medium\": 0}}," + "\"processedDenies\":true, \"lastIndexedTx\":34}";
    ResultSet results = mockResultset(expectedResponse);
    SearchQuery searchQuery = helper.extractFromJson(jsonQuery);
    SearchRequestContext searchRequest = SearchRequestContext.from(searchQuery);
    SearchContext searchContext = mapper.toSearchContext((SolrJSONResultSet) results, searchRequest, searchQuery, 0);
    assertEquals(34l, searchContext.getConsistency().getlastTxId());
    assertEquals(null, searchContext.getFacetQueries());
    assertEquals(1, searchContext.getFacets().size());
    assertEquals(3, searchContext.getFacets().get(0).getBuckets().size());
    assertEquals("small", searchContext.getFacets().get(0).getBuckets().get(0).getLabel());
    assertEquals("content.size:[0 TO 102400]", searchContext.getFacets().get(0).getBuckets().get(0).getFilterQuery());
    assertFalse(searchContext.getFacets().get(0).getBuckets().get(0).getMetrics().isEmpty());
    Metric[] metrics = searchContext.getFacets().get(0).getBuckets().get(0).getMetrics().toArray(new Metric[searchContext.getFacets().get(0).getBuckets().get(0).getMetrics().size()]);
    assertEquals(METRIC_TYPE.count, metrics[0].getType());
    assertEquals("{count=52}", metrics[0].getValue().toString());
    jsonQuery = jsonQuery.replace("V2", "V1");
    searchQuery = helper.extractFromJson(jsonQuery);
    results = mockResultset(expectedResponse);
    searchRequest = SearchRequestContext.from(searchQuery);
    searchContext = mapper.toSearchContext((SolrJSONResultSet) results, searchRequest, searchQuery, 0);
    assertEquals(34l, searchContext.getConsistency().getlastTxId());
    assertEquals(3, searchContext.getFacetQueries().size());
    assertEquals("small", searchContext.getFacetQueries().get(0).getLabel());
    assertEquals("content.size:[0 TO 102400]", searchContext.getFacetQueries().get(0).getFilterQuery());
    assertEquals(52, searchContext.getFacetQueries().get(0).getCount());
    assertEquals("large", searchContext.getFacetQueries().get(1).getLabel());
    // FacetField Test
    jsonQuery = "{\"query\": {\"query\": \"A*\"}," + "\"facetFields\": {\"facets\": [" + "{\"field\": \"creator\", \"mincount\": 1}," + "{\"field\": \"modifier\", \"mincount\": 1}]}," + "\"facetFormat\":\"V1\"}";
    expectedResponse = "{\"responseHeader\":{\"status\":0,\"QTime\":9},\"_original_parameters_\":\"org.apache.solr.common.params.DefaultSolrParams:{params(df=TEXT&alternativeDic=DEFAULT_DICTIONARY&fl=DBID,score&start=0&fq={!afts}AUTHORITY_FILTER_FROM_JSON&fq={!afts}TENANT_FILTER_FROM_JSON&rows=1000&locale=en_US&wt=json),defaults(carrot.url=id&spellcheck.collateExtendedResults=true&carrot.produceSummary=true&spellcheck.maxCollations=3&spellcheck.maxCollationTries=5&spellcheck.alternativeTermCount=2&spellcheck.extendedResults=false&defType=afts&spellcheck.maxResultsForSuggest=5&spellcheck=false&carrot.outputSubClusters=false&spellcheck.count=5&carrot.title=mltext@m___t@{http://www.alfresco.org/model/content/1.0}title&carrot.snippet=content@s___t@{http://www.alfresco.org/model/content/1.0}content&spellcheck.collate=true)}\",\"_field_mappings_\":{},\"_date_mappings_\":{},\"_range_mappings_\":{},\"_pivot_mappings_\":{},\"_interval_mappings_\":{},\"_stats_field_mappings_\":{},\"_stats_facet_mappings_\":{},\"_facet_function_mappings_\":{},\"response\":{\"numFound\":6,\"start\":0,\"maxScore\":0.7849362,\"docs\":[{\"DBID\":565,\"score\":0.7849362},{\"DBID\":566,\"score\":0.7849362},{\"DBID\":521,\"score\":0.3540957},{\"DBID\":514,\"score\":0.33025497},{\"DBID\":420,\"score\":0.32440513},{\"DBID\":415,\"score\":0.2780319}]}," + "\"spellcheck\":{\"searchInsteadFor\":\"alfresco\"}," + "\"facet_counts\":{\"facet_fields\":{\"creator\":[\"System\",124,\"mjackson\",11,\"abeecher\",4],\"modifier\":[\"System\",124,\"mjackson\",8,\"admin\",7]}}," + "\"processedDenies\":true, \"lastIndexedTx\":34}";
    results = mockResultset(expectedResponse);
    searchQuery = helper.extractFromJson(jsonQuery);
    searchContext = mapper.toSearchContext((SolrJSONResultSet) results, searchRequest, searchQuery, 0);
    assertFalse(searchContext.getFacetsFields().isEmpty());
    assertTrue(searchContext.getFacets().isEmpty());
    assertEquals("creator", searchContext.getFacetsFields().get(0).getLabel());
    assertEquals(3, searchContext.getFacetsFields().get(0).getBuckets().size());
    assertEquals(124, searchContext.getFacetsFields().get(0).getBuckets().get(0).getCount());
    assertEquals("creator:\"System\"", searchContext.getFacetsFields().get(0).getBuckets().get(0).getFilterQuery());
    assertEquals("System", searchContext.getFacetsFields().get(0).getBuckets().get(0).getLabel());
    assertEquals("modifier", searchContext.getFacetsFields().get(1).getLabel());
    jsonQuery = jsonQuery.replace("V1", "V2");
    searchQuery = helper.extractFromJson(jsonQuery);
    searchContext = mapper.toSearchContext((SolrJSONResultSet) results, searchRequest, searchQuery, 0);
    assertTrue(searchContext.getFacetsFields().isEmpty());
    assertFalse(searchContext.getFacets().isEmpty());
    assertEquals("creator", searchContext.getFacets().get(0).getLabel());
    assertEquals(3, searchContext.getFacets().get(0).getBuckets().size());
    metrics = searchContext.getFacets().get(0).getBuckets().get(0).getMetrics().toArray(new Metric[searchContext.getFacets().get(0).getBuckets().get(0).getMetrics().size()]);
    assertEquals("{count=124}", metrics[0].getValue().toString());
    assertEquals("creator:\"System\"", searchContext.getFacets().get(0).getBuckets().get(0).getFilterQuery());
    assertEquals("System", searchContext.getFacets().get(0).getBuckets().get(0).getLabel());
    assertEquals("modifier", searchContext.getFacets().get(1).getLabel());
}
Also used : SearchQuery(org.alfresco.rest.api.search.model.SearchQuery) ResultSet(org.alfresco.service.cmr.search.ResultSet) EmptyResultSet(org.alfresco.repo.search.EmptyResultSet) SolrJSONResultSet(org.alfresco.repo.search.impl.lucene.SolrJSONResultSet) SearchContext(org.alfresco.rest.api.search.context.SearchContext) PercentileMetric(org.alfresco.repo.search.impl.solr.facet.facetsresponse.PercentileMetric) Metric(org.alfresco.repo.search.impl.solr.facet.facetsresponse.Metric) SimpleMetric(org.alfresco.repo.search.impl.solr.facet.facetsresponse.SimpleMetric) ListMetric(org.alfresco.repo.search.impl.solr.facet.facetsresponse.ListMetric) Matchers.anyString(org.mockito.Matchers.anyString) SolrJSONResultSet(org.alfresco.repo.search.impl.lucene.SolrJSONResultSet) SearchRequestContext(org.alfresco.rest.api.search.context.SearchRequestContext) Test(org.junit.Test)

Example 12 with SearchContext

use of org.alfresco.rest.api.search.context.SearchContext in project alfresco-remote-api by Alfresco.

the class ResultMapperTests method testFacetingWithPartialGroup.

@Test
public /**
 *  When the following is passed
 * "facetQueries": [
 *       {"query": "content.size:[0 TO 102400]", "label": "small", "group": "one"},
 *       {"query": "content.size:[102400 TO 1048576]", "label": "medium", "group": "two"},
 *       {"query": "content.size:[1048576 TO 16777216]", "label": "large"}
 *  We expect to see 3 groups of 1,2,null.
 * @throws Exception
 */
void testFacetingWithPartialGroup() throws Exception {
    String jsonQuery = "{\"query\": {\"query\": \"alfresco\"}," + "\"facetQueries\": [" + "{\"query\": \"content.size:[o TO 102400]\", \"label\": \"small\",\"group\":\"1\"}," + "{\"query\": \"content.size:[102400 TO 1048576]\", \"label\": \"medium\",\"group\":\"2\"}," + "{\"query\": \"content.size:[1048576 TO 16777216]\", \"label\": \"large\"}]" + "}";
    String expectedResponse = "{\"responseHeader\":{\"status\":0,\"QTime\":9},\"_original_parameters_\":\"org.apache.solr.common.params.DefaultSolrParams:{params(df=TEXT&alternativeDic=DEFAULT_DICTIONARY&fl=DBID,score&start=0&fq={!afts}AUTHORITY_FILTER_FROM_JSON&fq={!afts}TENANT_FILTER_FROM_JSON&rows=1000&locale=en_US&wt=json),defaults(carrot.url=id&spellcheck.collateExtendedResults=true&carrot.produceSummary=true&spellcheck.maxCollations=3&spellcheck.maxCollationTries=5&spellcheck.alternativeTermCount=2&spellcheck.extendedResults=false&defType=afts&spellcheck.maxResultsForSuggest=5&spellcheck=false&carrot.outputSubClusters=false&spellcheck.count=5&carrot.title=mltext@m___t@{http://www.alfresco.org/model/content/1.0}title&carrot.snippet=content@s___t@{http://www.alfresco.org/model/content/1.0}content&spellcheck.collate=true)}\",\"_field_mappings_\":{},\"_date_mappings_\":{},\"_range_mappings_\":{},\"_pivot_mappings_\":{},\"_interval_mappings_\":{},\"_stats_field_mappings_\":{},\"_stats_facet_mappings_\":{},\"_facet_function_mappings_\":{},\"response\":{\"numFound\":6,\"start\":0,\"maxScore\":0.7849362,\"docs\":[{\"DBID\":565,\"score\":0.7849362},{\"DBID\":566,\"score\":0.7849362},{\"DBID\":521,\"score\":0.3540957},{\"DBID\":514,\"score\":0.33025497},{\"DBID\":420,\"score\":0.32440513},{\"DBID\":415,\"score\":0.2780319}]}," + "\"spellcheck\":{\"searchInsteadFor\":\"alfresco\"}," + "\"facet_counts\":{\"facet_queries\": {\"small\": 52,\"large\": 0,\"medium\": 0}}," + "\"processedDenies\":true, \"lastIndexedTx\":34}";
    ResultSet results = mockResultset(expectedResponse);
    SearchQuery searchQuery = helper.extractFromJson(jsonQuery);
    SearchRequestContext searchRequest = SearchRequestContext.from(searchQuery);
    SearchContext searchContext = mapper.toSearchContext((SolrJSONResultSet) results, searchRequest, searchQuery, 0);
    assertEquals(34l, searchContext.getConsistency().getlastTxId());
    assertEquals(null, searchContext.getFacetQueries());
    assertEquals(3, searchContext.getFacets().size());
    assertEquals(1, searchContext.getFacets().get(0).getBuckets().size());
    assertEquals(1, searchContext.getFacets().get(1).getBuckets().size());
    assertEquals(1, searchContext.getFacets().get(2).getBuckets().size());
    assertEquals("large", searchContext.getFacets().get(0).getBuckets().get(0).getLabel());
    assertEquals("small", searchContext.getFacets().get(1).getBuckets().get(0).getLabel());
    assertEquals("medium", searchContext.getFacets().get(2).getBuckets().get(0).getLabel());
    assertEquals("content.size:[o TO 102400]", searchContext.getFacets().get(1).getBuckets().get(0).getFilterQuery());
    assertFalse(searchContext.getFacets().get(0).getBuckets().get(0).getMetrics().isEmpty());
    Metric[] metrics = searchContext.getFacets().get(1).getBuckets().get(0).getMetrics().toArray(new Metric[searchContext.getFacets().get(0).getBuckets().get(0).getMetrics().size()]);
    assertEquals(METRIC_TYPE.count, metrics[0].getType());
    assertEquals("{count=52}", metrics[0].getValue().toString());
}
Also used : SearchQuery(org.alfresco.rest.api.search.model.SearchQuery) ResultSet(org.alfresco.service.cmr.search.ResultSet) EmptyResultSet(org.alfresco.repo.search.EmptyResultSet) SolrJSONResultSet(org.alfresco.repo.search.impl.lucene.SolrJSONResultSet) SearchContext(org.alfresco.rest.api.search.context.SearchContext) PercentileMetric(org.alfresco.repo.search.impl.solr.facet.facetsresponse.PercentileMetric) Metric(org.alfresco.repo.search.impl.solr.facet.facetsresponse.Metric) SimpleMetric(org.alfresco.repo.search.impl.solr.facet.facetsresponse.SimpleMetric) ListMetric(org.alfresco.repo.search.impl.solr.facet.facetsresponse.ListMetric) Matchers.anyString(org.mockito.Matchers.anyString) SearchRequestContext(org.alfresco.rest.api.search.context.SearchRequestContext) Test(org.junit.Test)

Example 13 with SearchContext

use of org.alfresco.rest.api.search.context.SearchContext in project alfresco-remote-api by Alfresco.

the class SearchQuerySerializerTests method testSerializeContext.

@Test
public void testSerializeContext() throws IOException {
    ExecutionResult exec1 = new ExecutionResult(new Farmer("180"), null);
    FacetFieldContext ffc = new FacetFieldContext("theLabel", Arrays.asList(new Bucket("b1", "name:b1", 23, "displayText1"), new Bucket("b2", null, 34, "displayText2")));
    SearchContext searchContext = new SearchContext(23l, null, Arrays.asList(new FacetQueryContext("f1", "creator:bob", 15), new FacetQueryContext("f2", null, 20)), Arrays.asList(ffc), new SpellCheckContext("aFlag", Arrays.asList("bish", "bash")), null);
    CollectionWithPagingInfo<ExecutionResult> coll = CollectionWithPagingInfo.asPaged(null, Arrays.asList(exec1), false, 2, null, searchContext);
    String out = helper.writeResponse(coll);
    assertTrue("There must 'context' json output", out.contains("\"context\":{\"consistency\":{\"lastTxId\":23}"));
    assertTrue("There must 'facetQueries' json output", out.contains("\"facetQueries\":"));
    assertTrue("There must 'facetQueries f1' json output", out.contains("{\"label\":\"f1\",\"filterQuery\":\"creator:bob\",\"count\":15}"));
    assertTrue("There must 'facetQueries f2' json output", out.contains("{\"label\":\"f2\",\"count\":20}"));
    assertTrue("There must 'spellCheck' json output", out.contains("\"spellCheck\":{\"type\":\"aFlag\",\"suggestions\":[\"bish\",\"bash\"]}"));
    assertTrue("There must 'facetsFields' json output", out.contains("\"facetsFields\":[{\"label\":\"theLabel\",\"buckets\""));
    assertTrue("There must 'bucket1' json output", out.contains("{\"label\":\"b1\",\"filterQuery\":\"name:b1\",\"count\":23,\"display\":\"displayText1\"}"));
    assertTrue("There must 'bucket2' json output", out.contains("{\"label\":\"b2\",\"count\":34,\"display\":\"displayText2\"}"));
    searchContext = new SearchContext(-1, null, null, null, null, null);
    coll = CollectionWithPagingInfo.asPaged(null, Arrays.asList(exec1), false, 2, null, searchContext);
    out = helper.writeResponse(coll);
    assertTrue("There must NOT BE a 'context' json output", out.contains("\"context\":{}"));
    assertFalse("There must NOT BE a 'facetsFields' json output", out.contains("\"facetsFields\":{}"));
}
Also used : FacetFieldContext(org.alfresco.rest.api.search.context.FacetFieldContext) FacetQueryContext(org.alfresco.rest.api.search.context.FacetQueryContext) Bucket(org.alfresco.rest.api.search.context.FacetFieldContext.Bucket) SearchContext(org.alfresco.rest.api.search.context.SearchContext) ExecutionResult(org.alfresco.rest.framework.jacksonextensions.ExecutionResult) Farmer(org.alfresco.rest.framework.tests.api.mocks.Farmer) SpellCheckContext(org.alfresco.rest.api.search.context.SpellCheckContext) Test(org.junit.Test)

Aggregations

SearchContext (org.alfresco.rest.api.search.context.SearchContext)13 SolrJSONResultSet (org.alfresco.repo.search.impl.lucene.SolrJSONResultSet)11 Test (org.junit.Test)11 SearchRequestContext (org.alfresco.rest.api.search.context.SearchRequestContext)10 SearchQuery (org.alfresco.rest.api.search.model.SearchQuery)10 ResultSet (org.alfresco.service.cmr.search.ResultSet)10 EmptyResultSet (org.alfresco.repo.search.EmptyResultSet)9 Metric (org.alfresco.repo.search.impl.solr.facet.facetsresponse.Metric)9 SimpleMetric (org.alfresco.repo.search.impl.solr.facet.facetsresponse.SimpleMetric)9 Matchers.anyString (org.mockito.Matchers.anyString)9 ListMetric (org.alfresco.repo.search.impl.solr.facet.facetsresponse.ListMetric)8 PercentileMetric (org.alfresco.repo.search.impl.solr.facet.facetsresponse.PercentileMetric)8 FacetFieldContext (org.alfresco.rest.api.search.context.FacetFieldContext)7 FacetQueryContext (org.alfresco.rest.api.search.context.FacetQueryContext)7 SpellCheckContext (org.alfresco.rest.api.search.context.SpellCheckContext)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 List (java.util.List)6 GenericFacetResponse (org.alfresco.repo.search.impl.solr.facet.facetsresponse.GenericFacetResponse)6 Serializable (java.io.Serializable)5