Java MongoDB中的位置反转索引
我想知道如何在MongoDB中实现位置反转索引。通过使用多键功能,可以创建反向索引,但是如何能够以有效的方式存储事件的位置呢 假设我们有这个物体Java MongoDB中的位置反转索引,java,mongodb,indexing,Java,Mongodb,Indexing,我想知道如何在MongoDB中实现位置反转索引。通过使用多键功能,可以创建反向索引,但是如何能够以有效的方式存储事件的位置呢 假设我们有这个物体 obj = { name: "Apollo", text: "Some text about Apollo moon landings", keywords: [ "some", "text", "about", "apollo", "moon", "landings" ] } 我希望能够查询“阿波罗”和“着陆点”必须连接的位置,而不仅仅
obj = {
name: "Apollo",
text: "Some text about Apollo moon landings",
keywords: [ "some", "text", "about", "apollo", "moon", "landings" ]
}
我希望能够查询“阿波罗”和“着陆点”必须连接的位置,而不仅仅是进行“交叉点”查询。那么像这样的对象呢:
obj = {
name: "Apollo",
text: "Some text about Apollo moon landings",
keywords: [
{idx:0, text: "some"},
{idx:1, text: "text"},
{idx:2, text: "about"},
{idx:3, text: "apollo"},
{idx:4, text: "moon"},
{idx:5, text: "landings"}
]
}
您可以对“keywords.text”执行ensureIndex,以在两个关键字都存在的情况下执行查询,然后在“where”过滤器中使用javascript检查输入关键字的相对位置。类似以下对象的情况如何:
obj = {
name: "Apollo",
text: "Some text about Apollo moon landings",
keywords: [
{idx:0, text: "some"},
{idx:1, text: "text"},
{idx:2, text: "about"},
{idx:3, text: "apollo"},
{idx:4, text: "moon"},
{idx:5, text: "landings"}
]
}
您可以对“keywords.text”执行ensureIndex,以在两个关键字都存在的情况下执行查询,然后在“where”过滤器中使用javascript检查输入关键字的相对位置。您可以使用$and或$all操作符来完成我认为您希望完成的任务 根据您的示例文档:
> db.test.find().pretty()
{
"_id" : ObjectId("4f26b716c27b085280a45a29"),
"name" : "Apollo",
"text" : "Some text about Apollo moon landings",
"keywords" : [
"some",
"text",
"about",
"apollo",
"moon",
"landings"
]
}
您可以使用$and运算符搜索“关键字”数组同时包含这两个单词的文档
> db.test.find({$and:[{keywords:"apollo"}, {keywords:"landings"}]})
{ "_id" : ObjectId("4f26b716c27b085280a45a29"), "name" : "Apollo", "text" : "Some text about Apollo moon landings", "keywords" : [ "some", "text", "about", "apollo", "moon", "landings" ] }
>
$all运算符将返回相同的结果,并且查询更加简化:
> db.test.find({keywords:{$all:["apollo", "landings"]}})
{ "_id" : ObjectId("4f26b716c27b085280a45a29"), "name" : "Apollo", "text" : "Some text about Apollo moon landings", "keywords" : [ "some", "text", "about", "apollo", "moon", "landings" ] }
如果我们在关键字数组上放置索引,两个查询都会使用它
> db.test.ensureIndex({keywords:1})
> db.test.find({$and:[{keywords:"apollo"}, {keywords:"landings"}]}).explain()
{
"cursor" : "BtreeCursor keywords_1",
"nscanned" : 1,
"nscannedObjects" : 1,
"n" : 1,
"millis" : 0,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : true,
"indexOnly" : false,
"indexBounds" : {
"keywords" : [
[
"apollo",
"apollo"
]
]
}
}
> db.test.find({keywords:{$all:["apollo", "landings"]}}).explain()
{
"cursor" : "BtreeCursor keywords_1",
"nscanned" : 1,
"nscannedObjects" : 1,
"n" : 1,
"millis" : 0,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : true,
"indexOnly" : false,
"indexBounds" : {
"keywords" : [
[
"apollo",
"apollo"
]
]
}
}
>
两个查询都使用关键字索引
有关不同类型查询的更多信息,请参阅“高级查询”文档。有关Mongo中索引工作原理的更多信息,请参阅“索引”文档。
“索引数组元素”部分链接到有关多键的文档。 如果您不熟悉mongodb的.explain函数,请在此处进行说明: 简而言之,它显示查询正在使用的任何索引,以及需要访问多少文档才能返回相关文档 最后,您的问题似乎与另一位用户的问题类似,他在今天上午早些时候询问在数组中搜索值的问题。也许这也与您有关。
希望这能帮助您编写所需的查询。如果您有任何后续问题,请告知我们 您可以使用$and或$all操作符来完成我认为您希望完成的任务 根据您的示例文档:
> db.test.find().pretty()
{
"_id" : ObjectId("4f26b716c27b085280a45a29"),
"name" : "Apollo",
"text" : "Some text about Apollo moon landings",
"keywords" : [
"some",
"text",
"about",
"apollo",
"moon",
"landings"
]
}
您可以使用$and运算符搜索“关键字”数组同时包含这两个单词的文档
> db.test.find({$and:[{keywords:"apollo"}, {keywords:"landings"}]})
{ "_id" : ObjectId("4f26b716c27b085280a45a29"), "name" : "Apollo", "text" : "Some text about Apollo moon landings", "keywords" : [ "some", "text", "about", "apollo", "moon", "landings" ] }
>
$all运算符将返回相同的结果,并且查询更加简化:
> db.test.find({keywords:{$all:["apollo", "landings"]}})
{ "_id" : ObjectId("4f26b716c27b085280a45a29"), "name" : "Apollo", "text" : "Some text about Apollo moon landings", "keywords" : [ "some", "text", "about", "apollo", "moon", "landings" ] }
如果我们在关键字数组上放置索引,两个查询都会使用它
> db.test.ensureIndex({keywords:1})
> db.test.find({$and:[{keywords:"apollo"}, {keywords:"landings"}]}).explain()
{
"cursor" : "BtreeCursor keywords_1",
"nscanned" : 1,
"nscannedObjects" : 1,
"n" : 1,
"millis" : 0,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : true,
"indexOnly" : false,
"indexBounds" : {
"keywords" : [
[
"apollo",
"apollo"
]
]
}
}
> db.test.find({keywords:{$all:["apollo", "landings"]}}).explain()
{
"cursor" : "BtreeCursor keywords_1",
"nscanned" : 1,
"nscannedObjects" : 1,
"n" : 1,
"millis" : 0,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : true,
"indexOnly" : false,
"indexBounds" : {
"keywords" : [
[
"apollo",
"apollo"
]
]
}
}
>
两个查询都使用关键字索引
有关不同类型查询的更多信息,请参阅“高级查询”文档。有关Mongo中索引工作原理的更多信息,请参阅“索引”文档。
“索引数组元素”部分链接到有关多键的文档。 如果您不熟悉mongodb的.explain函数,请在此处进行说明: 简而言之,它显示查询正在使用的任何索引,以及需要访问多少文档才能返回相关文档 最后,您的问题似乎与另一位用户的问题类似,他在今天上午早些时候询问在数组中搜索值的问题。也许这也与您有关。
希望这能帮助您编写所需的查询。如果您有任何后续问题,请告知我们 我的问题可能写得不好,但我想解决的问题是,查询应该只返回“apollo”和“landings”相邻的结果,例如在措辞严谨的查询“apollo landings”中。如果我有一个带有“apollo xxxx landings”的文本,那么查询不应该作为结果返回它,因为它不是一个短语。有可能进行这样的查询吗?我的问题可能写得不好,但我想解决的问题是,查询应该只返回“apollo”和“landings”相邻的结果,例如在短语化的查询“apollo landings”中。如果我有一个带有“apollo xxxx landings”的文本,那么查询不应该作为结果返回它,因为它不是一个短语。有没有可能提出这样的疑问?