Search in sources :

Example 1 with TextQuery

use of org.springframework.data.mongodb.core.query.TextQuery in project spring-data-mongodb by spring-projects.

the class QueryMapperUnitTests method shouldParseNestedKeywordWithArgumentMatchingTheSourceEntitiesConstructorCorrectly.

// DATAMONGO-2517
@Test
void shouldParseNestedKeywordWithArgumentMatchingTheSourceEntitiesConstructorCorrectly() {
    TextQuery source = new TextQuery("test");
    org.bson.Document target = mapper.getMappedObject(source.getQueryObject(), context.getPersistentEntity(WithSingleStringArgConstructor.class));
    assertThat(target).isEqualTo(org.bson.Document.parse("{\"$text\" : { \"$search\" : \"test\" }}"));
}
Also used : TextQuery(org.springframework.data.mongodb.core.query.TextQuery) Test(org.junit.jupiter.api.Test)

Example 2 with TextQuery

use of org.springframework.data.mongodb.core.query.TextQuery in project main by JohnPeng739.

the class GeneralAccessorImpl method search.

/**
 * {@inheritDoc}
 *
 * @see GeneralTextSearchAccessor#search(Pagination, List, boolean, Class)
 */
@Override
public <T extends Base> List<T> search(Pagination pagination, List<String> contents, boolean valid, Class<T> clazz) {
    try {
        if (clazz.isInterface()) {
            clazz = EntityFactory.getEntityClass(clazz);
        }
        Query query = new TextQuery(new TextCriteria().matchingAny(contents.toArray(new String[0])));
        if (valid) {
            query.addCriteria(where("valid").is(true));
        }
        if (pagination != null) {
            pagination.setTotal((int) template.count(query, clazz));
            query.skip((pagination.getPage() - 1) * pagination.getSize());
            query.limit(pagination.getSize());
        }
        return template.find(query, clazz);
    } catch (ClassNotFoundException ex) {
        if (logger.isErrorEnabled()) {
            logger.error(String.format("Text search entity[%s] fail, condition: %s.", clazz.getName(), StringUtils.merge(contents, ",")), ex);
        }
        throw new UserInterfaceDalErrorException(UserInterfaceDalErrorException.DalErrors.ENTITY_INSTANCE_FAIL);
    }
}
Also used : TextQuery(org.springframework.data.mongodb.core.query.TextQuery) Query(org.springframework.data.mongodb.core.query.Query) TextCriteria(org.springframework.data.mongodb.core.query.TextCriteria) UserInterfaceDalErrorException(org.mx.dal.error.UserInterfaceDalErrorException) TextQuery(org.springframework.data.mongodb.core.query.TextQuery)

Aggregations

TextQuery (org.springframework.data.mongodb.core.query.TextQuery)2 Test (org.junit.jupiter.api.Test)1 UserInterfaceDalErrorException (org.mx.dal.error.UserInterfaceDalErrorException)1 Query (org.springframework.data.mongodb.core.query.Query)1 TextCriteria (org.springframework.data.mongodb.core.query.TextCriteria)1