如何使用elasticsearch-dsl-py创建“处于”条件的过滤器?

时间:2020-08-14 10:32:01

标签: python-3.x elasticsearch dsl elasticsearch-dsl

下面的查询是我想使用elasticsearch-dsl-py构造的,但是我不知道该怎么做。

GET /my_index/_search
{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "should": [
            {
              "term": {
                "status": "a"
              },
              "term": {
                "status": "b"
              },
              "term": {
                "status": "c"
              }
            }
          ]
        }
      }
    }
  }
}

我只想以SQL格式执行如下查询

select * from my_index where status in ("a","b","c")

使用elasticsearch-dsl-py,它与我所能接近的很近,但是并不相同。

class MyIndex(Document):

    status = Keyword() / Text()
    
    

MyIndex.search().filter('should', status=["a","b","c"])

1 个答案:

答案 0 :(得分:1)

执行此操作的另一种方法是对数组使用package com.bigfamilydevelopers.saverforall.activity; public class SplashScreen extends AppCompatActivity { SplashScreen activity; Context context; AppUpdateManager appUpdateManager; AppLangSessionManager appLangSessionManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash_screen); context = activity = this; appUpdateManager = AppUpdateManagerFactory.create(context); appLangSessionManager = new AppLangSessionManager(activity); UpdateApp(); setLocale(appLangSessionManager.getLanguage()); } @Override protected void onResume() { super.onResume(); activity = this; appLangSessionManager = new AppLangSessionManager(activity); appUpdateManager.getAppUpdateInfo().addOnSuccessListener(appUpdateInfo -> { if (appUpdateInfo.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) { try { appUpdateManager.startUpdateFlowForResult( appUpdateInfo, IMMEDIATE, activity, 101); } catch (IntentSender.SendIntentException e) { e.printStackTrace(); } } }); } public void HomeScreen() { new Handler().postDelayed(new Runnable() { @Override public void run() { Intent i = new Intent(SplashScreen.this, MainActivity.class); startActivity(i); } }, 1000); } public void UpdateApp() { try { Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo(); appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> { if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE && appUpdateInfo.isUpdateTypeAllowed(IMMEDIATE)) { try { appUpdateManager.startUpdateFlowForResult( appUpdateInfo, IMMEDIATE, activity, 101); } catch (IntentSender.SendIntentException e) { e.printStackTrace(); } } else { HomeScreen(); } }).addOnFailureListener(e -> { e.printStackTrace(); HomeScreen(); }); } catch (Exception e) { e.printStackTrace(); } } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 101) { if (resultCode != RESULT_OK) { HomeScreen(); } else { HomeScreen(); } } } public void setLocale(String lang) { if (lang.equals("")){ lang="en"; } Locale myLocale = new Locale(lang); Resources res = getResources(); DisplayMetrics dm = res.getDisplayMetrics(); Configuration conf = res.getConfiguration(); conf.locale = myLocale; res.updateConfiguration(conf, dm); } } 查询,因为每个数组元素都被隐式或。另外,我不确定您正在运行哪个版本的ES,但是很早以前就知道filtered has been replaced by bool在版本5中。因此,您的查询可以这样重写:

terms

使用GET /my_index/_search { "query": { "bool": { "filter": { "terms": { "status": ["a", "b", "c"] } } } } } 表示:

elasticsearch-dsl-py