Skip to content

Commit

Permalink
1. 7.5.1.3.RELEASE 支持Nested查询
Browse files Browse the repository at this point in the history
  • Loading branch information
weilong.hu committed Dec 8, 2020
1 parent 8b0b801 commit 9a1506c
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 14 deletions.
2 changes: 1 addition & 1 deletion ebatis-core/pom.xml
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>ebatis</artifactId>
<groupId>io.manbang</groupId>
<version>7.5.1.2.RELEASE</version>
<version>7.5.1.3.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ebatis-core</artifactId>
Expand Down
@@ -1,5 +1,7 @@
package io.manbang.ebatis.core.annotation;

import org.apache.lucene.search.join.ScoreMode;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand All @@ -14,4 +16,7 @@
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface Nested {
String path();

ScoreMode scoreMode() default ScoreMode.None;
}
Expand Up @@ -41,7 +41,7 @@ public enum QueryType {
INDICES(null),
MLT(null),
MULTI_MATCH(null),
NESTED(null),
NESTED(QueryBuilderFactory.nested()),
PREFIX(null),
QUERY_STRING(null),
RANGE(null),
Expand Down
@@ -0,0 +1,43 @@
package io.manbang.ebatis.core.builder;

import io.manbang.ebatis.core.annotation.Nested;
import io.manbang.ebatis.core.exception.AttributeNotFoundException;
import io.manbang.ebatis.core.meta.ConditionMeta;
import io.manbang.ebatis.core.provider.PathProvider;
import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.index.query.NestedQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

import java.util.Objects;
import java.util.Optional;

/**
* @author weilong.hu
* @since 2020/12/4 11:27
*/
class NestedQueryBuilderFactory extends AbstractQueryBuilderFactory<NestedQueryBuilder, Nested> {
public static final NestedQueryBuilderFactory INSTANCE = new NestedQueryBuilderFactory();

@Override
protected NestedQueryBuilder doCreate(ConditionMeta conditionMeta, Object condition) {
String path = null;
ScoreMode scoreMod = null;
if (condition instanceof PathProvider) {
PathProvider provider = (PathProvider) condition;
path = provider.getPath();
scoreMod = provider.getScoreMode();
}
final Nested nested =
Optional.ofNullable(conditionMeta).flatMap(c -> c.findAttributeAnnotation(Nested.class)).orElse(null);
if (Objects.nonNull(nested)) {
path = nested.path();
scoreMod = nested.scoreMode();
}
if (Objects.isNull(path)) {
throw new AttributeNotFoundException("条件必须实现 PathProvider或在注解上表明nest");
}
return QueryBuilders.nestedQuery(path, AutoQueryBuilderFactory.INSTANCE.create(conditionMeta, condition), scoreMod);


}
}
Expand Up @@ -83,6 +83,10 @@ static QueryBuilderFactory boosting() {
return BoostingQueryBuilderFactory.INSTANCE;
}

static QueryBuilderFactory nested() {
return NestedQueryBuilderFactory.INSTANCE;
}

/**
* 创建查询构建器
*
Expand Down
@@ -0,0 +1,26 @@
package io.manbang.ebatis.core.provider;

import org.apache.lucene.search.join.ScoreMode;

/**
* @author weilong.hu
* @since 2020/12/4 11:47
*/
@FunctionalInterface
public interface PathProvider extends Provider {
/**
* nested path
*
* @return nested path
*/
String getPath();

/**
* ScoreMode
*
* @return ScoreMode
*/
default ScoreMode getScoreMode() {
return ScoreMode.None;
}
}
4 changes: 2 additions & 2 deletions ebatis-sample/pom.xml
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>ebatis</artifactId>
<groupId>io.manbang</groupId>
<version>7.5.1.2.RELEASE</version>
<version>7.5.1.3.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -16,7 +16,7 @@
<dependency>
<groupId>io.manbang</groupId>
<artifactId>ebatis-spring</artifactId>
<version>7.5.1.2.RELEASE</version>
<version>7.5.1.3.RELEASE</version>
</dependency>

<dependency>
Expand Down
Expand Up @@ -2,6 +2,7 @@

import io.manbang.ebatis.core.annotation.Field;
import io.manbang.ebatis.core.annotation.Must;
import io.manbang.ebatis.core.annotation.Nested;
import io.manbang.ebatis.core.annotation.QueryType;
import io.manbang.ebatis.core.annotation.Should;
import io.manbang.ebatis.core.domain.Range;
Expand Down Expand Up @@ -98,6 +99,9 @@ public class RecentOrderCondition extends SampleRecentOrderCondition implements
@Must(queryType = QueryType.WILDCARD)
private String unloadAddress;

@Must(queryType = QueryType.NESTED, nest = @Nested(path = "attrs"))
private Integer cargoAttr;

@Override
public ScoreFunction getFunction() {
return ScoreFunction.fieldValueFactor("startCityId", 10, 10, FieldValueFactorFunction.Modifier.LN);
Expand Down
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>ebatis-spring-boot</artifactId>
<groupId>io.manbang</groupId>
<version>7.5.1.2.RELEASE</version>
<version>7.5.1.3.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion ebatis-spring-boot/ebatis-spring-boot-starter/pom.xml
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>ebatis-spring-boot</artifactId>
<groupId>io.manbang</groupId>
<version>7.5.1.2.RELEASE</version>
<version>7.5.1.3.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
8 changes: 4 additions & 4 deletions ebatis-spring-boot/pom.xml
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>ebatis</artifactId>
<groupId>io.manbang</groupId>
<version>7.5.1.2.RELEASE</version>
<version>7.5.1.3.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -30,17 +30,17 @@
<dependency>
<groupId>io.manbang</groupId>
<artifactId>ebatis-spring</artifactId>
<version>7.5.1.2.RELEASE</version>
<version>7.5.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>io.manbang</groupId>
<artifactId>ebatis-core</artifactId>
<version>7.5.1.2.RELEASE</version>
<version>7.5.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>io.manbang</groupId>
<artifactId>ebatis-spring-boot-autoconfigure</artifactId>
<version>7.5.1.2.RELEASE</version>
<version>7.5.1.3.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
Expand Down
4 changes: 2 additions & 2 deletions ebatis-spring/pom.xml
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>ebatis</artifactId>
<groupId>io.manbang</groupId>
<version>7.5.1.2.RELEASE</version>
<version>7.5.1.3.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ebatis-spring</artifactId>
Expand All @@ -14,7 +14,7 @@
<dependency>
<artifactId>ebatis-core</artifactId>
<groupId>io.manbang</groupId>
<version>7.5.1.2.RELEASE</version>
<version>7.5.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
Expand Down
2 changes: 1 addition & 1 deletion ebatis-web/pom.xml
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>ebatis</artifactId>
<groupId>io.manbang</groupId>
<version>7.5.1.2.RELEASE</version>
<version>7.5.1.3.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -6,7 +6,7 @@

<groupId>io.manbang</groupId>
<artifactId>ebatis</artifactId>
<version>7.5.1.2.RELEASE</version>
<version>7.5.1.3.RELEASE</version>
<packaging>pom</packaging>
<name>ebatis</name>
<description>Elasticsearch ORM Framework</description>
Expand Down

0 comments on commit 9a1506c

Please sign in to comment.