如何使用 CB View 在 Couchbase 中实现搜索。我不想使用 Elasticsearch 进行搜索。根据值(value)搜索文档的最佳方法是什么?例如下面是我的文档。我想实现基于名称、城市、州和国家的搜索。
{
"_id": "location::370794",
"name": "Kenai Riverside Fishing",
"description": "Welcome to the Kenai Riverside Fishing program at Kenai Riverside Lodge, your destination for incredible Alaska fishing experiences. Kenai Riverside Fishing is part of Alaska Wildland Adventures, a long-time operator of guided fishing trips since 1977. Our trips explore the world-famous Kenai River for freshwater species, like king and sockeye salmon, rainbow trout and Dolly Varden. We also fish Resurrection Bay for halibut and silver salmon. Whether you are a seasoned angler or a first time fisherman, we’ll put you on the fish!",
"city": "Cooper Landing",
"state": "Alaska",
"country": "USA",
}
如何使用 couchbase View 实现此目的?
请您参考如下方法:
如果可以,请使用 N1QL
您是否研究过新发布的 Couchbase Server 4.0,特别是 N1QL
?
这是一种非常强大的查询语言(SQL 的超集),它允许您使用任何组合来查询文档,包括例如。 州
和城市
等...
您可以在最有可能查询的字段上设置二级索引以获得更好的性能,并且您可以获得很大的灵 active !
如果你真的想坚持自己的观点
要回答有关 View 的问题,如果您想要的是能够通过仅选择 4 个条件中的一个来进行查询,那么您需要做的是构建 4 个 View (每个条件一个)。每个 View 都会将文档映射到相应的条件。例如,对于国家/地区:
function (doc, meta) {
if (doc.country) {
emit(doc.country, null);
}
}
if
检查该字段是否确实存在。如果您的存储桶中有多种文档类型,也许您还想过滤足够的文档类型(例如,通过检查 _id
属性的前缀是否为 location::
在你的例子中)。
搜索只需从 4 个 View 中查询正确的 View ,提供要搜索的key
,例如。法国。注意:也许您想发出数据的 toUpperCase()
版本,以便搜索不区分大小写(查询时也必须将查找键大写)。
文档
有关使用 Couchbase 进行查询的一般信息(从开发人员的角度来看),请参阅 N1QL 上的开发人员指南。和 Views .
另请参阅documentation of your SDK了解如何用您喜欢的语言查询 View
和/或N1QL
。