我们如何通过不完整的名称找到产品或类别?
How can we find a product or category by incomplete name?
我最近在使用 Commercetools 平台,我有这样一个问题。我们如何通过不完整的名称查找产品或类别等?
例如,如果在我的 url 中我写了这样的东西
https://localhost:8080/cat?catName=G
我想找到包含 G
的所有类别。我们如何做到这一点?
您可以使用 GraphQL API 上的 categoryAutoComplete 查询来获取此信息。以下查询请求以 "hi" 开头的类别。您至少需要提供两个字符,只有一个字母不会return任何结果。
{
categoryAutocomplete(locale: "en", text: "hi") {
results {
name(locale: "en")
}
}
}
在我的测试项目中,查询 returns 两个英文名称中包含术语 "hint" 的类别:
{
"data": {
"categoryAutocomplete": {
"results": [
{
"name": "Test duplicate order hint"
},
{
"name": "order hint test"
}
]
}
}
}
这有帮助吗?
您可以使用 commercetools JVM SDK 发出 GraphQL 请求,如下所示:
- 首先实现代表您的 GraphQL 响应的 Java class。因此,如果一个结果对象看起来像:
{
"name": "category name"
}
然后实现 java class 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;
}
}
- 然后实现 GraphQL 请求class
CategoryAutoCompleteRequest
。这可以通过从 commercetools JVM SDK 中实现 SphereRequest
接口来简化,如下所示:
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();
我最近在使用 Commercetools 平台,我有这样一个问题。我们如何通过不完整的名称查找产品或类别等?
例如,如果在我的 url 中我写了这样的东西
https://localhost:8080/cat?catName=G
我想找到包含 G
的所有类别。我们如何做到这一点?
您可以使用 GraphQL API 上的 categoryAutoComplete 查询来获取此信息。以下查询请求以 "hi" 开头的类别。您至少需要提供两个字符,只有一个字母不会return任何结果。
{
categoryAutocomplete(locale: "en", text: "hi") {
results {
name(locale: "en")
}
}
}
在我的测试项目中,查询 returns 两个英文名称中包含术语 "hint" 的类别:
{
"data": {
"categoryAutocomplete": {
"results": [
{
"name": "Test duplicate order hint"
},
{
"name": "order hint test"
}
]
}
}
}
这有帮助吗?
您可以使用 commercetools JVM SDK 发出 GraphQL 请求,如下所示:
- 首先实现代表您的 GraphQL 响应的 Java class。因此,如果一个结果对象看起来像:
{
"name": "category name"
}
然后实现 java class 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;
}
}
- 然后实现 GraphQL 请求class
CategoryAutoCompleteRequest
。这可以通过从 commercetools JVM SDK 中实现SphereRequest
接口来简化,如下所示:
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();