productdocument.java

来自「一个搜索引擎,希望对大家有用」· Java 代码 · 共 63 行

JAVA
63
字号
package com.luceneheritrixbook.index;

import org.apache.lucene.document.*;

import com.luceneheritrixbook.core.Product;

public class ProductDocument {

	private static final String PRODUCT_ID = "productid";

	private static final String INDEX_TIME = "indextime";

	private static final String PRODUCT_URL = "producturl";

	private static final String CATEGORY = "category";

	private static final String PRODUCT_NAME = "name";

	private static final String PRODUCT_TYPE = "type";

	public static Document buildProductDocument(Product product, int id) {

		Document doc = new Document();

		Field identifier = new Field(PRODUCT_ID, id + "", Field.Store.YES,
				Field.Index.UN_TOKENIZED);

		long mills = System.currentTimeMillis();
		Field indextime = new Field(INDEX_TIME, mills + "", Field.Store.YES,
				Field.Index.UN_TOKENIZED);

		Field producturl = new Field(PRODUCT_URL, product.getOriginalUrl(),
				Field.Store.YES, Field.Index.UN_TOKENIZED);

		Field category = new Field(CATEGORY, product.getCategory(),
				Field.Store.YES, Field.Index.TOKENIZED);

		Field name = new Field(PRODUCT_NAME, product.getName(),
				Field.Store.YES, Field.Index.TOKENIZED);

		Field type = new Field(PRODUCT_TYPE, product.getType(),
				Field.Store.YES, Field.Index.TOKENIZED);
		
		String text = product.getCategory();
		text += " " + product.getName();
		text += " " + product.getType();
		Field all = new Field("all", text, Field.Store.YES, Field.Index.TOKENIZED);

		// add all
		doc.add(identifier);
		doc.add(indextime);
		doc.add(producturl);
		doc.add(category);
		doc.add(name);
		doc.add(type);
		doc.add(all);

		return doc;

	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?