Skip to content

Commit

Permalink
Token ID search
Browse files Browse the repository at this point in the history
  • Loading branch information
dwerning committed Sep 7, 2023
2 parents f68f998 + d0c8bd9 commit 4bf32e2
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 70 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ settings.gradle
gradlew
gradle/wrapper/gradle-wrapper.properties
src/main/java/tla/domain
src/main/java/tla/error
109 changes: 53 additions & 56 deletions src/main/java/tla/backend/es/query/SentenceSearchQueryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,73 +12,70 @@
import lombok.extern.slf4j.Slf4j;
import tla.backend.es.model.SentenceEntity;
import tla.backend.es.model.SentenceEntity.Context;
import tla.backend.es.model.parts.Token;
import tla.backend.service.ModelClass;
import tla.domain.command.PassportSpec;
import tla.domain.command.SentenceSearch.TokenSpec;

@Slf4j
@Getter
@ModelClass(SentenceEntity.class)
public class SentenceSearchQueryBuilder extends ESQueryBuilder implements MultiLingQueryBuilder {

public final static String AGG_ID_TEXT_IDS = "text_ids";

public void setContext(Context context) {
BoolQueryBuilder textQuery = boolQuery();
String textId = context.getTextId();
if (textId != null) {

log.info("sentence query: receive {} text IDs", textId);
textQuery.must(
QueryBuilders.termQuery(
"context.textId",
textId
)
);
this.filter(textQuery);
}
public final static String AGG_ID_TEXT_IDS = "text_ids";

}
public void setContext(Context context) {
BoolQueryBuilder textQuery = boolQuery();
String textId = context.getTextId();
if (textId != null) {
log.info("sentence query: receive {} textIDs", textId);
textQuery.must(QueryBuilders.termQuery("context.textId", textId));
this.filter(textQuery);
}

public void setTokens(Collection<TokenSearchQueryBuilder> tokenQueries) {
BoolQueryBuilder tokenQuery = boolQuery();
if (tokenQueries != null) {
tokenQueries.forEach(
query -> tokenQuery.must(
QueryBuilders.nestedQuery(
"tokens",
query.getNativeRootQueryBuilder(),
ScoreMode.None
)
)
);
}
this.filter(tokenQuery);
}
}

public void setPassport(PassportSpec spec) {
log.info("set sentence search passport specs");
if (spec != null && !spec.isEmpty()) {
log.info("spawn text search dependency");
var textSearchQuery = new TextSearchQueryBuilder();
textSearchQuery.setExpansion(true);
textSearchQuery.setPassport(spec);
this.dependsOn(
textSearchQuery,
this::setTextIds
);
}
}
/* public void setTokens(Collection<Token> tokens) {
if (tokens != null) {
BoolQueryBuilder tokenQuery = boolQuery();
String tokenId = tokens.iterator().next().getId();
if (tokenId != null) {
log.info("sentence query: receive {} as tokenID", tokenId);
tokenQuery.must(QueryBuilders.nestedQuery("tokens", QueryBuilders.termQuery("tokens.id", tokenId),
ScoreMode.None));
this.filter(tokenQuery);
}
}
} */

public void setTextIds(Collection<String> textIds) {
if (textIds != null) {
log.info("sentence query: receive {} text IDs", textIds.size());
this.filter(
QueryBuilders.termsQuery(
"context.textId",
textIds
)
);
}
}
public void setTokens(Collection<TokenSearchQueryBuilder> tokenQueries) {
BoolQueryBuilder tokenQuery = boolQuery();
if (tokenQueries != null) {
tokenQueries.forEach(query -> tokenQuery
.must(QueryBuilders.nestedQuery("tokens", query.getNativeRootQueryBuilder(), ScoreMode.None)));
}
this.filter(tokenQuery);
}


public void setPassport(PassportSpec spec) {
log.info("set sentence search passport specs");
if (spec != null && !spec.isEmpty()) {
log.info("spawn text search dependency");
var textSearchQuery = new TextSearchQueryBuilder();
textSearchQuery.setExpansion(true);
textSearchQuery.setPassport(spec);
this.dependsOn(textSearchQuery, this::setTextIds);
}
}

// TODO
public void setTextIds(Collection<String> textIds) {
if (textIds != null) {
log.info("sentence query: receive {} text IDs", textIds.size());
this.filter(QueryBuilders.termsQuery("context.textId", textIds));
}
}

}
29 changes: 15 additions & 14 deletions src/main/java/tla/backend/es/query/TokenSearchQueryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@

public class TokenSearchQueryBuilder extends ESQueryBuilder implements MultiLingQueryBuilder {

@Override
public String nestedPath() {
return "tokens.";
}
@Override
public String nestedPath() {
return "tokens.";
}
//TODO where is this function called
public void setTokenId(String tokenId) {
if (tokenId != null) {
this.must(QueryBuilders.termQuery(String.format("%sid", this.nestedPath()), tokenId));
}
}

public void setLemma(Lemmatization lemma) {
if (lemma != null && !lemma.isEmpty()) {
this.must(
QueryBuilders.termQuery(
String.format("%slemma.id", this.nestedPath()),
lemma.getId()
)
);
}
}
public void setLemma(Lemmatization lemma) {
if (lemma != null && !lemma.isEmpty()) {
this.must(QueryBuilders.termQuery(String.format("%slemma.id", this.nestedPath()), lemma.getId()));
}
}

}

0 comments on commit 4bf32e2

Please sign in to comment.