Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I found a bug in serializing with SearchRequest.toString() #573

Closed
qinxingnet opened this issue Mar 15, 2023 · 6 comments
Closed

I found a bug in serializing with SearchRequest.toString() #573

qinxingnet opened this issue Mar 15, 2023 · 6 comments
Labels
bug Something isn't working

Comments

@qinxingnet
Copy link

Description

  SearchRequest searchRequest = new SearchRequest("w");
        searchRequest.setFilter(new String[]{"mass < 200"});
        searchRequest.setSort(new String[]{"mass:asc"});
        searchRequest.setShowMatchesPosition(true);
        searchRequest.setAttributesToHighlight(new String[]{"title"});
        final Searchable meteorites = client.index("meteorites").search(searchRequest);

I tracked for a period of time and found that it was the SearchRequest.toString() problem,Because when String [] is used as key and value, it will not be serialized.

Expected behavior
You need to rewrite SearchRequest. toString() and add the value processing mechanism for String []

  @Override
    public String toString() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("q", this.getQ());
        jsonObject.put("offset", this.getOffset());
        jsonObject.put("limit", this.getLimit());
        jsonObject.put("attributesToRetrieve", getStrArrays(this.getAttributesToRetrieve()));
        jsonObject.put("cropLength", this.getCropLength());
        jsonObject.put("cropMarker", this.getCropMarker());
        jsonObject.put("highlightPreTag", this.getHighlightPreTag());
        jsonObject.put("highlightPostTag", this.getHighlightPostTag());
        jsonObject.put("matchingStrategy", this.getMatchingStrategy() == null ? null : this.getMatchingStrategy().toString());
        jsonObject.put("showMatchesPosition", this.getShowMatchesPosition());
        jsonObject.put("facets", getStrArrays(this.getFacets()));
        jsonObject.put("sort", getStrArrays(this.getSort()));
        jsonObject.put("page", this.getPage());
        jsonObject.put("hitsPerPage", this.getHitsPerPage());
        jsonObject.putOpt("attributesToCrop", getStrArrays(this.getAttributesToCrop()));
        jsonObject.putOpt("attributesToHighlight", getStrArrays(this.getAttributesToHighlight()));
        jsonObject.putOpt("filter", getStrArrays(this.getFilter()));
        jsonObject.putOpt("filter", this.getFilterArray());
        return jsonObject.toString();
    }

    /**
     * trans it to json String value 
     *
     * @param arr arr
     * @return {@code Object}
     */
    private static Object getStrArrays(String[] arr) {
        if (arr == null) {
            return null;
        }
        if (arr.length >= 1) {
            final JSONArray jsonArray = new JSONArray();
            for (String s : arr) {
                jsonArray.put(s);
            }
            return jsonArray;
        } else {
            return arr;
        }
    }

The above code solves the problem before serializing to httpRequest.

Screenshots or Logs
image

Exception in thread "main" Meilisearch ApiException: {Error=APIError: {message='Invalid value type at `.attributesToHighlight`: expected an array, but found a string: `"[Ljava.lang.String;@5d11346a"`', code='invalid_search_attributes_to_highlight', type='invalid_request', link='https://docs.meilisearch.com/errors#invalid_search_attributes_to_highlight'}}

Environment (please complete the following information):

  • OS: [centOS 8.1 ]
  • Meilisearch version: [ v.0.1.2]
  • meilisearch-java version: [ v0.11.0]
@alallema alallema added the bug Something isn't working label Mar 15, 2023
@alallema
Copy link
Contributor

Hi @qinxingnet,
Thank you very much for raising this issue and providing the means to fix it ❤️

@ancatoza
Copy link

hi :D
Can I help doing this easy task?

@alallema
Copy link
Contributor

Hi @ancatoza,
Yes of course!

@ancatoza
Copy link

Thank you, I know that @qinxingnet already provide the code solution, I only want to be 100% sure about the location of the bug, so does this directory match?
meilisearch-java/src/main/java/com/meilisearch/sdk/SearchRequest.java

@alallema
Copy link
Contributor

Yes exactly!

@brunoocasali
Copy link
Member

Hi @qinxingnet, I was not able to reproduce this error

I used this simple Java setup to try it out brunoocasali/meili-java@87bdc2b. The latest commit is trying to reproduce your use case.

Below the results of that execution:

image

I'm closing this since there is no evidence we have an issue in this repo. Feel free to reopen if you think that is a mistake!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants