1
votes

I basically would like to search for a query with 2 filters/tags

(e.g. 1 city in 2 countries - US OR CANADA)

ElasticSearchAPi.java


    public interface ElasticSearchAPI {
        @GET("_search/")
        Call search(
                @HeaderMap Map headers,
                @Query("default_operator") String operator,
                @Query("q") String query

        );
    }

Part of Search.java


    Retrofit retrofit = new Retrofit.Builder()
                                .baseUrl(BASE_URL)
                                .addConverterFactory(GsonConverterFactory.create())
                                .build();

                        ElasticSearchAPI searchAPI = retrofit.create(ElasticSearchAPI.class);

                        HashMap headerMap = new HashMap();
                        headerMap.put("Authorization", Credentials.basic("user", mElasticSearchPassword));

                        String searchString = "";

                        if(!mSearchText.equals("")){
                            searchString = searchString + mSearchText.getText().toString() + "*";
                        }
                        if(!mPrefCountry.equals("")){
                            searchString = searchString + " country:" + mPrefCountry;
                        }

                        Call call = searchAPI.search(headerMap, "AND", searchString);

                        call.enqueue(new Callback() {
                            @Override
                            public void onResponse(Call call, Response response) {

                                HitsList hitsList = new HitsList();
                                String jsonResponse = "";
                                try{
                                    Log.d(TAG, "onResponse: server response: " + response.toString());

                                    if(response.isSuccessful()){
                                        hitsList = response.body().getHits();
                                    }else{
                                        jsonResponse = response.errorBody().string();
                                    }

                                    Log.d(TAG, "onResponse: hits: " + hitsList);

                                    for(int i = 0; i  call, Throwable t) {
                                Log.e(TAG, "onFailure: " + t.getMessage() );
                                Toast.makeText(getActivity(), "search failed", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                    return false;
                }

e.g. the code renders the ff query which shows the available cities with NEW in its name inside the country US :

_search?default_operator=AND&q=new*+country:Us

but what im trying to achieve is something like this :

_search?default_operator=AND&q=new*+country:Us||Canada

that should result in showing all cities with NEW in its name inside US OR CANADA.

but || doesnt seem to work, so im asking for the correct Retrofit OR operator, or something that can help me achieve my goal.

1
What's the question? What problem you're facing with current code posted here as minimal reproducible example? Please elaborate. And also a sample URL that you're trying to access would be helpful to understand the problem.Shashanth
@Shashanth i added more info hope it clarifies my questionuser3605994

1 Answers

0
votes

Found the answer :

| signifies OR operation

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html

But it doesnt work in queries, so to achieve my goal I need to turn | into OR (with spaces before and after)

_search?default_operator=AND&q=new*+country:(Us OR Canada)