|
|
@@ -8,13 +8,9 @@ import java.io.IOException;
|
|
|
import java.nio.file.Paths;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
-import java.util.Set;
|
|
|
|
|
|
import org.apache.log4j.Logger;
|
|
|
import org.apache.lucene.document.Document;
|
|
|
-import org.apache.lucene.document.Field.Store;
|
|
|
-import org.apache.lucene.document.StringField;
|
|
|
-import org.apache.lucene.document.TextField;
|
|
|
import org.apache.lucene.index.IndexWriter;
|
|
|
import org.apache.lucene.index.Term;
|
|
|
import org.apache.lucene.store.FSDirectory;
|
|
|
@@ -46,7 +42,6 @@ import com.uas.search.console.model.OrderDetailSimpleInfo;
|
|
|
import com.uas.search.console.model.OrderInvoiceDetailSimpleInfo;
|
|
|
import com.uas.search.console.model.OrderInvoiceSimpleInfo;
|
|
|
import com.uas.search.console.model.OrderSimpleInfo;
|
|
|
-import com.uas.search.console.model.PropertyValue;
|
|
|
import com.uas.search.console.model.PurchaseDetailSimpleInfo;
|
|
|
import com.uas.search.console.model.PurchaseInvoiceDetailSimpleInfo;
|
|
|
import com.uas.search.console.model.PurchaseInvoiceSimpleInfo;
|
|
|
@@ -224,7 +219,7 @@ public class IndexServiceImpl implements IndexService {
|
|
|
logger.info("发现数据:" + kinds.size() + "条");
|
|
|
long count = 0;
|
|
|
for (KindSimpleInfo kind : kinds) {
|
|
|
- Document document = toDocument(kind);
|
|
|
+ Document document = ObjectToDocumentUtils.toDocument(kind);
|
|
|
if (document != null) {
|
|
|
indexWriter.addDocument(document);
|
|
|
count++;
|
|
|
@@ -249,7 +244,7 @@ public class IndexServiceImpl implements IndexService {
|
|
|
logger.info("发现数据:" + brands.size() + "条");
|
|
|
long count = 0;
|
|
|
for (BrandSimpleInfo brand : brands) {
|
|
|
- Document document = toDocument(brand);
|
|
|
+ Document document = ObjectToDocumentUtils.toDocument(brand);
|
|
|
if (document != null) {
|
|
|
indexWriter.addDocument(document);
|
|
|
count++;
|
|
|
@@ -277,7 +272,7 @@ public class IndexServiceImpl implements IndexService {
|
|
|
String line = null;
|
|
|
while (!StringUtils.isEmpty(line = bufferedReader.readLine())) {
|
|
|
ComponentSimpleInfo component = JSONObject.parseObject(line, ComponentSimpleInfo.class);
|
|
|
- Document document = toDocument(component);
|
|
|
+ Document document = ObjectToDocumentUtils.toDocument(component);
|
|
|
if (document != null) {
|
|
|
size++;
|
|
|
// 每创建10000条,打印一次进度
|
|
|
@@ -323,7 +318,7 @@ public class IndexServiceImpl implements IndexService {
|
|
|
while (totalElements > size) {
|
|
|
List<ComponentSimpleInfo> components = pageResult.getContent();
|
|
|
for (ComponentSimpleInfo component : components) {
|
|
|
- Document document = toDocument(component);
|
|
|
+ Document document = ObjectToDocumentUtils.toDocument(component);
|
|
|
if (document != null) {
|
|
|
indexWriter.addDocument(document);
|
|
|
}
|
|
|
@@ -434,162 +429,27 @@ public class IndexServiceImpl implements IndexService {
|
|
|
return count;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * KindSimpleInfo对象转为Document
|
|
|
- *
|
|
|
- * @param kind
|
|
|
- * @return
|
|
|
- */
|
|
|
- private Document toDocument(KindSimpleInfo kind) {
|
|
|
- if (kind == null || kind.getId() == null || StringUtils.isEmpty(kind.getNameCn()) || kind.getIsLeaf() == null
|
|
|
- || kind.getLevel() == null) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- Document document = new Document();
|
|
|
- // 不能用LongField,否则后续实时更新索引时,方法updateDocument(new Term("", ""),
|
|
|
- // doc)无法根据id进行更新
|
|
|
- document.add(new StringField(SearchConstants.KIND_ID_FIELD, String.valueOf(kind.getId()), Store.YES));
|
|
|
- document.add(new TextField(SearchConstants.KIND_NAMECN_FIELD, kind.getNameCn(), Store.YES));
|
|
|
- document.add(new StringField(SearchConstants.KIND_ISLEAF_FIELD, String.valueOf(kind.getIsLeaf()), Store.YES));
|
|
|
- document.add(new StringField(SearchConstants.KIND_LEVEL_FIELD, String.valueOf(kind.getLevel()), Store.YES));
|
|
|
- return document;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * BrandSimpleInfo对象转为Document
|
|
|
- *
|
|
|
- * @param brand
|
|
|
- * @return
|
|
|
- */
|
|
|
- private Document toDocument(BrandSimpleInfo brand) {
|
|
|
- if (brand == null || brand.getId() == null || StringUtils.isEmpty(brand.getNameCn())
|
|
|
- || StringUtils.isEmpty(brand.getUuid())) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- Document document = new Document();
|
|
|
- document.add(new StringField(SearchConstants.BRAND_ID_FIELD, String.valueOf(brand.getId()), Store.YES));
|
|
|
- document.add(new TextField(SearchConstants.BRAND_NAMECN_FIELD, brand.getNameCn(), Store.YES));
|
|
|
- String nameEn = brand.getNameEn();
|
|
|
- if (!StringUtils.isEmpty(nameEn)) {
|
|
|
- document.add(new TextField(SearchConstants.BRAND_NAMEEN_FIELD, nameEn, Store.YES));
|
|
|
- }
|
|
|
- document.add(new StringField(SearchConstants.BRAND_UUID_FIELD, brand.getUuid(), Store.YES));
|
|
|
- return document;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * ComponentSimpleInfo对象转为Document
|
|
|
- *
|
|
|
- * @param component
|
|
|
- * @return
|
|
|
- */
|
|
|
- private Document toDocument(ComponentSimpleInfo component) {
|
|
|
- if (component == null || component.getId() == null || StringUtils.isEmpty(component.getUuid())
|
|
|
- || StringUtils.isEmpty(component.getCode()) || component.getKindid() == null
|
|
|
- || component.getBrandid() == null) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- Document document = new Document();
|
|
|
- document.add(new StringField(SearchConstants.COMPONENT_ID_FIELD, String.valueOf(component.getId()), Store.YES));
|
|
|
- document.add(new StringField(SearchConstants.COMPONENT_UUID_FIELD, component.getUuid(), Store.YES));
|
|
|
- // 转小写,以避免分词,又不会因大小写影响搜索
|
|
|
- String code = component.getCode().toLowerCase();
|
|
|
- document.add(new StringField(SearchConstants.COMPONENT_CODE_FIELD, code, Store.YES));
|
|
|
- document.add(new StringField(SearchConstants.COMPONENT_KINDID_FIELD, String.valueOf(component.getKindid()),
|
|
|
- Store.YES));
|
|
|
- document.add(new StringField(SearchConstants.COMPONENT_BRANDID_FIELD, String.valueOf(component.getBrandid()),
|
|
|
- Store.YES));
|
|
|
-
|
|
|
- // 属性值加入索引,索引中field的键:"pr_"前缀连接属性的id
|
|
|
- Set<PropertyValue> propertyValues = component.getProperties();
|
|
|
- for (PropertyValue propertyValue : propertyValues) {
|
|
|
- if (!StringUtils.isEmpty(propertyValue.getValue())) {
|
|
|
- String fieldKey = SearchConstants.COMPONENT_PROPERTY_PREFIX + propertyValue.getPropertyid();
|
|
|
- document.add(new StringField(fieldKey, propertyValue.getValue(), Store.YES));
|
|
|
-
|
|
|
- // 另建一份分词的属性索引,用于属性值联想时不区分大小写
|
|
|
- String fieldKeyTokenized = fieldKey + SearchConstants.COMPONENT_PROPERTY_TOKENIZED_SUFFIX;
|
|
|
- document.add(new TextField(fieldKeyTokenized, propertyValue.getValue(), Store.YES));
|
|
|
- }
|
|
|
- }
|
|
|
- return document;
|
|
|
- }
|
|
|
-
|
|
|
@Override
|
|
|
public void save(Object obj) {
|
|
|
if (obj == null) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- try {
|
|
|
- indexWriter = indexWriterManager.get();
|
|
|
- if (obj instanceof KindSimpleInfo) {
|
|
|
- Document document = toDocument((KindSimpleInfo) obj);
|
|
|
- if (document != null) {
|
|
|
- indexWriter.addDocument(document);
|
|
|
- }
|
|
|
- } else if (obj instanceof BrandSimpleInfo) {
|
|
|
- Document document = toDocument((BrandSimpleInfo) obj);
|
|
|
- if (document != null) {
|
|
|
- indexWriter.addDocument(document);
|
|
|
- }
|
|
|
- } else if (obj instanceof ComponentSimpleInfo) {
|
|
|
- Document document = toDocument((ComponentSimpleInfo) obj);
|
|
|
- if (document != null) {
|
|
|
- indexWriter.addDocument(document);
|
|
|
- }
|
|
|
- } else if (obj instanceof OrderSimpleInfo) {
|
|
|
- Document document = ObjectToDocumentUtils.toDocument((OrderSimpleInfo) obj);
|
|
|
- if (document != null) {
|
|
|
- indexWriter.addDocument(document);
|
|
|
- }
|
|
|
- } else if (obj instanceof OrderDetailSimpleInfo) {
|
|
|
- Document document = ObjectToDocumentUtils.toDocument((OrderDetailSimpleInfo) obj);
|
|
|
- if (document != null) {
|
|
|
- indexWriter.addDocument(document);
|
|
|
- }
|
|
|
- } else if (obj instanceof OrderInvoiceSimpleInfo) {
|
|
|
- Document document = ObjectToDocumentUtils.toDocument((OrderInvoiceSimpleInfo) obj);
|
|
|
- if (document != null) {
|
|
|
- indexWriter.addDocument(document);
|
|
|
- }
|
|
|
- } else if (obj instanceof OrderInvoiceDetailSimpleInfo) {
|
|
|
- Document document = ObjectToDocumentUtils.toDocument((OrderInvoiceDetailSimpleInfo) obj);
|
|
|
- if (document != null) {
|
|
|
- indexWriter.addDocument(document);
|
|
|
- }
|
|
|
- } else if (obj instanceof PurchaseSimpleInfo) {
|
|
|
- Document document = ObjectToDocumentUtils.toDocument((PurchaseSimpleInfo) obj);
|
|
|
- if (document != null) {
|
|
|
- indexWriter.addDocument(document);
|
|
|
- }
|
|
|
- } else if (obj instanceof PurchaseDetailSimpleInfo) {
|
|
|
- Document document = ObjectToDocumentUtils.toDocument((PurchaseDetailSimpleInfo) obj);
|
|
|
- if (document != null) {
|
|
|
- indexWriter.addDocument(document);
|
|
|
- }
|
|
|
- } else if (obj instanceof PurchaseInvoiceSimpleInfo) {
|
|
|
- Document document = ObjectToDocumentUtils.toDocument((PurchaseInvoiceSimpleInfo) obj);
|
|
|
- if (document != null) {
|
|
|
- indexWriter.addDocument(document);
|
|
|
- }
|
|
|
- } else if (obj instanceof PurchaseInvoiceDetailSimpleInfo) {
|
|
|
- Document document = ObjectToDocumentUtils.toDocument((PurchaseInvoiceDetailSimpleInfo) obj);
|
|
|
- if (document != null) {
|
|
|
- indexWriter.addDocument(document);
|
|
|
- }
|
|
|
- } else {
|
|
|
- logger.error("Message parsing failed!");
|
|
|
+ Document document = ObjectToDocumentUtils.toDocument(obj);
|
|
|
+ if (document != null) {
|
|
|
+ try {
|
|
|
+ indexWriter = indexWriterManager.get();
|
|
|
+ indexWriter.addDocument(document);
|
|
|
+ indexWriter.commit();
|
|
|
+ logger.info("Saved... " + obj + "\n");
|
|
|
+ } catch (IOException | InterruptedException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ indexWriterManager.release();
|
|
|
}
|
|
|
- indexWriter.commit();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } catch (InterruptedException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- indexWriterManager.release();
|
|
|
+ } else {
|
|
|
+ logger.info("对象转为Document时为null:" + obj);
|
|
|
}
|
|
|
- logger.info("Saved... " + obj + "\n");
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -597,41 +457,34 @@ public class IndexServiceImpl implements IndexService {
|
|
|
if (obj == null) {
|
|
|
return;
|
|
|
}
|
|
|
- try {
|
|
|
- indexWriter = indexWriterManager.get();
|
|
|
- if (obj instanceof KindSimpleInfo) {
|
|
|
- KindSimpleInfo kind = (KindSimpleInfo) obj;
|
|
|
- Document document = toDocument(kind);
|
|
|
- if (document != null) {
|
|
|
- indexWriter.updateDocument(new Term(SearchConstants.KIND_ID_FIELD, String.valueOf(kind.getId())),
|
|
|
- document);
|
|
|
- }
|
|
|
- } else if (obj instanceof BrandSimpleInfo) {
|
|
|
- BrandSimpleInfo brand = (BrandSimpleInfo) obj;
|
|
|
- Document document = toDocument((BrandSimpleInfo) obj);
|
|
|
- if (document != null) {
|
|
|
- indexWriter.updateDocument(new Term(SearchConstants.BRAND_ID_FIELD, String.valueOf(brand.getId())),
|
|
|
+ Document document = ObjectToDocumentUtils.toDocument(obj);
|
|
|
+ if (document != null) {
|
|
|
+ try {
|
|
|
+ indexWriter = indexWriterManager.get();
|
|
|
+ if (obj instanceof KindSimpleInfo) {
|
|
|
+ indexWriter.updateDocument(
|
|
|
+ new Term(SearchConstants.KIND_ID_FIELD, String.valueOf(((KindSimpleInfo) obj).getId())),
|
|
|
document);
|
|
|
- }
|
|
|
- } else if (obj instanceof ComponentSimpleInfo) {
|
|
|
- ComponentSimpleInfo component = (ComponentSimpleInfo) obj;
|
|
|
- Document document = toDocument(component);
|
|
|
- if (document != null) {
|
|
|
+ } else if (obj instanceof BrandSimpleInfo) {
|
|
|
indexWriter.updateDocument(
|
|
|
- new Term(SearchConstants.COMPONENT_ID_FIELD, String.valueOf(component.getId())), document);
|
|
|
+ new Term(SearchConstants.BRAND_ID_FIELD, String.valueOf(((BrandSimpleInfo) obj).getId())),
|
|
|
+ document);
|
|
|
+ } else if (obj instanceof ComponentSimpleInfo) {
|
|
|
+ indexWriter.updateDocument(new Term(SearchConstants.COMPONENT_ID_FIELD,
|
|
|
+ String.valueOf(((ComponentSimpleInfo) obj).getId())), document);
|
|
|
+ } else {
|
|
|
+ logger.error("Message parsing failed!");
|
|
|
}
|
|
|
- } else {
|
|
|
- logger.error("Message parsing failed!");
|
|
|
+ indexWriter.commit();
|
|
|
+ logger.info("Updated... " + obj + "\n");
|
|
|
+ } catch (IOException | InterruptedException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ indexWriterManager.release();
|
|
|
}
|
|
|
- indexWriter.commit();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } catch (InterruptedException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- indexWriterManager.release();
|
|
|
+ } else {
|
|
|
+ logger.info("对象转为Document时为null:" + obj);
|
|
|
}
|
|
|
- logger.info("Updated... " + obj + "\n");
|
|
|
}
|
|
|
|
|
|
@Override
|