compass关键字匹配

	Compass compass = compassTemplate.getCompass();
		CompassSession session = compass.openSession();
		CompassQueryBuilder queryBuilder = session.queryBuilder();
		CompassBooleanQueryBuilder boolQuery = queryBuilder.bool();
		List list = new ArrayList();
		
		//查询一个属性
		CompassHits hits1 = session.queryBuilder().queryString("name:" + queryString).toQuery().hits();

		// 查询任意个属性
		boolQuery.addShould(queryBuilder.queryString("name:" + queryString).toQuery());
		boolQuery.addShould(queryBuilder.queryString("description:" + queryString).toQuery());
		CompassHits hits2 = boolQuery.toQuery().hits();

		// 查询所有的属性
		CompassHits hits = queryBuilder.queryString(queryString).toQuery().hits();

		//查询结果处理
		System.out.println("queryString:" + queryString);
		System.out.println("hits:" + hits.getLength());
		for (int i = 0; i < hits.length(); i++) {
			Product hit = (Product) hits.data(i);
			list.add(hit);
		}
参考:
CompassQueryBuilder queryBuilder=session.queryBuilder();
CompassBooleanQueryBuilder boolQuery=queryBuilder.bool();
//必须匹配
boolQuery.addMust(queryBuilder.queryString(type+":"+queryString).toQuery());
//必须不匹配
boolQuery.addMustNot(queryBuilder.queryString(type+":"+queryString).toQuery());
//或者匹配 也许没有 
boolQuery.addShould(queryBuilder.queryString(type+":"+queryString).toQuery());

你可能感兴趣的:(compass)