美文网首页
文本搜索

文本搜索

作者: 北海北_6dc3 | 来源:发表于2020-05-21 17:20 被阅读0次

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

插入用例数据

> 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" }
...    ]
... )
BulkWriteResult({
    "writeErrors" : [ ],
    "writeConcernErrors" : [ ],
    "nInserted" : 5,
    "nUpserted" : 0,
    "nMatched" : 0,
    "nModified" : 0,
    "nRemoved" : 0,
    "upserted" : [ ]
})

文字索引

MongoDB provides text indexes to support text search queries on string content. text indexes can include any field whose value is a string or an array of string elements.

To perform text search queries, you must have a text index on your collection. A collection can only have one text search index, but that index can cover multiple fields.

For example you can run the following in a mongo shell to allow text search over the name and descriptionfields:

> db.stores.createIndex( { name: "text", description: "text" } )
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 2,
    "ok" : 1
}
> 

$text运算符

使用$text查询运算符对具有文本索引的集合执行文本搜索。

$text将使用空格和大多数标点符号作为分隔符来OR标记搜索字符串,并对搜索字符串中的所有此类标记执行逻辑运算。

例如,您可以使用以下查询从列表“ coffee”,“ shop”和“ java”中查找包含任何术语的所有商店:

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

精确匹配

您还可以通过将它们括在双引号中来搜索确切的短语
以下将查找包含“coffee shop”的所有文档:

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

排除单词

要排除一个单词,可以在前面加上一个“ -”字符。例如,要查找所有包含“ java”或“ shop”但不包含“ coffee”的商店,请使用以下命令:

> db.stores.find( { $text: { $search: "java shop -coffee" } } )
{ "_id" : 5, "name" : "Java Shopping", "description" : "Indonesian goods" }

排序

默认情况下,MongoDB将以未排序的顺序返回结果。但是,文本搜索查询将为每个文档计算相关性分数,以指定文档与查询的匹配程度。
要按相关性得分的顺序对结果进行排序,必须显式投影该字段并对其进行排序:$meta textScore field

> db.stores.find(
...    { $text: { $search: "java coffee shop" } },
...    { score: { $meta: "textScore" } }
... ).sort( { score: { $meta: "textScore" } } )
{ "_id" : 3, "name" : "Coffee Shop", "description" : "Just coffee", "score" : 2.25 }
{ "_id" : 1, "name" : "Java Hut", "description" : "Coffee and cakes", "score" : 1.5 }
{ "_id" : 5, "name" : "Java Shopping", "description" : "Indonesian goods", "score" : 1.5 }
> 

读取关注

相关文章

  • 文本搜索

    搜索包含关键字的行 会将所有包含关键字的行均输出,有时候只需要输出匹配的最后几行: 当然,我们可以在grep 后面...

  • 文本搜索

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

  • 本地工具

    文本编辑器:EditPlus 文本编辑器,常用于文本编辑、转码、搜索、Hex查看。尤其是文本搜索,可以查询所有子文...

  • mongodb文本搜索

    这几天做app,绕不开搜索,在没有看mongodb文档的情况下,胡思乱想了半个小时该怎么处理文本搜索,没想到官网对...

  • 全文本搜索

    全文本搜索 使用全文本(fulltext)搜索,MySQL不需要分别查看每个行,不需要分析和处理每个词。MySQL...

  • Mongodb文本搜索

    近日用mongodb做文本搜索遇到一个问题。针对国外的人的姓名搜索,之前的中文名完全匹配的搜索不能用了。 比如我们...

  • Linux命令 - grep

    grep是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来. 1、基本用法,在文本中搜索一...

  • iOS --- searchBar模糊搜索

    模糊搜索的实现思路:当搜索框开始编辑时对搜索框中的文本与后台给的资源相对比,包含搜索文本的展示在tableview...

  • Linux文本搜索:grep

    文本搜索:grep Linux系统中grep命令是一种强大的文本搜索工具,grep允许对文本文件进行模式查找,如果...

  • NLP

    本地搜索 文本匹配, 与 文本 转化为 声音 匹配。 与 语音识别翻译 ML:搜索识别, 语音识别,文字识别,图像...

网友评论

      本文标题:文本搜索

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