在 Mongodb 中,创建文本索引后,使用文本过滤器进行查询时没有任何输出显示
In Mongodb, after create the text index, when query using text filter there is no any output showing
在Mongodb中,我创建了如下合集。
db.test.insertMany([
{CustomerKey : "11026", FirstName : "Harold", LastName : "Sai", BirthDate : new Date("1951-10-1"),MaritalStatus : "S", Gender : "M", EmailAddress : "harold3@adventure-works.com", YearlyIncome : 30000, TotalChildren : 2, NumberChildrenAtHome : 0, EnglishEducation : "Partial College", EnglishOccupation : "Clerical", NumberCarsOwned : 2, AddressLine1 : {House_No : 2596, Area_Name: "Franklin Canyon Road"}, Phone : "1 (11) 500 555-0131", DateFirstPurchase : new Date("2011-10-1"), CommuteDistance : "1-2 Miles"} ,
{CustomerKey : "11027", FirstName : "Jessie", LastName : "Zhao", BirthDate : new Date("1952-6-5"),MaritalStatus : "M", Gender : "M", EmailAddress : "jessie16@adventure-works.com", YearlyIncome : 30000, TotalChildren : 2, NumberChildrenAtHome : 0, EnglishEducation : "Partial College", EnglishOccupation : "Clerical", NumberCarsOwned : 2, AddressLine1 : {House_No : 8211, Area_Name: "Leeds Ct."}, Phone : "1 (11) 500 555-0184", DateFirstPurchase : new Date("2011-6-1"), CommuteDistance : "5-10 Miles"} ,
{CustomerKey : "11028", FirstName : "Jill", LastName : "Jimenez", BirthDate : new Date("1951-10-9"),MaritalStatus : "M", Gender : "F", EmailAddress : "jill13@adventure-works.com", YearlyIncome : 30000, TotalChildren : 2, NumberChildrenAtHome : 0, EnglishEducation : "Partial College", EnglishOccupation : "Clerical", NumberCarsOwned : 2, AddressLine1 : {House_No : 213, Area_Name: "Valencia Place"}, Phone : "1 (11) 500 555-0116", DateFirstPurchase : new Date("2011-10-1"), CommuteDistance : "1-2 Miles"} ,
]);
以下是查询的输出:(Harold 可用的电子邮件地址)
我已将“EmailAddress”字段设置为文本索引。
db.test.createIndex({EmailAddress : "text"})
但是当我使用下面的代码查询时,没有任何文本过滤器的输出。
db.test.find({$text:{$search:"harold"}})
你要找的是
db.test.find({"EmailAddress":{"$regex":"harold"}})
因为您正在寻找某种模式匹配。
文本索引通过删除停用词、用词干替换词等以标记化形式存储字段
您可以在此处阅读更多相关信息 https://docs.mongodb.com/manual/text-search/#-text-operator
正则表达式运算符及其索引使用:https://docs.mongodb.com/manual/reference/operator/query/regex/
文本索引不支持部分单词匹配。他们应该在句子中找到整个单词。在您的示例中, harold
被视为单词 harold3@adventure-works.com
的一部分,因此您正在尝试执行部分单词匹配。将以下文档作为测试用例...
db.test.insert({
"CustomerKey" : "11026",
"FirstName" : "Harold",
"LastName" : "Sai",
"BirthDate" : ISODate("1951-10-01T00:00:00Z"),
"MaritalStatus" : "S",
"Gender" : "M",
"EmailAddress" : "harold is at harold3@adventure-works.com",
"YearlyIncome" : 30000,
"TotalChildren" : 2,
"NumberChildrenAtHome" : 0,
"EnglishEducation" : "Partial College",
"EnglishOccupation" : "Clerical",
"NumberCarsOwned" : 2,
"AddressLine1" : {
"House_No" : 2596,
"Area_Name" : "Franklin Canyon Road"
},
"Phone" : "1 (11) 500 555-0131",
"DateFirstPurchase" : ISODate("2011-10-01T00:00:00Z"),
"CommuteDistance" : "1-2 Miles"
})
...现在,您的原始查询会找到它,因为在字段 EmailAddress
.
中找到了整个单词 harold
虽然文本索引不支持部分单词匹配,但它们将允许词干提取。例如,如果您搜索 运行,它将找到 运行ning.
另一种选择是使用 MongoDB Atlas。 Atlas 支持基于 Apache Lucene 的搜索索引,提供部分和模糊匹配功能。
有关类似 SO 文章的另一参考,请参阅 。
在Mongodb中,我创建了如下合集。
db.test.insertMany([
{CustomerKey : "11026", FirstName : "Harold", LastName : "Sai", BirthDate : new Date("1951-10-1"),MaritalStatus : "S", Gender : "M", EmailAddress : "harold3@adventure-works.com", YearlyIncome : 30000, TotalChildren : 2, NumberChildrenAtHome : 0, EnglishEducation : "Partial College", EnglishOccupation : "Clerical", NumberCarsOwned : 2, AddressLine1 : {House_No : 2596, Area_Name: "Franklin Canyon Road"}, Phone : "1 (11) 500 555-0131", DateFirstPurchase : new Date("2011-10-1"), CommuteDistance : "1-2 Miles"} ,
{CustomerKey : "11027", FirstName : "Jessie", LastName : "Zhao", BirthDate : new Date("1952-6-5"),MaritalStatus : "M", Gender : "M", EmailAddress : "jessie16@adventure-works.com", YearlyIncome : 30000, TotalChildren : 2, NumberChildrenAtHome : 0, EnglishEducation : "Partial College", EnglishOccupation : "Clerical", NumberCarsOwned : 2, AddressLine1 : {House_No : 8211, Area_Name: "Leeds Ct."}, Phone : "1 (11) 500 555-0184", DateFirstPurchase : new Date("2011-6-1"), CommuteDistance : "5-10 Miles"} ,
{CustomerKey : "11028", FirstName : "Jill", LastName : "Jimenez", BirthDate : new Date("1951-10-9"),MaritalStatus : "M", Gender : "F", EmailAddress : "jill13@adventure-works.com", YearlyIncome : 30000, TotalChildren : 2, NumberChildrenAtHome : 0, EnglishEducation : "Partial College", EnglishOccupation : "Clerical", NumberCarsOwned : 2, AddressLine1 : {House_No : 213, Area_Name: "Valencia Place"}, Phone : "1 (11) 500 555-0116", DateFirstPurchase : new Date("2011-10-1"), CommuteDistance : "1-2 Miles"} ,
]);
以下是查询的输出:(Harold 可用的电子邮件地址)
我已将“EmailAddress”字段设置为文本索引。
db.test.createIndex({EmailAddress : "text"})
但是当我使用下面的代码查询时,没有任何文本过滤器的输出。
db.test.find({$text:{$search:"harold"}})
你要找的是
db.test.find({"EmailAddress":{"$regex":"harold"}})
因为您正在寻找某种模式匹配。
文本索引通过删除停用词、用词干替换词等以标记化形式存储字段
您可以在此处阅读更多相关信息 https://docs.mongodb.com/manual/text-search/#-text-operator
正则表达式运算符及其索引使用:https://docs.mongodb.com/manual/reference/operator/query/regex/
文本索引不支持部分单词匹配。他们应该在句子中找到整个单词。在您的示例中, harold
被视为单词 harold3@adventure-works.com
的一部分,因此您正在尝试执行部分单词匹配。将以下文档作为测试用例...
db.test.insert({
"CustomerKey" : "11026",
"FirstName" : "Harold",
"LastName" : "Sai",
"BirthDate" : ISODate("1951-10-01T00:00:00Z"),
"MaritalStatus" : "S",
"Gender" : "M",
"EmailAddress" : "harold is at harold3@adventure-works.com",
"YearlyIncome" : 30000,
"TotalChildren" : 2,
"NumberChildrenAtHome" : 0,
"EnglishEducation" : "Partial College",
"EnglishOccupation" : "Clerical",
"NumberCarsOwned" : 2,
"AddressLine1" : {
"House_No" : 2596,
"Area_Name" : "Franklin Canyon Road"
},
"Phone" : "1 (11) 500 555-0131",
"DateFirstPurchase" : ISODate("2011-10-01T00:00:00Z"),
"CommuteDistance" : "1-2 Miles"
})
...现在,您的原始查询会找到它,因为在字段 EmailAddress
.
虽然文本索引不支持部分单词匹配,但它们将允许词干提取。例如,如果您搜索 运行,它将找到 运行ning.
另一种选择是使用 MongoDB Atlas。 Atlas 支持基于 Apache Lucene 的搜索索引,提供部分和模糊匹配功能。
有关类似 SO 文章的另一参考,请参阅