Example usage for org.apache.solr.client.solrj SolrQuery SolrQuery

List of usage examples for org.apache.solr.client.solrj SolrQuery SolrQuery

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj SolrQuery SolrQuery.

Prototype

public SolrQuery(String q) 

Source Link

Document

Create a new SolrQuery

Usage

From source file:de.hybris.platform.solrfacetsearch.integration.IndexFullProductTest.java

License:Open Source License

@Test
public void verifyManufacturername() throws SolrServerException {
    final List<ProductModel> productModels = searchResult.getResult();
    for (final ProductModel productModel : productModels) {
        final String manufacturerName = productModel.getManufacturerName();
        final String solrDocId = prepareProductSolrId(productModel);
        final QueryResponse solrResponse = solrServer.query(new SolrQuery(
                "manufacturerName_string:\"" + manufacturerName + "\" AND id:" + "\"" + solrDocId + "\""));
        final SolrDocumentList solrDocumentList = solrResponse.getResults();
        assertNotNull(solrDocumentList);
        assertEquals("Manufacturer name  for document with ID " + solrDocId + " is not found!", 1,
                solrDocumentList.size());
    }// www .  ja v  a2  s. com
}

From source file:de.hybris.platform.solrfacetsearch.integration.IndexFullProductTest.java

License:Open Source License

@Test
public void verifyProcessorVariantAttribute() throws SolrServerException {
    final List<ProductModel> productModels = searchResult.getResult();
    for (final ProductModel productModel : productModels) {
        final FeatureContainer cont = FeatureContainer.load((Product) modelService.getSource(productModel));
        if (!cont.hasFeature("processor")) {
            continue;
        }//from  w  w  w.j a  va2  s. c  o m
        final Feature feature = cont.getFeature("processor");
        if (feature == null || feature.isEmpty()) {
            continue;
        }
        final List<FeatureValue> fieldValues = feature.getValues();
        for (final FeatureValue fieldValue : fieldValues) {
            final String solrDocId = prepareProductSolrId(productModel);
            final String solrQuery = "processor_string_mv:\"" + fieldValue.getValue() + "\" AND id:\""
                    + solrDocId + "\"";
            final QueryResponse solrResponse = solrServer.query(new SolrQuery(solrQuery));
            final SolrDocumentList solrDocumentList = solrResponse.getResults();
            assertNotNull(solrDocumentList);
            assertEquals("Processor classification property " + solrQuery + " for document with ID " + solrDocId
                    + " is not found!", 1, solrDocumentList.size());
        }
    }
}

From source file:de.hybris.platform.solrfacetsearch.integration.IndexFullProductTest.java

License:Open Source License

private void checkCategoryForProductInSolr(final String solrAttributeName, final ProductModel productModel,
        final String categoryName) throws SolrServerException {
    final String solrDocId = prepareProductSolrId(productModel);
    final QueryResponse solrResponse = solrServer
            .query(new SolrQuery(solrAttributeName + ":\"" + categoryName + "\" AND id:\"" + solrDocId + "\""));
    final SolrDocumentList solrDocumentList = solrResponse.getResults();
    assertNotNull(solrDocumentList);//  w  ww .  j  a v a2 s.co  m
    assertTrue("Category name " + categoryName + " is not found for " + solrDocId + "!",
            (1 == solrDocumentList.size()));
}

From source file:de.hybris.platform.solrfacetsearch.integration.IndexFullProductTest.java

License:Open Source License

private void checkTextAttributeForLanguage(final Locale enLocale, final String solrPropertyName,
        final KindOfTextAttribute kindOfTextAttribute) throws SolrServerException {
    final List<ProductModel> productModels = searchResult.getResult();
    for (final ProductModel productModel : productModels) {
        String localisedProductvalue = null;
        switch (kindOfTextAttribute) {
        case NAME:
            localisedProductvalue = productModel.getName(enLocale);
            break;
        case DESCRIPTION:
            localisedProductvalue = productModel.getDescription(enLocale);
            break;
        default://from   w  w  w .jav a  2  s .c o m
            throw new UnsupportedOperationException("Uknown name of indexed text attribute.");
        }
        if (StringUtils.isEmpty(localisedProductvalue)) {
            continue;
        }
        final String solrDocumentId = prepareProductSolrId(productModel);
        localisedProductvalue = localisedProductvalue.replaceAll("\"", "\\\\\"");
        final String queryStr = solrPropertyName + ":\"" + localisedProductvalue + "\" AND id:" + "\""
                + solrDocumentId + "\"";
        final QueryResponse solrResponse = solrServer.query(new SolrQuery(queryStr));
        final SolrDocumentList solrDocumentList = solrResponse.getResults();
        assertNotNull(solrDocumentList);
        assertEquals("Document with ID " + solrDocumentId + " is not found or umbigous result!", 1,
                solrDocumentList.size());
    }
}

From source file:de.hybris.platform.solrfacetsearch.integration.IndexFullProductTest.java

License:Open Source License

@Test
public void testVerifyNumberOfIndexedRecords() throws Exception {
    final int numberOfProductsToIndex = searchResult.getTotalCount();

    LOG.info("Number of products to index = " + numberOfProductsToIndex);

    final QueryResponse solrResponse = solrServer.query(new SolrQuery("*:*"));
    final SolrDocumentList solrDocumentList = solrResponse.getResults();
    assertNotNull(solrDocumentList);//from   w w  w  .j  a v  a  2 s  .  c om
    LOG.info("Number of indexed documents = " + solrDocumentList.getNumFound());
    assertEquals(numberOfProductsToIndex, solrDocumentList.getNumFound());
}

From source file:de.hybris.platform.solrfacetsearch.integration.IndexFullProductTest.java

License:Open Source License

private void checkPriceForAllProducts(final String currency) throws SolrServerException {
    final List<ProductModel> productModels = searchResult.getResult();
    final CurrencyModel currencyModel = commonI18NService.getCurrency(currency);
    commonI18NService.setCurrentCurrency(currencyModel);
    for (final ProductModel productModel : productModels) {
        final String solrDocId = prepareProductSolrId(productModel);
        final List<PriceInformation> prices = priceService.getPriceInformationsForProduct(productModel);
        assertNotNull(prices);/*  w  w  w  .  j  a  v  a  2s .  c o m*/
        assertFalse(prices.isEmpty());
        final Double value = Double.valueOf(prices.get(0).getPriceValue().getValue());
        if (value != null && value.doubleValue() > 0) {
            final String rangeStr = getPriceRangeForValue(value.doubleValue());
            final IndexedProperty priceProperty = indexedType.getIndexedProperties().get("price");
            final String priceField = fieldNameProvider.getFieldName(priceProperty, currency, FieldType.INDEX);
            final String solrQuery = priceField + ":\"" + rangeStr + "\" AND id:\"" + solrDocId + "\"";
            final QueryResponse solrResponse = solrServer.query(new SolrQuery(solrQuery));
            final SolrDocumentList solrDocumentList = solrResponse.getResults();
            assertNotNull(solrDocumentList);
            assertEquals("Price range " + rangeStr + " was not find for currency " + currency + ", solrDocId: "
                    + solrDocId, 1, solrDocumentList.getNumFound());
        }
    }
}

From source file:de.hybris.platform.solrfacetsearch.integration.IndexUpdateDeleteProductTest.java

License:Open Source License

@Test
public void testUpdateNewProduct() throws Exception {
    try {/*  ww w  .  jav a 2  s  . c  o  m*/
        final String productId = prepareProductSolrId(CATALOG_ID, VERSION_ONLINE, NEW_TEST_PRODUCT_CODE);
        final String solrQuery = "id:\"" + productId + "\"";
        QueryResponse solrResponse = solrServer.query(new SolrQuery(solrQuery));
        assertEquals("Failed test data. New product " + productId + " already exist in indexer!", 0,
                solrResponse.getResults().getNumFound());
        Thread.sleep(1100);
        final ProductModel newProduct = createNewProduct(hwOnline);

        modelService.saveAll(Arrays.asList(pricerow, unit, newProduct));

        final ProductModel testProduct = productService.getProductForCode(NEW_TEST_PRODUCT_CODE);
        assertNotNull("New test product can not be created.", testProduct);
        modelService.refresh(testProduct);
        assertEquals(testProduct.getCode(), NEW_TEST_PRODUCT_CODE);

        Date indexTime = (Date) solrServer.getLukeSatatisticsValue("lastModified");
        final Date productDate = testProduct.getModifiedtime();
        final DateFormat df = SimpleDateFormat.getTimeInstance(SimpleDateFormat.FULL);
        LOG.info(NEW_TEST_PRODUCT_CODE + " modify time : " + df.format(productDate) + " milis : "
                + productDate.getTime());
        LOG.info("Solr last index time : " + df.format(indexTime) + " milis : " + indexTime.getTime());
        assertTrue("LastIndexTime is not before new product modification time", indexTime.before(productDate));
        Thread.sleep(1100);
        indexerService.updateIndex(facetSearchConfig);
        solrServer.commit(true, true);
        indexTime = (Date) solrServer.getLukeSatatisticsValue("lastModified");

        LOG.info(NEW_TEST_PRODUCT_CODE + " modify time : " + df.format(productDate) + " milis : "
                + productDate.getTime());
        LOG.info("Solr last index time : " + df.format(indexTime) + " milis : " + indexTime.getTime());
        assertTrue("LastIndexTime is not after new product modification time", indexTime.after(productDate));

        solrResponse = solrServer.query(new SolrQuery(solrQuery));
        assertEquals("New product " + productId + " was not transfered to indexer!", 1,
                solrResponse.getResults().getNumFound());
    } finally {
        final ProductModel product = productService.getProductForCode(NEW_TEST_PRODUCT_CODE);
        modelService.remove(product);
    }
}

From source file:de.hybris.platform.solrfacetsearch.integration.IndexUpdateDeleteProductTest.java

License:Open Source License

@Test
public void testUpdateExistingProduct() throws Exception {
    final ProductModel testProduct = productService.getProductForCode(hwOnline, EXISTING_TEST_PRODUCT_CODE);
    assertNotNull("Failed test data! Product with code " + EXISTING_TEST_PRODUCT_CODE + " does not exist.",
            testProduct);/*w  w  w  .  j av a  2  s  .c o  m*/
    final String productName = testProduct.getName(enLocale);
    final String description = testProduct.getDescription(enLocale);

    final String productId = prepareProductSolrId(testProduct);
    String solrQuery = "id:\"" + productId + "\" AND name_text_en:\"" + productName
            + "\" AND description_text_en:\"" + description + "\"";
    QueryResponse solrResponse = solrServer.query(new SolrQuery(solrQuery));
    assertEquals("Test product was not indexed. Missed solr document " + productId, 1,
            solrResponse.getResults().getNumFound());

    Thread.sleep(1100);

    testProduct.setName(NEW_PRODUCT_NAME, enLocale);
    testProduct.setDescription(NEW_PRODUCT_EN_DESCRIPTION, enLocale);
    modelService.save(testProduct);
    modelService.refresh(testProduct);

    Date indexTime = (Date) solrServer.getLukeSatatisticsValue("lastModified");
    final Date productDate = testProduct.getModifiedtime();

    final DateFormat df = SimpleDateFormat.getTimeInstance(SimpleDateFormat.FULL);
    LOG.info(NEW_TEST_PRODUCT_CODE + " modify time : " + df.format(productDate) + " milis : "
            + productDate.getTime());
    LOG.info("Solr last index time : " + df.format(indexTime) + " milis : " + indexTime.getTime());

    assertTrue("LastIndexTime is not before new product modification time", indexTime.before(productDate));

    Thread.sleep(1100);

    indexerService.updateIndex(getIndexConfig());
    solrServer.commit(true, true);

    indexTime = (Date) solrServer.getLukeSatatisticsValue("lastModified");
    LOG.info(NEW_TEST_PRODUCT_CODE + " modify time : " + df.format(productDate) + " milis : "
            + productDate.getTime());
    LOG.info("Solr last index time : " + df.format(indexTime) + " milis : " + indexTime.getTime());
    assertTrue("LastIndexTime is not after new product modification time", indexTime.after(productDate));

    solrResponse = solrServer.query(new SolrQuery(solrQuery));
    assertEquals("Test product is still in old version in the indexer. Product id: " + productId, 0,
            solrResponse.getResults().getNumFound());

    solrQuery = "id:\"" + productId + "\" AND name_text_en:\"" + NEW_PRODUCT_NAME
            + "\" AND description_text_en:\"" + NEW_PRODUCT_EN_DESCRIPTION + "\"";
    solrResponse = solrServer.query(new SolrQuery(solrQuery));
    assertEquals("Changed test product was not indexed. Missed solr document " + productId, 1,
            solrResponse.getResults().getNumFound());

}

From source file:de.hybris.platform.solrfacetsearch.integration.IndexUpdateDeleteProductTest.java

License:Open Source License

@Test
public void testDeleteProduct() throws Exception {
    final ProductModel testProduct = productService.getProductForCode(hwOnline, EXISTING_TEST_PRODUCT_CODE);
    assertNotNull("Failed test data! Product with code " + EXISTING_TEST_PRODUCT_CODE + " does not exist.",
            testProduct);//from   w  w  w. ja  va 2  s  . com
    final String productId = prepareProductSolrId(testProduct);
    final String solrQuery = "id:\"" + productId + "\"";
    QueryResponse solrResponse = solrServer.query(new SolrQuery(solrQuery));
    assertEquals("Test product was not indexed. Missed solr document " + productId, 1,
            solrResponse.getResults().getNumFound());

    indexerService.deleteFromIndex(getIndexConfig());

    solrResponse = solrServer.query(new SolrQuery(solrQuery));
    assertEquals("Product with soldId = " + productId + "wasn't properly deleted from indexer.", 0,
            solrResponse.getResults().getNumFound());
}

From source file:de.hybris.platform.solrfacetsearch.solr.SolrSchemaTest.java

License:Open Source License

@Test
public void testDynamicInt() throws Exception {
    final String dynamicField = "dynamic_int";
    final String dynamicFieldMultiValued = "dynamic_int_mv";
    final SolrServer solrServer = getSolrServer();
    final String id = UUID.randomUUID().toString();
    try {//from   www  . ja v a2  s  .  c  o  m
        final SolrInputDocument document = new SolrInputDocument();
        document.addField("id", id);
        document.addField(dynamicField, Integer.valueOf(1));
        document.addField(dynamicFieldMultiValued, Integer.valueOf(3));
        document.addField(dynamicFieldMultiValued, Integer.valueOf(4));
        solrServer.add(document);
        solrServer.commit();

        // Perform the query, getting the response, and validating the results
        final QueryResponse response = solrServer.query(new SolrQuery("id:" + id));
        Assert.assertNotNull(response);
        final SolrDocumentList documents = response.getResults();
        Assert.assertNotNull(documents);
        Assert.assertEquals(1, documents.size());
        final SolrDocument resultDocument = documents.iterator().next();
        Assert.assertNotNull(resultDocument);

        // Verify the identifier field
        Assert.assertEquals(resultDocument.getFieldValue("id"), id);

        // Verify the non-multivalued field
        Assert.assertEquals(resultDocument.getFieldValue(dynamicField), Integer.valueOf(1));

        // Verify the multivalued field
        final Collection<Object> dynamicFieldValues = resultDocument.getFieldValues(dynamicFieldMultiValued);
        Assert.assertNotNull(dynamicFieldValues);
        Assert.assertEquals(2, dynamicFieldValues.size());
        Assert.assertTrue(dynamicFieldValues.contains(Integer.valueOf(3)));
        Assert.assertTrue(dynamicFieldValues.contains(Integer.valueOf(4)));
    } finally {
        if (solrServer != null) {
            solrServer.deleteById(id);
            solrServer.commit();
        }
    }
}