我最近与 Commercetools 平台合作,我有一个这样的问题。我们如何通过不完整的名称找到产品或类别等?
例如,如果在我的网址中我写了这样的内容
https://localhost:8080/cat?catName=G
我想找到所有包含G
. 我们怎么能做到这一点?
我最近与 Commercetools 平台合作,我有一个这样的问题。我们如何通过不完整的名称找到产品或类别等?
例如,如果在我的网址中我写了这样的内容
https://localhost:8080/cat?catName=G
我想找到所有包含G
. 我们怎么能做到这一点?
您可以通过GraphQL API 上的categoryAutoComplete查询来获取此信息。以下查询要求以“hi”开头的类别。您至少需要提供两个字符,只有一个字母不会返回任何结果。
{
categoryAutocomplete(locale: "en", text: "hi") {
results {
name(locale: "en")
}
}
}
在我的测试项目中,此查询返回两个类别,其英文名称中包含“提示”一词:
{
"data": {
"categoryAutocomplete": {
"results": [
{
"name": "Test duplicate order hint"
},
{
"name": "order hint test"
}
]
}
}
}
这有帮助吗?
您可以使用 commercetools JVM SDK 发出 GraphQL 请求,如下所示:
{
"name": "category name"
}
然后实现的 java 类CategoryAutoCompleteResult
看起来像这样:
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.sphere.sdk.models.Base;
public class CategoryAutoCompleteResult extends Base {
private final String name;
@JsonCreator
public CategoryAutoCompleteResult(@JsonProperty("name") final String name) {
this.name = name;
}
public String getName() {
return name;
}
}
CategoryAutoCompleteRequest
。这可以通过实现SphereRequest
commercetools JVM SDK 的接口来简化,如下所示:import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import io.sphere.sdk.client.HttpRequestIntent;
import io.sphere.sdk.client.SphereRequest;
import io.sphere.sdk.http.HttpMethod;
import io.sphere.sdk.http.HttpResponse;
import io.sphere.sdk.json.SphereJsonUtils;
import io.sphere.sdk.models.Base;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
public class CategoryAutoCompleteRequest extends Base implements SphereRequest<List<CategoryAutoCompleteResult>> {
private final String queryString;
CategoryAutoCompleteRequest(final String queryString) {
this.queryString = queryString;
}
public static CategoryAutoCompleteRequest of(@Nonnull final String queryString) {
return new CategoryAutoCompleteRequest(queryString);
}
@Nullable
@Override
public List<CategoryAutoCompleteResult> deserialize(final HttpResponse httpResponse) {
final JsonNode rootJsonNode = SphereJsonUtils.parse(httpResponse.getResponseBody());
final JsonNode results = rootJsonNode.get("data").get("categoryAutocomplete").get("results");
return SphereJsonUtils
.readObject(results, new TypeReference<List<CategoryAutoCompleteResult>>() {
});
}
@Override
public HttpRequestIntent httpRequestIntent() {
final String queryBody =
String.format("{categoryAutocomplete(locale: \"en\", text: \"%s\") {results{name(locale: \"en\")}}}",
queryString);
final String graphQlQuery = buildGraphQlQuery(queryBody);
return HttpRequestIntent.of(HttpMethod.POST, "/graphql", graphQlQuery);
}
private static String buildGraphQlQuery(@Nonnull final String queryBody) {
return String.format("{ \"query\": \"%s\"}", queryBody.replace("\"", "\\\""));
}
}
SphereClient
。执行请求如下所示:final List<CategoryAutoCompleteResult> results = sphereClient
.execute(CategoryAutoCompleteRequest.of("myIncompleteCatName")).toCompletableFuture().join();