gpt4 book ai didi

mongodb - 如何使用 Spring Data MongoDB 结合文本和地理查询?

转载 作者:可可西里 更新时间:2023-11-01 09:58:59 25 4
gpt4 key购买 nike

根据 Mongodb 文档 “查询不能同时使用文本和地理空间索引” 意味着我们不能同时使用 $textSearch$nearSphere在单个 Spring Data Mongo 存储库方法中。

但我正在寻找一些解决方法,它可以让我同时使用 TextCriterianearSphere Pint,我没有别的办法,我是真的想让这个工作。

我找到了 https://groups.google.com/forum/#!msg/mongodb-user/pzlYGKMYMVQ/O6P5S578Xx0J这表示他能够执行一些解决方法,但我不明白他是如何为后续查询编写 Repository 方法的?

find({"add.loc": {$near:{$geometry: {type: "Point",coordinates: 
[116.425, -31.09]}, $maxDistance: 500000}},$text:{$search: "hello"}}

我的处境很糟糕

对于我的存储库方法,它提供:

Page<User> getAddress_PositionNear(TextCriteria tc,Point gpoint, Distance d, Pageable p);

"errmsg" : "text and geoNear not allowed in same query" , "code" : 2

最佳答案

在一个查询中组合 2 个特殊索引结构(在撰写本文时)不可能使用 MongoDB。这意味着文本索引不能与地理空间索引一起使用。
$near需要对 2d 或 2dsphere 索引进行操作,因此不能与文本搜索相结合。

但是可以使用 $geoWithin不需要地理空间索引。使用 PolygonCircle 来构造类似

的查询
db.text_and_geo.find({
"loc": { "$geoWithin" : { "$geometry": { "type" : "Polygon" , "coordinates": [ [ [ 0, 0 ], [ 3, 6 ], [ 6, 1 ], [ 0, 0 ] ] ] }}},
"$text" : { "$search" : "spring"}
})

db.text_and_geo.find({
"loc": { "$geoWithin": { "$center": [ [1, 1], 10 ] } } ,
"$text" : { "$search" : "spring"}
})

几乎可以通过 MongoTemplate

完成相同的操作
Query queryWithPolygon = TextQuery.queryText(TextCriteria.forDefaultLanguage()
.matching("spring"))
.addCriteria(
where("loc")
.within(new GeoJsonPolygon(new Point(0, 0), new Point(3, 6), new Point(6, 1), new Point(0, 0)));
List<TextAndGeo> result = template.find(queryWithPolygon, TextAndGeo.class);

Query queryWithCircle = TextQuery.queryText(TextCriteria.forDefaultLanguage()
.matching("spring"))
.addCriteria(
where("loc")
.within(new Circle(new Point(0, 0), 10));
List<TextAndGeo> result = template.find(queryWithCircle, TextAndGeo.class);

使用 Repository 只需为您的派生查询使用 Within 关键字

Page<TextAndGeo> findByLocWithin(Circle circle, TextCriteria tc, Pageable page)

关于mongodb - 如何使用 Spring Data MongoDB 结合文本和地理查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33156106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com