如何使用 Spring 数据 MongoDB 组合文本和地理查询?
How to combine text and geo query using Spring Data MongoDB?
根据 Mongodb 文档 "Queries cannot use both text and Geospatial Indexes"
意味着我们不能在单个 Spring 数据中同时使用 $textSearch
和 $nearSphere
Mongo 存储库方法。
但我正在寻找一些解决方法,让我可以同时使用 TextCriteria
和 nearSphere 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。使用 Polygon
或 Circle
来构造查询,如
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 文档 "Queries cannot use both text and Geospatial Indexes"
意味着我们不能在单个 Spring 数据中同时使用 $textSearch
和 $nearSphere
Mongo 存储库方法。
但我正在寻找一些解决方法,让我可以同时使用 TextCriteria
和 nearSphere 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。使用 Polygon
或 Circle
来构造查询,如
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)