按照MongoDB的文档,"Queries cannot use both text and Geospatial Indexes"意味着我们不能同时使用$textSearch,并$nearSphere在单个弹簧数据蒙戈库方法.
但我正在寻找一些解决方法,这将允许我使用两者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"}}
Run Code Online (Sandbox Code Playgroud)
我处境最糟糕
对于我的存储库方法,它给出:
Page<User> getAddress_PositionNear(TextCriteria tc,Point gpoint, Distance d, Pageable p);
"errmsg" : "text and geoNear not allowed in same query" , "code" : 2
Run Code Online (Sandbox Code Playgroud)
在一个查询中结合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"}
})
Run Code Online (Sandbox Code Playgroud)
几乎可以通过相同的方式完成 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);
Run Code Online (Sandbox Code Playgroud)
使用Repository简单地使用Within关键字作为派生查询
Page<TextAndGeo> findByLocWithin(Circle circle, TextCriteria tc, Pageable page)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1211 次 |
| 最近记录: |