使用NearSphere查询创建querydsl谓词

时间:2019-04-30 11:49:23

标签: mongodb spring-data-mongodb querydsl predicate

我正在尝试创建querydsl谓词(BooleanBuilder)

我添加了依赖性和插件来创建我的Qclasses。我已经创建了所有容易的部分,但是我不知道如何为Double [],nearSphere和distance做此操作。

这些是我的依赖项:

<!-- Query dsl -->
<dependency>
    <groupId>com.querydsl</groupId>
    <artifactId>querydsl-apt</artifactId>
    <scope>provided</scope>
    </dependency>

<dependency>
    <groupId>com.querydsl</groupId>
    <artifactId>querydsl-mongodb</artifactId>
</dependency>

<dependency>
    <groupId>com.querydsl</groupId>
    <artifactId>querydsl-sql-spatial</artifactId>
    <version>${querydsl.version}</version>
</dependency>

<dependency>
    <groupId>org.mongodb.morphia</groupId>
    <artifactId>morphia</artifactId>
    <version>${morphia.version}</version>
</dependency>

<dependency>
    <groupId>com.google.code</groupId>
    <artifactId>morphia</artifactId>
    <version>${morphia.code.version}</version>
</dependency>
<!-- QUery dsl -->

这是我的插件

<plugin>
    <groupId>com.mysema.maven</groupId>
    <artifactId>apt-maven-plugin</artifactId>
    <version>1.1.3</version>
        <executions>
            <execution>
                <goals>
                    <goal>process</goal>
                </goals>
                <configuration>
                        <outputDirectory>target/generated-sources/java</outputDirectory>
                        <processor>com.querydsl.apt.morphia.MorphiaAnnotationProcessor</processor>
                        <spatial>true</spatial>
                </configuration>
            </execution>
        </executions>
        <dependencies>
            <dependency>
                <groupId>javax.inject</groupId>
                <artifactId>javax.inject</artifactId>
                <version>1</version>
            </dependency>
        </dependencies>
</plugin>

这是我的包含Double []

的课程
import org.mongodb.morphia.annotations.Entity;

@Entity
public class OwnerLocation implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 8481550794454165168L;

    private Double[] geo;
    private String location;
    private Town town;

    public OwnerLocation() {}

    public OwnerLocation(Double[] geo, String location, Town town) {
        super();
        this.geo = geo;
        this.location = location;
        this.town = town;
    }
}

这是我的QClass生成的

import static com.querydsl.core.types.PathMetadataFactory.*;

import com.querydsl.core.types.dsl.*;

import com.querydsl.core.types.PathMetadata;
import javax.annotation.Generated;
import com.querydsl.core.types.Path;


/**
 * QOwnerLocation is a Querydsl query type for OwnerLocation
 */
@Generated("com.querydsl.codegen.EntitySerializer")
public class QOwnerLocation extends EntityPathBase<OwnerLocation> {

    private static final long serialVersionUID = 905971258L;

    public static final QOwnerLocation ownerLocation = new QOwnerLocation("ownerLocation");

    // custom
    public final com.querydsl.mongodb.Point geo = new com.querydsl.mongodb.Point(forProperty("geo"));

    public final StringPath location = createString("location");

    public final SimplePath<Town> town = createSimple("town", Town.class);

    public QOwnerLocation(String variable) {
        super(OwnerLocation.class, forVariable(variable));
    }

    public QOwnerLocation(Path<? extends OwnerLocation> path) {
        super(path.getType(), path.getMetadata());
    }

    public QOwnerLocation(PathMetadata metadata) {
        super(OwnerLocation.class, metadata);
    }

}

这是我要使用NearSphere的类

public class QueryAdHelper {

    public static Predicate buildQuery(SearchForm searchForm) {

        QAd qAd = QAd.ad;
        BooleanBuilder predicate = new BooleanBuilder();

        Category category = searchForm.getHeaderSearchForm().getCategory();

        // Header
        String keyword = searchForm.getHeaderSearchForm().getKeyWord();
        Town town = searchForm.getHeaderSearchForm().getTown();

        // Facet
        String adType = searchForm.getFacetSearchForm().getAdType();
        long minPrice = searchForm.getFacetSearchForm().getPriceRange().getMin();
        long maxPrice = searchForm.getFacetSearchForm().getPriceRange().getMax();
        String adContent = searchForm.getFacetSearchForm().getAdContent();

        if (keyword != null && !keyword.isEmpty()) {
            predicate.and(qAd.title.containsIgnoreCase(keyword));
        }

        if (category != null && category.getId() != null && !category.getId().isEmpty()) {
            // categories code.
            predicate.and(qAd.category.id.startsWith(category.getId()));
        }

        if (town != null && town.getCode() != null && !town.getCode().isEmpty()) {
            predicate.and(qAd.ownerLocation.town.eq(town));
        }

        ????????????????????????????
        MongodbExpressions.nearSphere();    

        if (adType != null && !AdTypeFacetEnum.ALL.getValue().equals(adType)) {
            predicate.and(qAd.type.eq(AdTypeEnum.valueOf(adType)));
        }

        if (minPrice > 0 || maxPrice > 0) {
            predicate.and(qAd.price.between(minPrice, maxPrice));
        }

        if (adContent != null) {
            if (AdContentFacetEnum.PICTURE.getValue().equals(adContent)) {
                predicate.and(qAd.images.isNotEmpty());
            }
        }

        return predicate;
    }
}

我缺少的部分是在“ QueryAdHelper”类中添加的问号。 预先谢谢你。

0 个答案:

没有答案