2016-01-21 104 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