文本搜索行为

时间:2017-01-29 09:09:41

标签: javascript node.js mongodb mongoose

我使用MONGOOSE应用程序在我的NODEJS中实现了MongoDB文本搜索,如果我搜索某些内容,那么结果有时对我来说没有用。

有人可以了解一下文字搜索的行为吗? 我做错了什么?

ie.i的名字是" Franz" - 我搜索" Franz" - 没有结果 我的名字是"你是英雄" - 我搜索"英雄 - 找到它 对于其他字段也是如此 - 有时它会找到条目,有时候不是

CommentSchema.index({name:'text',shortDescription:'text',longDescription:'text',"topics.text":'text',geoAreaGeneral:'text',geoAreaSpecific:'text'});

顺便说一句:主题的索引(由topics: [{}]定义)不起作用 - 如何让它工作?

这是我的代码:

  var queryParamFind = {
    "addedDate" : {$lt:dateTimeISO}, 
    'deleted': {$ne:true}
  };

    if(order.includes("addedDate"))
    {queryParamOrder = {"addedDate" : -1}}
    if(order.includes("featured"))
    {queryParamOrder = {"featured" : -1}}
    if(order.includes("upvotes"))
    {queryParamOrder = {"upvotes" : -1}}
    else
    {queryParamOrder = {"addedDate" : -1}}

    if(searchPhrase != 'undefined'){
      if (searchPhrase.length==0)
      {}
      else
      {
      queryParamOrder= { "score": { "$meta": "textScore" } };
      queryParamFind= { "$text": { "$search": searchPhrase  }, 'deleted': {$ne:true}};
      queryParamSelect = { "score": { "$meta": "textScore" } };   
      }

    };

  Comment.find(queryParamFind)
        .select(queryParamSelect)
        .populate("addedBy")
        .sort(queryParamOrder)
        .limit(page * onePage)
        .exec(function(err, result){
    if(err){ return next(err); }
    res.json(result);
  });
例如

{
    "_id": ObjectId("588dd5be93ca1b09003cf41a"),
    "number": NumberInt(1015),
    "addedBy": ObjectId("586e17f808612e10dcdcdc35"),
    "name": "This is a new comment",
    "shortDescription": "Please find me",
    "longDescription": "Please find me",
    "geoAreaGeneral": "Europe",
    "geoAreaSpecific": "Specific geo area",
    "creationDate": "2222-12-29T23:00:00.000Z",
    "updatedDate": ISODate("2017-01-29T11:45:02.585+0000"),
    "addedDate": ISODate("2017-01-29T11:45:02.585+0000"),
    "topics": [{
        "text": "Tag1"
    }, {
        "text": "Tag2"
    }],
    "__v": NumberInt(0)
} {
    "_id": ObjectId("588dd5df93ca1b09003cf41b"),
    "number": NumberInt(1016),
    "addedBy": ObjectId("586e17f808612e10dcdcdc35"),
    "name": "Hello my name is",
    "shortDescription": "Hello my name is",
    "longDescription": "Hello my name is",
    "geoAreaGeneral": "North America",
    "creationDate": "2222-02-11T23:00:00.000Z",
    "upvotes": NumberInt(0),
    "clicksCount": NumberInt(0),
    "updatedCount": NumberInt(0),
    "updatedDate": ISODate("2017-01-29T11:45:35.343+0000"),
    "addedDate": ISODate("2017-01-29T11:45:35.343+0000"),
    "topics": [{
        "text": "Tag4"
    }, {
        "text": "Tag1000"
    }],
    "__v": NumberInt(0)
} {
    "_id": ObjectId("588dd62093ca1b09003cf41c"),
    "number": NumberInt(1017),
    "addedBy": ObjectId("586e17f808612e10dcdcdc35"),
    "name": "My name is Hello",
    "shortDescription": "Fiind me via my short description",
    "longDescription": "Fiind me via my long description",
    "geoAreaGeneral": "North America",
    "updatedDate": ISODate("2017-01-29T11:46:40.956+0000"),
    "addedDate": ISODate("2017-01-29T11:46:40.956+0000"),
    "topics": [{
        "text": "Tag3000"
    }],
    "__v": NumberInt(0)
}

当我搜索"你好" - 我只看到"你好我的名字是"条目

当我搜索"评论" - 没什么

当我搜索"请找到" - 发现"这是一个新评论"条目

当我搜索" Fiind" - 发现"我的名字是你好"

当我搜索" North" - 没什么

获取索引:

> [
>         {
>                 "v" : 2,
>                 "key" : {
>                         "_id" : 1
>                 },
>                 "name" : "_id_",
>                 "ns" : "main.comments"
>         },
>         {
>                 "v" : 2,
>                 "unique" : true,
>                 "key" : {
>                         "number" : 1
>                 },
>                 "name" : "number_1",
>                 "ns" : "main.comments",
>                 "background" : true
>         },
>         {
>                 "v" : 2,
>                 "key" : {
>                         "_fts" : "text",
>                         "_ftsx" : 1
>                 },
>                 "name" : "shortDescription_text_longDescription_text_topics_text",
>                 "ns" : "main.comments",
>                 "background" : true,
>                 "weights" : {
>                         "longDescription" : 1,
>                         "shortDescription" : 1,
>                         "topics" : 1
>                 },
>                 "default_language" : "english",
>                 "language_override" : "language",
>                 "textIndexVersion" : 3
>         } ]
> 

0 个答案:

没有答案