Package org.springframework.data.solr.core.query

Examples of org.springframework.data.solr.core.query.GroupOptions


  }

  private void processGroupOptions(SolrQuery solrQuery, Query query) {

    GroupOptions groupOptions = query.getGroupOptions();

    if (groupOptions == null
        || (CollectionUtils.isEmpty(groupOptions.getGroupByFields())
            && CollectionUtils.isEmpty(groupOptions.getGroupByFunctions()) && CollectionUtils.isEmpty(groupOptions
            .getGroupByQueries()))) {
      return;
    }

    solrQuery.set(GroupParams.GROUP, true);
    solrQuery.set(GroupParams.GROUP_MAIN, groupOptions.isGroupMain());
    solrQuery.set(GroupParams.GROUP_FORMAT, "grouped");

    if (!CollectionUtils.isEmpty(groupOptions.getGroupByFields())) {
      for (Field field : groupOptions.getGroupByFields()) {
        solrQuery.add(GroupParams.GROUP_FIELD, field.getName());
      }
    }

    if (!CollectionUtils.isEmpty(groupOptions.getGroupByFunctions())) {
      for (Function function : groupOptions.getGroupByFunctions()) {
        String functionFragment = createFunctionFragment(function, 0);
        setObjectNameOnGroupQuery(query, function, functionFragment);
        solrQuery.add(GroupParams.GROUP_FUNC, functionFragment);
      }
    }

    if (!CollectionUtils.isEmpty(groupOptions.getGroupByQueries())) {
      for (Query groupQuery : groupOptions.getGroupByQueries()) {
        String queryFragment = getQueryString(groupQuery);
        setObjectNameOnGroupQuery(query, groupQuery, queryFragment);
        solrQuery.add(GroupParams.GROUP_QUERY, queryFragment);
      }
    }

    if (groupOptions.getSort() != null) {

      for (Order order : groupOptions.getSort()) {
        solrQuery.add(GroupParams.GROUP_SORT, order.getProperty().trim() + " "
            + (order.isAscending() ? ORDER.asc : ORDER.desc));
      }
    }

    if (groupOptions.getCachePercent() > 0) {
      solrQuery.add(GroupParams.GROUP_CACHE_PERCENTAGE, String.valueOf(groupOptions.getCachePercent()));
    }

    if (groupOptions.getLimit() != null && groupOptions.getLimit() >= 0) {
      solrQuery.set(GroupParams.GROUP_LIMIT, groupOptions.getLimit());
    }

    if (groupOptions.getOffset() != null && groupOptions.getOffset() >= 0) {
      solrQuery.set(GroupParams.GROUP_OFFSET, groupOptions.getOffset());
    }

    solrQuery.set(GroupParams.GROUP_TOTAL_COUNT, groupOptions.isTotalCount());
    solrQuery.set(GroupParams.GROUP_FACET, groupOptions.isGroupFacets());
    solrQuery.set(GroupParams.GROUP_TRUNCATE, groupOptions.isTruncateFacets());
  }
View Full Code Here


  public void testNamedObjectsGroupQuery() {
    List<Function> functionList = Arrays.asList(Mockito.mock(Function.class), Mockito.mock(Function.class));
    List<Query> queriesList = Arrays.asList(Mockito.mock(Query.class), Mockito.mock(Query.class));

    Query groupQueryMock = Mockito.mock(Query.class);
    GroupOptions groupOptions = Mockito.mock(GroupOptions.class);
    Mockito.when(groupQueryMock.getGroupOptions()).thenReturn(groupOptions);
    Mockito.when(groupOptions.getGroupByFunctions()).thenReturn(functionList);
    Mockito.when(groupOptions.getGroupByQueries()).thenReturn(queriesList);

    NamedObjectsQuery decorator = new NamedObjectsQuery(groupQueryMock);
    decorator.setName(functionList.get(0), "nameFunc0");
    decorator.setName(functionList.get(1), "nameFunc1");
    decorator.setName(queriesList.get(0), "nameQuery0");
View Full Code Here

    Function f = IfFunction.when("inStock").then("1").otherwise("2");
    Query q1 = new SimpleQuery("cat:category2");
    Query q2 = new SimpleQuery("cat:category1");

    SimpleQuery groupQuery = new SimpleQuery(new SimpleStringCriteria("*:*"));
    GroupOptions groupOptions = new GroupOptions();
    groupQuery.setGroupOptions(groupOptions);
    groupOptions.addSort(new Sort("name", "id"));
    groupOptions.addGroupByField("name");
    groupOptions.addGroupByFunction(f);
    groupOptions.addGroupByQuery(q1);
    groupOptions.addGroupByQuery(q2);
    groupOptions.setLimit(2);

    // asserts result page
    GroupPage<ExampleSolrBean> groupResultPage = solrTemplate.queryForGroupPage(groupQuery, ExampleSolrBean.class);
    GroupResult<ExampleSolrBean> groupResultName = groupResultPage.getGroupResult("name");
    GroupResult<ExampleSolrBean> groupResultFunction = groupResultPage.getGroupResult(f);
View Full Code Here

    solrTemplate.saveBean(new ExampleSolrBean("id_5", "name2", "category2", 2, false));
    solrTemplate.saveBean(new ExampleSolrBean("id_6", "name2", "category1", 1, true));
    solrTemplate.commit();

    SimpleFacetQuery groupQuery = new SimpleFacetQuery(new SimpleStringCriteria("*:*"));
    GroupOptions groupOptions = new GroupOptions();
    groupQuery.setGroupOptions(groupOptions);
    groupQuery.setFacetOptions(new FacetOptions("inStock"));
    groupOptions.addGroupByField("name");
    groupOptions.setGroupFacets(true);
    FacetPage<ExampleSolrBean> groupResultPage = solrTemplate.queryForFacetPage(groupQuery, ExampleSolrBean.class);

    Page<FacetFieldEntry> facetResultPage = groupResultPage.getFacetResultPage("inStock");

    List<FacetFieldEntry> facetContent = facetResultPage.getContent();
View Full Code Here

        documents1_1);
    Mockito.when(groupCommand1.getMatches()).thenReturn(1);
    Mockito.when(groupCommand1.getName()).thenReturn("group1_name");
    Mockito.when(groupCommand1.getNGroups()).thenReturn(2);

    GroupOptions groupOptions = new GroupOptions();
    groupOptions.setLimit(1);

    Mockito.when(query.getPageRequest()).thenReturn(new PageRequest(0, 1));
    Mockito.when(query.getGroupOptions()).thenReturn(groupOptions);

    Object group1Key = new Object();
View Full Code Here

  /**
   * @see DATASOLR-121
   */
  @Test
  public void testConstructGroupQueryWithAllPossibleParameters() {
    GroupOptions groupOptions = new GroupOptions();

    SimpleQuery query = new SimpleQuery();
    query.addCriteria(new SimpleStringCriteria("*:*"));
    query.setGroupOptions(groupOptions);
    groupOptions.setOffset(1);
    groupOptions.setLimit(2);
    groupOptions.addGroupByField("field_1");
    groupOptions.addGroupByFunction(MaxFunction.max("field_1", "field_2"));
    groupOptions.addGroupByQuery(new SimpleQuery("*:*"));
    groupOptions.addSort(new Sort(Sort.Direction.DESC, "field_3"));
    groupOptions.setTotalCount(true);

    SolrQuery solrQuery = queryParser.constructSolrQuery(query);

    assertGroupFormatPresent(solrQuery, true);
    Assert.assertEquals("field_1", solrQuery.get(GroupParams.GROUP_FIELD));
View Full Code Here

   */
  @Test
  public void testConstructGroupQueryWithoutPagingParameters() {
    SimpleQuery query = new SimpleQuery();
    query.addCriteria(new SimpleStringCriteria("*:*"));
    query.setGroupOptions(new GroupOptions().addGroupByField("fieldName"));

    SolrQuery solrQuery = queryParser.constructSolrQuery(query);

    assertGroupFormatPresent(solrQuery, false);
    Assert.assertNull(solrQuery.get(GroupParams.GROUP_SORT));
View Full Code Here

   */
  @Test
  public void testConstructGroupQueryWithMultipleFunctions() {
    SimpleQuery query = new SimpleQuery();
    query.addCriteria(new SimpleStringCriteria("*:*"));
    query.setGroupOptions(new GroupOptions());
    query.getGroupOptions().addGroupByFunction(MaxFunction.max("field_1", "field_2"));
    query.getGroupOptions().addGroupByFunction(MaxFunction.max("field_3", "field_4"));

    SolrQuery solrQuery = queryParser.constructSolrQuery(query);

View Full Code Here

   */
  @Test
  public void testConstructGroupQueryWithMultipleQueries() {
    SimpleQuery query = new SimpleQuery();
    query.addCriteria(new SimpleStringCriteria("*:*"));
    query.setGroupOptions(new GroupOptions());
    query.getGroupOptions().addGroupByQuery(new SimpleQuery("query1"));
    query.getGroupOptions().addGroupByQuery(new SimpleQuery("query2"));

    SolrQuery solrQuery = queryParser.constructSolrQuery(query);

View Full Code Here

        documents1_1);
    Mockito.when(groupCommand1.getMatches()).thenReturn(1);
    Mockito.when(groupCommand1.getName()).thenReturn("group1_name");
    Mockito.when(groupCommand1.getNGroups()).thenReturn(null);

    GroupOptions groupOptions = new GroupOptions();
    groupOptions.setLimit(1);

    Mockito.when(query.getPageRequest()).thenReturn(new PageRequest(0, 1));
    Mockito.when(query.getGroupOptions()).thenReturn(groupOptions);

    Object group1Key = new Object();
View Full Code Here

TOP

Related Classes of org.springframework.data.solr.core.query.GroupOptions

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.