【问题标题】:Mongodb Full text search, get the matched string in the documentMongodb全文搜索,获取文档中匹配的字符串
【发布时间】:2014-11-06 10:47:52
【问题描述】:

我正在尝试使用 node 和 mongdodb 后端实现 FTS 功能。在搜索结果中,我还想投影一个新字段,即文档中匹配的字符串。这会给它一种像谷歌一样的感觉。有没有人在没有编写很多自己的自定义函数的情况下对此有想法?

架构如下所示

var version = new mongoose.Schema({     
    name:   String,     
    owner: String, 
    reviewer: String,
    date_of_modification: Date,
    comments:           String,             
    hints: [String],
    global:   Boolean,      
    **content:  { type: [String], index: true }**
    version_no: Number,                 
});


var artifactSchema= new mongoose.Schema({

    pid :   String,
    trashed :  Boolean,
    baseline : Number,
    versions : [version],                   
});

我想在版本模型的内容字段上创建一个索引。

【问题讨论】:

  • 请向我们展示您的预期文档结构和预期结果。实际上,使用聚合应该很容易,但是为了使答案有意义,请举一个具体的例子。
  • 文本搜索结果中的此类元数据当前不可用。也没有一种有效的方法来询问服务器上的响应以在响应中“突出显示”您的搜索词,而无需使用非常讨厌的 mapReduce 代码处理方法,这不是您想要的。为此,您最好使用外部文本搜索引擎。正如@MarkusWMahlberg 所建议的那样,聚合框架缺少匹配字符串的必要运算符,因此这不是一个选择。
  • @NeilLunn:$match 早期阶段的 $regex 会起作用!并且可以使用在项目阶段突出显示字符串连接。还是我错过了什么?
  • @MarkusWMahlberg 但是如何确定搜索中的哪些字符串是匹配项并在聚合管道中“突出显示”它们?没有逻辑上的等价物,因此无法完成。我认为 mapReduce 也不适合,即使您可以在 JavaScript 中使用正则表达式匹配和匹配变量来执行此操作。外部解决方案比 MongoDB 目前和/或将来可能会更好地针对这一点。 Solr/ElasticSearch 等都这样做 OOTB
  • 我认为 Mongodb 会变得一团糟。由于它是一个初始阶段的原型项目,我放入了一个存储内容字段的弹性数据库。 Elasticdb 为 elasticSearch 提供了高亮功能,效果很好。

标签: node.js mongodb search full-text-search


【解决方案1】:

我知道它有点晚了,但最近我用 MEAN+angucomplete 做到了 AngularJS 查询例如http://localhost:8080/api/search?s= 快速查询

router.route('/search')
    .get(function(req, res) {
        Dept.aggregate(
                { $match : { 'Product.name' : new RegExp(query, 'gi') } },
                { $project : { name : 1, _id : 1,  'Product.name' : 1, 'Product._id' : 1} },
                { $unwind : "$Product" },
                { $group : {
                    _id : "$_id",
                    Category : { $addToSet : "$name"},
                    Product : { $push : "$Product"}
                }}
        )
    });

Angucomplete 标记

    <div angucomplete-alt id="depts"
                     placeholder="Click Here to Search across Whole Store"
                     pause="300" 
                     selected-object="selectedDepts" 
                     remote-url="http://localhost:8080/api/search?s="
                     remote-url-data-field="Departments"
                     remote-url-response-formatter="responseFormatted"     
                     search-fields="name"
                     title-field="name"
                     minlength="1"
                     maxlength="20"
                     description-field="description"
                     override-suggestions="true" 
                     match-class="highlight"
                     autocapitalize="on" autocorrect="off" autocomplete="off" 
                     input-class="form-control form-control-small"
                     class="col-lg-8"
                     ng-pattern="/^[A-Za-z]+$/"
                     docs-search-input-focus
                     >
   </div>

【讨论】:

    猜你喜欢
    • 2015-01-25
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 2017-10-15
    • 1970-01-01
    • 2019-08-01
    • 2015-01-18
    • 1970-01-01
    相关资源
    最近更新 更多