2016-01-21 69 views

回答

1

Spring数据内置支持文本搜索。

我用以下的依赖:

<dependency> 
    <groupId>org.springframework.data</groupId> 
    <artifactId>spring-data-mongodb</artifactId> 
    <version>1.8.2.RELEASE</version> 
</dependency> 

尝试以下语法:

TextCriteria criteria = TextCriteria.forDefaultLanguage().matchingAny("read"); 

Query query = TextQuery.queryText(criteria);  

List<klass> list = mongoTemplate.find(query, klass, "collection_name"); 

如需详细资料请参阅this

要做到在聚合使用相同的语法如下:

BasicDBObject match = new BasicDBObject("$match", 
       new BasicDBObject("$text", new BasicDBObject("$search", "COST"))); 

List<DBObject> aggregationList = new ArrayList<DBObject>(); 
aggregationList.add(match); 

AggregationOutput aggregationOutput = mongoTemplate.getCollection("categoryMaster") 
     .aggregate(aggregationList); 

List<DBObject> dbObjects = (List<DBObject>) aggregationOutput.results(); 

转换这dbobjectsklass如下:

for(DBObject dbObject : dbObjects) { 
    mongoTemplate.getConverter().read(klass, dbObject); 
} 
+0

感谢,你知道如何,使用聚合办? – shemerk

+0

谢谢!无法在网络上的任何位置找到此示例! – shemerk