我想知道如何在 MongoDB 中实现位置倒排索引。通过使用多键功能可以创建倒排索引,但是如何才能有效地存储出现的位置呢?
假设我们有这个对象
obj = {
name: "Apollo",
text: "Some text about Apollo moon landings",
keywords: [ "some", "text", "about", "apollo", "moon", "landings" ]
}
我希望能够在“apollo”和“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 来检查输入关键字的相对位置。
关于java - MongoDB 中的位置倒排索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9055492/