使用Java在MongoDB中从外部为值分配权重 [英] Assigning weights to the values externally in MongoDB using Java

查看:86
本文介绍了使用Java在MongoDB中从外部为值分配权重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我可以在 Mongo Shell 中创建具有不同权重的索引的方法.

This is how I could create index with different weights in Mongo Shell.

             db.blog.ensureIndex(
                 {
                   content: "text",
                   keywords: "text",
                   about: "text"
                 },
                 {
                   weights: {
                              content: 10,
                              keywords: 5,
                            },
                   name: "TextIndex"
                 }
               )

这就是我使用 Java 为字段创建索引的方式.

This is how I created Index for the fields using Java.

    BasicDBObject index = new BasicDBObject();
        index.put("content", "text");
        index.put("keywords", "text");
        index.put("about", "text");

collection.ensureIndex( index, "TextIndex");

如何使用Java驱动程序分配权重?

How do I assign weights using the Java driver?

推荐答案

将权重分配给Java中的索引字段:

Assigning weights to the indexed fields from Java:

    BasicDBObject index = new BasicDBObject();
    index.put("content", "text");
    index.put("keywords", "text");
    index.put("about", "text");
    BasicDBObject weights = new BasicDBObject("content", 10).append("keyword", 5);
    BasicDBObject options = new BasicDBObject("weights", weights).append("name", "TextIndex");
    collection.createIndex(index,options);

这篇关于使用Java在MongoDB中从外部为值分配权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆