public void testPolyQuery() throws Exception {
    CompassSession session = openSession();
    CompassTransaction tr = session.beginTransaction();

    Long id = new Long(1);
    ExtendsA extendsA = new ExtendsA();
    extendsA.setId(id);
    extendsA.setValue("value");
    extendsA.setExtendsValue("evalue");

    session.save("extends", extendsA);

    extendsA = (ExtendsA) session.load("extends", id);
    assertEquals("value", extendsA.getValue());

    id = new Long(1);
    extendsA = new ExtendsA();
    extendsA.setId(id);
    extendsA.setValue("value");
    extendsA.setExtendsValue("evalue");

    session.save("override", extendsA);

    extendsA = (ExtendsA) session.load("override", id);
    assertEquals("value", extendsA.getValue());

    CompassHits hits = session.queryBuilder().polyAlias("base").hits();
    assertEquals(2, hits.length());

    hits = session.find("base");
    assertEquals(2, hits.length());

    tr.commit();
    session.close();
  }
示例#2
0
  public Pager search(Pager pager) {
    Compass compass = compassTemplate.getCompass();
    CompassSession compassSession = compass.openSession();
    CompassQueryBuilder compassQueryBuilder = compassSession.queryBuilder();
    CompassBooleanQueryBuilder compassBooleanQueryBuilder = compassQueryBuilder.bool();

    CompassQuery compassQuery =
        compassBooleanQueryBuilder
            .addMust(compassQueryBuilder.term("isMarketable", true))
            .addMust(compassQueryBuilder.queryString("name:*" + pager.getKeyword() + "*").toQuery())
            .toQuery();

    if (StringUtils.isEmpty(pager.getOrderBy()) || pager.getOrderType() == null) {
      compassQuery
          .addSort("isBest", SortPropertyType.STRING, SortDirection.REVERSE)
          .addSort("isNew", SortPropertyType.STRING, SortDirection.REVERSE)
          .addSort("isHot", SortPropertyType.STRING, SortDirection.REVERSE);
    } else {
      if (pager.getOrderType() == OrderType.asc) {
        if (StringUtils.equalsIgnoreCase(pager.getOrderBy(), "price")) {
          compassQuery.addSort("price", SortPropertyType.FLOAT);
        }
      } else {
        if (StringUtils.equalsIgnoreCase(pager.getOrderBy(), "price")) {
          compassQuery.addSort("price", SortPropertyType.FLOAT, SortDirection.REVERSE);
        }
      }
    }

    CompassHits compassHits = compassQuery.hits();

    List<Product> productList = new ArrayList<Product>();

    int firstResult = (pager.getPageNumber() - 1) * pager.getPageSize();
    int maxReasults = pager.getPageSize();
    int totalCount = compassHits.length();

    int end = Math.min(totalCount, firstResult + maxReasults);
    for (int i = firstResult; i < end; i++) {
      Product product = (Product) compassHits.data(i);
      productList.add(product);
    }
    compassSession.close();
    pager.setList(productList);
    pager.setTotalCount(totalCount);
    return pager;
  }