我基本上想要搜索带有2个过滤器/标签的查询
(例如,2个国家的1个城市 - 美国或加拿大)
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;
}
例如代码呈现ff查询,该查询显示国家 US 内名称中包含 NEW 的可用城市:
_search?default_operator = AND&q = new * country:Us
但我想要实现的是这样的:
_search?default_operator = AND&q = new * country:Us || Canada
这应该导致在 US OR CANADA 中显示名称中包含 NEW 的所有城市 .
但是||似乎没有用,所以我要求正确的改造OR运算符,或者可以帮助我实现目标的东西 .
1 回答
找到答案:
|
表示OR操作https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html
但它在查询中不起作用,所以为了实现我的目标,我需要将
|
转换为OR
(前后有空格)