文本索引MongoDB,最小搜索字符串长度 [英] Text Indexes MongoDB, Minimum length of search string

查看:230
本文介绍了文本索引MongoDB,最小搜索字符串长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从mongo shell中为集合X创建了文本索引

I have created a text index for collection X from mongo shell

db.X.ensureIndex({name: 'text', cusines: 'text', 'address.city': 'text'})

现在,如果其name属性的值具有 seasons (季节)的文档,则其长度为7

now if a document whose name property has a value seasons, its length is 7

所以如果我运行查找查询(搜索字符串的长度为< = 5)

so if I run the find query(with a search string of length <= 5)

db.X.find({$text: {$search: 'seaso'}})

如果我将搜索字符串更改为 season (长度> = 6),则不会返回任何值,然后它将返回文档.

it does not return any value if I change the search string to season (length >= 6) then it returns the document.

现在我的问题是搜索字符串是否具有某些最小长度限制以获取记录. 如果是,那么有什么办法可以改变它?

Now my question is does the search string has some minimum length constraint to fetch the records. if yes, then is there is any way to change it?

推荐答案

MongoDB $text搜索不支持部分匹配. MongoDB允许对字符串内容进行文本搜索查询,并支持不区分大小写和阻止.

MongoDB $text searches do not support partial matching. MongoDB allows support text search queries on string content with support for case insensitivity and stemming.

查看您的示例:

// this returns nothing because there is no inferred association between 
// the value: 'seasons' and your input: 'seaso'
db.X.find({$text: {$search: 'seaso'}})

// this returns a match because 'season' is seen as a stem of 'seasons'
db.X.find({$text: {$search: 'season'}})

因此,这与输入时间无关.在seaso上搜索未返回任何匹配项,因为:

So, this is not an inssue with the length of your input. Searching on seaso returns no matches because:

  • 您的文本索引不包含整个单词:seaso
  • 您的文本索引不包含seaso是公认词干的整个单词
  • Your text index does not contain the whole word: seaso
  • Your text index does not contain a whole word for which seaso is a recognised stem

这假定您的文本索引的语言是英语,您可以通过运行db.X.getIndexes()进行确认,然后您会在文本索引的定义中看到这一点:

This presumes that the language of your text index is English, You can confirm this by runing db.X.getIndexes() and you'll see this in the definition of your text index:

"default_language" : "english"

FWIW,如果您的索引不区分大小写,则以下内容也将返回匹配项:

FWIW, if your index is case insensitive then the following will also return matches:

db.X.find({$text: {$search: 'SEaSON'}})
db.X.find({$text: {$search: 'SEASONs'}})

更新1:,回答了这个问题是否可以使用RegExp" .

假设name属性包含值seasons,并且您正在搜索seaso,则以下内容将与您的文档匹配:

Assuming the name attribute contains the value seasons and you are seaching with seaso then the following will match your document:

db.X.find({type: {$regex: /^seaso/}})

文档中的更多详细信息,但...

More details in the docs but ...

  • 这将使用您的文本索引,因此,如果继续使用$regex运算符,则将不需要文本索引.
  • 使用$regex运算符的索引覆盖范围可能不是您期望的,因此摘要如下:如果您的搜索值是固定的(即seaso,而不是easons),则MongoDB可以使用索引,但否则不能.
  • This will not use your text index so if you proceeed with using the $regex operator then you won't need the text index.
  • Index coverage with the $regex operator is probably not what you expect, the brief summary is this: if your search value is anchored (i.e. seaso, rather than easons) then MongoDB can use an index but otherwise it cannot.

这篇关于文本索引MongoDB,最小搜索字符串长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆