【问题标题】:How to use text-search in mongo-spring aggregate如何在 mongo-spring 聚合中使用文本搜索
【发布时间】:2016-01-21 08:54:09
【问题描述】:

我如何翻译一个简单的 mongo shell $match 短语,它是等价的 在Java中的mongo-spring中-使用聚合?

$match: { $text: { $search: "read" } } 

【问题讨论】:

    标签: java spring spring-mongo fulltext-index


    【解决方案1】:

    Spring-data 内置了对文本搜索的支持。

    我使用了以下依赖项:

    <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();
    

    将此dbobjects 转换为您的klass 如下:

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

    【讨论】:

    • 谢谢,你知道如何使用聚合来做到这一点吗?
    • 谢谢!在网络上的任何地方都找不到此示例!
    猜你喜欢
    • 2020-11-11
    • 1970-01-01
    • 2022-01-14
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-24
    相关资源
    最近更新 更多