Skip to content

Text Indexes

Stephan Bösebeck edited this page Jun 8, 2017 · 1 revision

Text Search

Mongodb has since V3.x a built in text search functionality. This can be used in commandline, or using morphium:

  @Test
    public void textIndexTest() throws Exception {
        morphium.dropCollection(Person.class);
        try {
            morphium.ensureIndicesFor(Person.class);

        } catch (Exception e) {
            log.info("Text search not enabled - test skipped");
            return;
        }
        createData();
        waitForWrites();
        Query<Person> p = morphium.createQueryFor(Person.class);
        List<Person> lst = p.text(Query.TextSearchLanguages.english, "hugo", "bruce").asList();
        assert (lst.size() == 2) : "size is " + lst.size();
        p = morphium.createQueryFor(Person.class);
        lst = p.text(Query.TextSearchLanguages.english, false, false, "Hugo", "Bruce").asList();
        assert (lst.size() == 2) : "size is " + lst.size();
    }

In this case, there is some Data begin created, which puts the name of some superheroes in a mongo. Searching for the text ist something different than searching via regular expressions, because Text Indexes are way more efficient in that case.

If you need more information on text indexes, have a look at Mongodbs documentation and take a look at the Tests for TextIndexes within the sourcecode of morphium.

You can’t perform that action at this time.