论坛首页 Java企业应用论坛

COmpass+庖丁解牛分词,有人遇到中文问题吗?

浏览 1222 次
该帖已经被评为隐藏帖
作者 正文
   发表时间:2009-12-01  
使用庖丁能够分词并且建立索引,但是不能搜索中文,只能搜索英文和数字,例如:标题为《数学最后冲刺135分》,搜数学没有结果,搜135就能搜索出来。能添加数据库,没有乱码,统一utf-8
下面是被搜索的实体:
package com.compass.test;

import org.compass.annotations.Index;
import org.compass.annotations.Searchable;
import org.compass.annotations.SearchableId;
import org.compass.annotations.SearchableProperty;

@Searchable
public class Book {
	
	@SearchableId
	private String id;

	@SearchableProperty(analyzer="PaodingAnalyzer", index=Index.TOKENIZED)
	private String title;
	
	@SearchableProperty(name="author", index=Index.NOT_ANALYZED)
	private String author;
	
	@SearchableProperty(name="price", index=Index.NOT_ANALYZED)
	private float price;

	public Book(){}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}
	
}

这是测试搜索类:
package com.compass.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.compass.core.Compass;
import org.compass.core.CompassSession;
import org.compass.core.config.CompassConfiguration;
import org.compass.core.support.search.CompassSearchCommand;
import org.compass.core.support.search.CompassSearchHelper;
import org.compass.core.support.search.CompassSearchResults;
import org.compass.gps.CompassGpsDevice;
import org.compass.gps.device.hibernate.HibernateGpsDevice;
import org.compass.gps.impl.SingleCompassGps;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class CompassSimpleDemo {

	public static void main(String[] args) {
		SessionFactory sessionFactory = new Configuration().configure()
						.buildSessionFactory();
		
		Compass compass = new CompassConfiguration().configure().addClass(
				Book.class).buildCompass();
		
		SingleCompassGps compassGps = new SingleCompassGps(compass);
		CompassGpsDevice compassGpsDevice = new HibernateGpsDevice("hibernate",
							sessionFactory);
		compassGps.addGpsDevice(compassGpsDevice);
		compassGps.start();
		
		CompassSession compassSession = compass.openSession();
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String query = "";
		try {
			query = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		CompassSearchHelper searchHelper = new CompassSearchHelper(compass, 10);
		CompassSearchResults results = searchHelper.search(
							new CompassSearchCommand(query, 0));
		
		System.out.println();
		
		System.out.println("==分页信息==");
		System.out.println("结果总数--->" + results.getTotalHits());
		System.out.println("页数--->" + results.getPages().length);
		System.out.println("当前分页结果数--->" + results.getHits().length);
		System.out.println("搜索耗时--->" + results.getSearchTime()+"ms");
		
		System.out.println();
		
		System.out.println("==商品记录信息==");
		for(int i=0; i<results.getTotalHits(); i++) {
			System.out.println("id--->" + ((Book)results.getHits()[i].getData()).getId());
			System.out.println("标题--->" + ((Book)results.getHits()[i].getData()).getTitle());
			System.out.println("作者--->" + ((Book)results.getHits()[i].getData()).getAuthor());
			System.out.println("价格--->" + ((Book)results.getHits()[i].getData()).getPrice());
			System.out.println("=====================================");
		}

		
		compassSession.close();
		compass.close();
	}

}


大家帮忙看看,网上是在找不到解答。















论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics