Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query which finds all Objects which contains text

I've following method, which should find all Objects with passed text="3":

private DBCursor queryForText(final String text) {

    Pattern searchPattern = Pattern.compile(text);

    DBObject query = QueryBuilder.start().or(
            QueryBuilder.start(MMSI_KEY).regex(searchPattern).get(),
            QueryBuilder.start(IMO_KEY).regex(searchPattern).get(),
            QueryBuilder.start(NAME_KEY).regex(searchPattern).get(),
            QueryBuilder.start(CALLSIGN_KEY).regex(searchPattern).get(),

            QueryBuilder.start(TYPEOFSHIP_KEY).regex(searchPattern).get(),
            QueryBuilder.start(LENGTH_KEY).regex(searchPattern).get(),
            QueryBuilder.start(WIDTH_KEY).regex(searchPattern).get())
            .get();

    return getVesselsCollection().find(query);
}

My Problem is, that TYPEOFSHIP, LENGTH and WIDTH are from type int. So if someone enters a text = "3", those attributes won't get found because these are no Strings. I'm looking for a way to not distinguish by valuetypes, e.g. with typecasts? I've no clue how to continue, maybe i'm still not familiar with mongoDB.

EDIT: http://docs.mongodb.org/manual/reference/sql-comparison/ there you see the relation between SQL and MONGODB. I think i need something like this:

SELECT *
FROM users
WHERE user_id like "%3%"

But QueryBuilder seems not to offer something like like.

Thats the JUnit test which I want to let work:

@Test
public void testSearchForTextt() {
    // Given
    VesselController controller = new VesselControllerImpl();
    controller.create("hugo", "bene", "2", "3", 4, 5, 6);
    controller.create(" ", "bene", "2", "3", 3, 6, 7);
    controller.create("3", "bene", "3", "hugo", 5, 6, 7);
    controller.create("3-name", "herbert", "Toto", "hugo", 5, 6, 7);

    proveAmountOfVessels(controller, 4);

    proveSearch(controller, "bene", 3);
    proveSearch(controller, "hugo", 3);
    proveSearch(controller, "3", 5);
    proveSearch(controller, "[", 0);
    proveSearch(controller, " ", 1);
}

private void ueberpruefeSuche(final VesselController controller, final String text, final int expectedElements) {
    // When
    Collection<Vessel> treffer = controller.findByText(text);
    // Then
    assertEquals(String.format("Wrong amount of elements '%s' found", text), expectedElements, treffer.size());
}

The relevant check is:

proveSearch(controller, "3", 5); 

because there are different kind of datatypes it isn't working. My MongoDB command of the shown code above should look almost like this:

db.vessels.find({$or : ['mmsi' : {$regex : 'text'}],
['imo' : {$regex : 'text'}],
['name' : {$regex : 'text'}],
['callsign' : {$regex : 'text'}],
...
['width' : {$regex : 'text'}]})

query: { $or: [ { mmsi: /3/ }, { imo: /3/ }, { name: /3/ }, { callsign: /3/ }, { typeofship: /3/ }, { length: /3/ }, { width: /3/ } ] }

I guess this should be the right Query! but I'm not sure, because the result isn't that what i've expected.

like image 621
headbanger Avatar asked Dec 23 '25 02:12

headbanger


1 Answers

You want to do a text search: http://docs.mongodb.org/manual/reference/operator/query/text/

like image 123
evanchooly Avatar answered Dec 24 '25 15:12

evanchooly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!