Include assessment in Morphia full-text search

I am trying to use MongoDB full-text indexes in Morphia. I need to return a grade for each document, as well as sort the results. This is what my query looks without Morphia:

   db.getCollection('disease').find( { $text: { $search: "brain" } }, 
                                     { score: { $meta: "textScore" } } )
                              .sort( { score: { $meta: "textScore" } } )

This works correctly and returns hits sorted by result.

I can also do this using the MongoDB Java driver directly without Morphia.

    // search with the Java driver
    BasicDBObject textSearch = new BasicDBObject("$search", "brain");
    BasicDBObject search = new BasicDBObject("$text", textSearch);

    BasicDBObject meta = new BasicDBObject("$meta", "textScore");
    BasicDBObject score = new BasicDBObject("score", meta);

    List<DBObject> diseases = collection.find(search, score).sort(score).toArray();
    Assert.assertEquals(2, diseases.size());
    Assert.assertEquals("brain", diseases.get(0).get("name"));
    Assert.assertEquals("benign-brain", diseases.get(1).get("name"));

I cannot figure out how to do the same in Morphia. Here is an example from the Morphia documentation ( http://mongodb.imtqy.com/morphia/1.0/guides/querying/#text-searching ):

List<Greeting> good = datastore.createQuery(Greeting.class)
                             .search("good")
                             .order("_id")
                             .asList();
Assert.assertEquals(4, good.size());

The example does not return a result and orders "_id". I see no way to handle the $ meta operator in Morphia. Has anyone done something like this?

+4
source share
3

Morphia , . , . , , . , , - .

+2

@evanchooly OP, :

.

Datastore morphiaDS = ...;
Query<myMorphiaModel> query = morphiaDS.createQuery(myMorphiaModel.class)
                .field("helloField").equal("world")
                .search("yadayadayada"); // Search performs a text search

Java Mongo. , , @evanchooly - , .

BasicDBObject meta = new BasicDBObject("$meta", "textScore");
BasicDBObject score = new BasicDBObject("score", meta);
List<DBObject> results = query.getCollection()
            .find(query.getQueryObject(), score)
            .sort(score).toArray();

,

Morphia morphia = mongoService.getMorphia();
List<myMorphiaModel> searchDocs = results.stream()
            .map((result) -> morphia.fromDBObject(myMorphiaModel.class, result))
            .collect(Collectors.toList());

, , , , "", , .

, , ...

0

, ​​ https://github.com/mongodb/morphia/commit/af4d64f6de3c0b1437dd216f5762d03bf98cdcb0, :

List<Greeting> good = datastore.createQuery(Greeting.class)
                                .search("good")
                                .project(Meta.textScore("score"))
                                .order(Meta.textScore("score"))

The only caveat is that, since the project in scoreis mandatory in order to be able to sort by score, if no other forecasts are added, the result simply contains a field score. Thus, forecasts must be added to the request for all required fields.

Hope this helps.

0
source

All Articles