diff --git a/lucene/build.gradle b/lucene/build.gradle index d786a544a..fb2a2bceb 100644 --- a/lucene/build.gradle +++ b/lucene/build.gradle @@ -17,7 +17,7 @@ * under the License. */ dependencies { - pluginLibsCompile 'org.apache.lucene:lucene-core:6.6.0' - pluginLibsCompile 'org.apache.lucene:lucene-queryparser:6.6.0' - pluginLibsCompile 'org.apache.lucene:lucene-analyzers-common:6.6.0' + pluginLibsCompile 'org.apache.lucene:lucene-core:7.1.0' + pluginLibsCompile 'org.apache.lucene:lucene-queryparser:7.1.0' + pluginLibsCompile 'org.apache.lucene:lucene-analyzers-common:7.1.0' } \ No newline at end of file diff --git a/lucene/src/main/java/org/apache/ofbiz/content/search/ProductDocument.java b/lucene/src/main/java/org/apache/ofbiz/content/search/ProductDocument.java index 7e19bf077..61d79c1b4 100644 --- a/lucene/src/main/java/org/apache/ofbiz/content/search/ProductDocument.java +++ b/lucene/src/main/java/org/apache/ofbiz/content/search/ProductDocument.java @@ -81,11 +81,11 @@ public class ProductDocument implements LuceneDocument { // Product Fields doc.add(new StringField("productId", productId, Field.Store.YES)); - this.addTextFieldByWeight(doc, "productName", product.getString("productName"), "index.weight.Product.productName", 0, false, "fullText", delegator); - this.addTextFieldByWeight(doc, "internalName", product.getString("internalName"), "index.weight.Product.internalName", 0, false, "fullText", delegator); - this.addTextFieldByWeight(doc, "brandName", product.getString("brandName"), "index.weight.Product.brandName", 0, false, "fullText", delegator); - this.addTextFieldByWeight(doc, "description", product.getString("description"), "index.weight.Product.description", 0, false, "fullText", delegator); - this.addTextFieldByWeight(doc, "longDescription", product.getString("longDescription"), "index.weight.Product.longDescription", 0, false, "fullText", delegator); + this.addTextField(doc, "productName", product.getString("productName"), false, "fullText", delegator); + this.addTextField(doc, "internalName", product.getString("internalName"), false, "fullText", delegator); + this.addTextField(doc, "brandName", product.getString("brandName"), false, "fullText", delegator); + this.addTextField(doc, "description", product.getString("description"), false, "fullText", delegator); + this.addTextField(doc, "longDescription", product.getString("longDescription"), false, "fullText", delegator); doc.add(new LongPoint("introductionDate", quantizeTimestampToDays(product.getTimestamp("introductionDate")))); nextReIndex = this.checkSetNextReIndex(product.getTimestamp("introductionDate"), nextReIndex); doc.add(new LongPoint("salesDiscontinuationDate", quantizeTimestampToDays(product.getTimestamp("salesDiscontinuationDate")))); @@ -113,9 +113,9 @@ public class ProductDocument implements LuceneDocument { doc.add(new StringField("productFeatureId", productFeatureAndAppl.getString("productFeatureId"), Field.Store.NO)); doc.add(new StringField("productFeatureCategoryId", productFeatureAndAppl.getString("productFeatureCategoryId"), Field.Store.NO)); doc.add(new StringField("productFeatureTypeId", productFeatureAndAppl.getString("productFeatureTypeId"), Field.Store.NO)); - this.addTextFieldByWeight(doc, "featureDescription", productFeatureAndAppl.getString("description"), "index.weight.ProductFeatureAndAppl.description", 0, false, "fullText", delegator); - this.addTextFieldByWeight(doc, "featureAbbreviation", productFeatureAndAppl.getString("abbrev"), "index.weight.ProductFeatureAndAppl.abbrev", 0, false, "fullText", delegator); - this.addTextFieldByWeight(doc, "featureCode", productFeatureAndAppl.getString("idCode"), "index.weight.ProductFeatureAndAppl.idCode", 0, false, "fullText", delegator); + this.addTextField(doc, "featureDescription", productFeatureAndAppl.getString("description"), false, "fullText", delegator); + this.addTextField(doc, "featureAbbreviation", productFeatureAndAppl.getString("abbrev"), false, "fullText", delegator); + this.addTextField(doc, "featureCode", productFeatureAndAppl.getString("idCode"), false, "fullText", delegator); // Get the ProductFeatureGroupIds List productFeatureGroupAppls = EntityQuery.use(delegator).from("ProductFeatureGroupAppl").where("productFeatureId", productFeatureAndAppl.get("productFeatureId")).queryList(); productFeatureGroupAppls = this.filterByThruDate(productFeatureGroupAppls); @@ -140,8 +140,8 @@ public class ProductDocument implements LuceneDocument { List productAttributes = EntityQuery.use(delegator).from("ProductAttribute").where("productId", productId).queryList(); for (GenericValue productAttribute: productAttributes) { - this.addTextFieldByWeight(doc, "attributeName", productAttribute.getString("attrName"), "index.weight.ProductAttribute.attrName", 0, false, "fullText", delegator); - this.addTextFieldByWeight(doc, "attributeValue", productAttribute.getString("attrValue"), "index.weight.ProductAttribute.attrValue", 0, false, "fullText", delegator); + this.addTextField(doc, "attributeName", productAttribute.getString("attrName"), false, "fullText", delegator); + this.addTextField(doc, "attributeValue", productAttribute.getString("attrValue"), false, "fullText", delegator); } } @@ -153,7 +153,7 @@ public class ProductDocument implements LuceneDocument { String idValue = goodIdentification.getString("idValue"); doc.add(new StringField("goodIdentificationTypeId", goodIdentificationTypeId, Field.Store.NO)); doc.add(new StringField(goodIdentificationTypeId + "_GoodIdentification", idValue, Field.Store.NO)); - this.addTextFieldByWeight(doc, "identificationValue", idValue, "index.weight.GoodIdentification.idValue", 0, false, "fullText", delegator); + this.addTextField(doc, "identificationValue", idValue, false, "fullText", delegator); } } @@ -172,7 +172,7 @@ public class ProductDocument implements LuceneDocument { } else if (thruDate != null) { nextReIndex = this.checkSetNextReIndex(thruDate, nextReIndex); } - this.addTextFieldByWeight(doc, "variantProductId", variantProductAssoc.getString("productIdTo"), "index.weight.Variant.Product.productId", 0, false, "fullText", delegator); + this.addTextField(doc, "variantProductId", variantProductAssoc.getString("productIdTo"), false, "fullText", delegator); } } } @@ -203,7 +203,7 @@ public class ProductDocument implements LuceneDocument { try { Map drContext = UtilMisc.toMap("product", product); String contentText = DataResourceWorker.renderDataResourceAsText(null, delegator, productContentAndInfo.getString("dataResourceId"), drContext, null, null, false); - this.addTextFieldByWeight(doc, "content", contentText, null, weight, false, "fullText", delegator); + this.addTextField(doc, "content", contentText, false, "fullText", delegator); } catch (IOException e1) { Debug.logError(e1, "Error getting content text to index", module); } catch (GeneralException e1) { @@ -279,26 +279,10 @@ public class ProductDocument implements LuceneDocument { } // An attempt to boost/weight values in a similar manner to what OFBiz product search does. - private void addTextFieldByWeight(Document doc, String fieldName, String value, String property, int defaultWeight, boolean store, String fullTextFieldName, Delegator delegator) { + private void addTextField(Document doc, String fieldName, String value, boolean store, String fullTextFieldName, Delegator delegator) { if (fieldName == null) return; - float weight = 0; - if (property != null) { - try { - weight = EntityUtilProperties.getPropertyAsFloat("prodsearch", property, 0).floatValue(); - } catch (Exception e) { - Debug.logWarning("Could not parse weight number: " + e.toString(), module); - } - } else if (defaultWeight > 0) { - weight = defaultWeight; - } - if (weight == 0 && !store) { - return; - } Field field = new TextField(fieldName, checkValue(value), (store? Field.Store.YES: Field.Store.NO)); - if (weight > 0 && weight != 1) { - field.setBoost(weight); - } doc.add(field); if (fullTextFieldName != null) { doc.add(new TextField(fullTextFieldName, checkValue(value), Field.Store.NO)); diff --git a/lucene/src/main/java/org/apache/ofbiz/content/search/SearchWorker.java b/lucene/src/main/java/org/apache/ofbiz/content/search/SearchWorker.java index 7a51df64f..bac684acf 100644 --- a/lucene/src/main/java/org/apache/ofbiz/content/search/SearchWorker.java +++ b/lucene/src/main/java/org/apache/ofbiz/content/search/SearchWorker.java @@ -41,7 +41,7 @@ public final class SearchWorker { public static final String module = SearchWorker.class.getName(); - private static final Version LUCENE_VERSION = Version.LUCENE_6_6_0; + private static final Version LUCENE_VERSION = Version.LUCENE_7_1_0; private SearchWorker() {} diff --git a/solr/build.gradle b/solr/build.gradle index 6369a56c0..48a1f5ddf 100644 --- a/solr/build.gradle +++ b/solr/build.gradle @@ -17,7 +17,7 @@ * under the License. */ dependencies { - pluginLibsCompile 'org.apache.solr:solr-core:6.6.0' + pluginLibsCompile 'org.apache.solr:solr-core:7.1.0' pluginLibsCompile 'com.google.guava:guava:20.0' } diff --git a/solr/src/main/java/org/apache/ofbiz/solr/SolrUtil.java b/solr/src/main/java/org/apache/ofbiz/solr/SolrUtil.java index 2eecc7c0e..2da0a37c5 100644 --- a/solr/src/main/java/org/apache/ofbiz/solr/SolrUtil.java +++ b/solr/src/main/java/org/apache/ofbiz/solr/SolrUtil.java @@ -330,7 +330,7 @@ public final class SolrUtil { httpLogin.setConfig(requestConfig); CloseableHttpResponse loginResponse = httpClient.execute(httpLogin, httpContext); loginResponse.close(); - return new HttpSolrClient(solrUrl + "/" + solrIndexName, httpClient); + return new HttpSolrClient.Builder(solrUrl + "/" + solrIndexName).withHttpClient(httpClient).build(); } } diff --git a/solr/src/main/java/org/apache/ofbiz/solr/test/SolrTests.java b/solr/src/main/java/org/apache/ofbiz/solr/test/SolrTests.java new file mode 100644 index 000000000..40b3dd789 --- /dev/null +++ b/solr/src/main/java/org/apache/ofbiz/solr/test/SolrTests.java @@ -0,0 +1,141 @@ + +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you 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 org.apache.ofbiz.solr.test; + +import java.util.HashMap; +import java.util.Map; +import java.util.ArrayList; +import java.util.List; + +import org.apache.jasper.tagplugins.jstl.core.Remove; +import org.apache.ofbiz.base.util.UtilMisc; +import org.apache.ofbiz.entity.Delegator; +import org.apache.ofbiz.entity.GenericValue; +import org.apache.ofbiz.entity.util.EntityQuery; +import org.apache.ofbiz.service.ServiceUtil; +import org.apache.ofbiz.service.testtools.OFBizTestCase; +import org.apache.solr.client.solrj.SolrQuery; +import org.apache.solr.common.SolrDocument; +import org.apache.solr.common.SolrDocumentList; +import org.apache.solr.client.solrj.response.QueryResponse; + +public class SolrTests extends OFBizTestCase { + + protected GenericValue userLogin = null; + private Map context; + private Map emptyContext = new HashMap(); + private Map response; + private String validTestProductId = "GZ-1006"; + private String validTestProductId_2 = "GZ-1005"; + private String invalidTestProductId = validTestProductId + validTestProductId; + + public SolrTests(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + userLogin = EntityQuery.use(delegator).from("UserLogin").where("userLoginId", "system").queryOne(); + } + + @Override + protected void tearDown() throws Exception { + } + + public void testAddProductToIndex() throws Exception { + + GenericValue product = delegator.findOne("Product", UtilMisc.toMap("productId", validTestProductId), false); + + Map ctx = new HashMap(); + ctx.put("instance", product); + + Map resp = dispatcher.runSync("addToSolr", ctx); + assertTrue("Could not init search index", ServiceUtil.isSuccess(resp)); + + Map sctx = new HashMap(); + sctx.put("productCategoryId", "102"); + + Map sresp = dispatcher.runSync("solrProductsSearch", sctx); + assertTrue("Could not query search index", ServiceUtil.isSuccess(sresp)); + + + } + + public void testAddToSolrIndex() throws Exception{ + context = new HashMap<>(); + context.put("productId", validTestProductId); + response = dispatcher.runSync("addToSolrIndex", context); + assertTrue("Could not add Product to Index", ServiceUtil.isSuccess( + response)); + } + + public void testAddToSolrIndex_invalidProduct() throws Exception { + context = new HashMap<>(); + context.put("productId", invalidTestProductId); + response = dispatcher.runSync("addToSolrIndex", context); + assertTrue("Could not test the addition of an invalid product to the Solr index", ServiceUtil.isSuccess( + response)); + } + + public void testAddListToSolrIndex() throws Exception { + List> products = new ArrayList<>(); + Map product_1 = new HashMap<>(); + Map product_2 = new HashMap<>(); + GenericValue validTestProduct = delegator.findOne("Product", UtilMisc.toMap("productId", validTestProductId), false); + GenericValue validTestProduct_2 = delegator.findOne("Product", UtilMisc.toMap("productId", validTestProductId_2), false); + + product_1.put("productId", validTestProduct); + product_2.put("productId", validTestProduct_2); + + products.add(product_1); + products.add(product_2); + context = new HashMap<>(); + context.put("fieldList", products); + + response = dispatcher.runSync("addListToSolrIndex", context); + assertTrue("Could not add products to index", ServiceUtil.isSuccess(response)); + + } + + public void testAddListToSolrIndex_invalidProducts() throws Exception { + List> products = new ArrayList<>(); + Map product_1 = new HashMap<>(); + Map product_2 = new HashMap<>(); + GenericValue testProduct = delegator.findOne("Product", UtilMisc.toMap("productId", validTestProductId), false); + GenericValue testProduct_2 = delegator.findOne("Product", UtilMisc.toMap("productId", validTestProductId_2), false); + + testProduct.replace("productId", invalidTestProductId); + testProduct.replace("productId", invalidTestProductId); + + product_1.put("productId", testProduct); + product_2.put("productId", testProduct_2); + + products.add(product_1); + products.add(product_2); + context = new HashMap<>(); + context.put("fieldList", products); + + response = dispatcher.runSync("addListToSolrIndex", context); + assertTrue("Could not test adding invalid products to index", ServiceUtil.isSuccess(response)); + + } +} + diff --git a/solr/testdef/data/SolrTestsData.xml b/solr/testdef/data/SolrTestsData.xml new file mode 100644 index 000000000..da49c238a --- /dev/null +++ b/solr/testdef/data/SolrTestsData.xml @@ -0,0 +1,24 @@ + + + + + + + \ No newline at end of file diff --git a/solr/testdef/solrtests.xml b/solr/testdef/solrtests.xml new file mode 100644 index 000000000..d2216f969 --- /dev/null +++ b/solr/testdef/solrtests.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + \ No newline at end of file