美文网首页
MongoDB Text Search

MongoDB Text Search

作者: JayIsAmazing | 来源:发表于2017-09-28 10:25 被阅读36次

简介

MongoDB支持一段内容的文本查询。为了执行这一运算,MongoDB使用了 text index$text
运算符。需要注意的是,Views视图不支持 text search。

例子

这一个例子将会展示如何创建一个 text index 和如何使用它去查找 ‘coffee shops’, 在给定的只有文本字段里。首先创建一个 collection stores

db.stores.insert(
   [
     { _id: 1, name: "Java Hut", description: "Coffee and cakes" },
     { _id: 2, name: "Burger Buns", description: "Gourmet hamburgers" },
     { _id: 3, name: "Coffee Shop", description: "Just coffee" },
     { _id: 4, name: "Clothes Clothes Clothes", description: "Discount clothing" },
     { _id: 5, name: "Java Shopping", description: "Indonesian goods" }
   ]
)

Text Index 文本索引

MongoDB 提供文本索引来支持文本查询请求在一串内容里,文本索引可以包含任意字段 只要它的值是一个string 或者 一个 string 元素的 array。
为了执行文本查询请求,你必须有一个文本索引在你的 collection里。一个 collection 只能有一个文本查询索引,但是那一个索引可以覆盖多个字段。
例如你可以运行下面的代码,可以操作文本查询在 namedescription 字段之上。

db.stores.createIndex( { name: "text", description: "text" } )

$text Operator

使用 $text 请求运算符去执行文本查询在一个 collection里通过文本索引。
直接用例子来解释了。
这个语句就是去查找所有stores包含任意关键词在[‘coffee’, 'shop','java']中

db.stores.find( { $text: { $search: "java coffee shop" } } )

精准短语

通过包装他们在双引号里

db.stores.find( { $text: { $search: "java \"coffee shop\"" } } )

排除某一项

在关键词前加 - 符号。只查找 “java” 和 “shop”,不包含 “coffee”

db.stores.find( { $text: { $search: "java shop -coffee" } } )

排序

MongoDB将会它的返还默认是未排序的。但是文本查询请求将会计算相关的评分对每一个 document 即指定 document 与请求匹配的程度。
要想按照评分去排序,你必须显性地指出 $meta textScore字段并且根据他们来排序。

db.stores.find(
   { $text: { $search: "java coffee shop" } },
   { score: { $meta: "textScore" } }
).sort( { score: { $meta: "textScore" } } )

Text search is also available in the aggregation pipeline.
原文:https://docs.mongodb.com/manual/text-search/

相关文章

  • 文本搜索

    To perform text search, MongoDB uses a text index and the...

  • MongoDB Text Search

    简介 MongoDB支持一段内容的文本查询。为了执行这一运算,MongoDB使用了 text index 和 $t...

  • Search Bars

    Search BarsA search bar provides an interface for text-ba...

  • MAMP环境安装mongodb扩展

    搜索可用的mongodb组件 brew search mongodb 根据本机php版本安装对应的mongodb ...

  • idea rename

    Search in comments and strings 在注释和字符串中搜索 search for text...

  • 一个查看兼容性的网站

    https://caniuse.com/#search=text-stroke

  • Free image search engine

    https://www.photosforclass.com/search?text=layout

  • Princeton Alogorithm COS226 Week

    substring search is to find a single string in text.patte...

  • Full Text Search

    全文搜索 1、相关性评价查询与其结果间的相关程度,并根据这种相关程度对结果排名。计算方式:TF/IDF、地理位置邻...

  • mongodb

    开始今天的MongoDB旅行吧 可以用homebrew安装mongodb,首先可以brew search mono...

网友评论

      本文标题:MongoDB Text Search

      本文链接:https://www.haomeiwen.com/subject/xsybsxtx.html