具有多个字段的MongoDB文本搜索重复结果集

时间:2016-06-15 12:44:57

标签: mongodb mongodb-query text-search

我的产品系列上有文字索引。该索引使用3个不同权重的列,如下所示。当我搜索让我们说“苹果”时,我看到同一产品多次返回。它不仅适用于一种产品,大多数产品都是重复的。可能是什么原因造成的?我需要重新索引我的收藏吗? 感谢

{
    "v": 1,
    "key": {
        "_fts": "text",
        "_ftsx": 1
    },
    "name": "searchCollection",
    "ns": "local.product",
    "weights": {
        "categoryList": 3,
        "productInformation": 1,
        "title": 5
    },
    "default_language": "english",
    "language_override": "language",
    "textIndexVersion": 2
}

结果如下:

/* 0 */
{
    "_id": ObjectId("56b122913fa3dac80849fa54"),
    "title": "Apple - Apple TV - Black",
    "urlTitle": "apple---apple-tv---black",
    "price": 75.0,
    "imageList": ["https://test.s3.amazonaws.com/test.png"],
    "boxPrice": 1.0,
    "upcCode": "IBXBB4854433",
    "categoryList": "electronics,televisions & home theater,home theater accessories,apple home theater accessories,Apple,",
    "productInformation": "<div class=\"feature\">\r\n<h4>1080p capability</h4>\r\n<p>Watch high-resolution movies and TV shows from the iTunes Store; view videos and photos from the Internet and your Mac or PC in crisp 1080p.</p>\r\n</div></div>",
    "productSpecification": "&lt;p&gt;Get access to a world of instant entertainment with this product. Just connect to the Internet and stream movies, listen to music, and access a wide variety of other content.&lt;/p&gt;",
    "user": "1",
    "isPromoted": false,
    "createdAt": ISODate("2016-02-02T21:41:37.508Z"),
    "updatedAt": ISODate("2016-06-15T02:52:39.761Z"),
    "weight": "2",
    "productOptions": null,
    "hiddenPrice": "69.99",
    "viewCount": 8.0
}

/* 1 */
{
    "_id": "56b122913fa3dac80849fa54",
    "title": "Apple - Apple TV - Black",
    "urlTitle": "apple---apple-tv---black",
    "price": 75.0,
    "imageList": ["https://test.s3.amazonaws.com/test.png"],
    "boxPrice": 1.0,
    "upcCode": "IBXBB4854433",
    "categoryList": "electronics,televisions & home theater,home theater accessories,apple home theater accessories,Apple,",
    "productInformation": "<div class=\"feature\">\r\n<h4>1080p capability</h4>\r\n<p>Watch high-resolution movies and TV shows from the iTunes Store; view videos and photos from the Internet and your Mac or PC in crisp 1080p.</p>\r\n</div></div>",
    "productSpecification": "&lt;p&gt;Get access to a world of instant entertainment with this product. Just connect to the Internet and stream movies, listen to music, and access a wide variety of other content.&lt;/p&gt;",
    "user": "1",
    "isPromoted": false,
    "createdAt": ISODate("2016-02-02T21:41:37.508Z"),
    "updatedAt": ISODate("2016-03-30T14:33:42.731Z"),
    "weight": "2",
    "productOptions": null,
    "hiddenPrice": "69.99",
    "viewCount": 1.0,
    "score": 9.231043781725889
}

/* 2 */
{
    "_id": ObjectId("57028d1d610bb00300e8b293"),
    "user": null,
    "title": "Apple Wired Mouse",
    "urlTitle": "apple-wired-mouse",
    "categoryList": "electronics,computers,computer accessories,apple computer accessories,Apple,",
    "boxPrice": 1.0,
    "imageList": ["https://test.s3.amazonaws.com/test.jpg"],
    "price": 47.0,
    "weight": "1",
    "productInformation": "<p style=\"color: rgb(0, 0, 0);background-color: rgb(255, 255, 255);\">It’s the wired mouse that reinvents the wheel — t</ul>",
    "productOptions": null,
    "hiddenPrice": "44",
    "isPromoted": false,
    "upcCode": "MB112LLB",
    "createdAt": ISODate("2016-04-04T15:49:49.760Z"),
    "updatedAt": ISODate("2016-06-14T13:15:11.174Z"),
    "viewCount": 1.0
}

/* 3 */
{
    "_id": "57028d1d610bb00300e8b293",
    "user": null,
    "title": "Apple Wired Mouse",
    "urlTitle": "apple-wired-mouse",
    "categoryList": "electronics,computers,computer accessories,apple computer accessories,Apple,",
    "boxPrice": 1.0,
    "imageList": ["https://test.s3.amazonaws.com/test.jpg"],
    "price": 47.0,
    "weight": "1",
    "productInformation": "<p style=\"color: rgb(0, 0, 0);background-color: rgb(255, 255, 255);\">It’s the wired mouse that reinvents the wheel</ul>",
    "productOptions": null,
    "hiddenPrice": "44",
    "isPromoted": false,
    "upcCode": "MB112LLB",
    "createdAt": ISODate("2016-04-04T15:49:49.760Z"),
    "updatedAt": ISODate("2016-04-07T13:22:09.108Z"),
    "score": 7.170701054107839
}

/* 4 */
{
    "_id": ObjectId("57028390610bb00300e8b286"),
    "user": null,
    "title": "Nomad Silver Stand Apple Watch Charger",
    "urlTitle": "nomad-silver-stand-apple-watch-charger",
    "categoryList": "watches,womens smart watches,mens smart watches,computer accessories,apple womens smart watches,apple mens smart watches,apple computer accessories,nomad womens smart watches,nomad mens smart watches,nomad computer accessories,Apple,Nomad,",
    "boxPrice": 1.0,
    "imageList": ["https://test.s3.amazonaws.com/test.jpg"],
    "price": 39.0,
    "weight": "1",
    "productInformation": "<p style=\"color: rgb(0, 0, 0);background-color: rgb(255, 255, 255);\">Keep your Apple Watch safe and secure while </ul>",
    "productOptions": null,
    "hiddenPrice": "30",
    "isPromoted": false,
    "upcCode": "STAND-APPLE-S-001",
    "createdAt": ISODate("2016-04-04T15:09:04.884Z"),
    "updatedAt": ISODate("2016-06-14T12:43:10.327Z"),
    "viewCount": 1.0
}

1 个答案:

答案 0 :(得分:0)

问题是由sailsjs使用.native()引起的。当我使用它时,系统不会返回模型,而是返回一个数组。由于该数组中的对象没有将其id字段指定为对象(objectId),因此系统将它们视为新产品,并在将记录插入相关文档(购物车)之前将它们插入到产品集合中。使用字符串ID删除产品文档并在应用程序中添加额外的id字段检查以解决问题。