如何在Python的弹性搜索中搜索多个OR条件

时间:2020-06-18 08:50:04

标签: python elasticsearch

我必须在弹性搜索中搜索数组中的所有项目以及静态细节。

Elastics搜索索引中的字段:tech_iddetailvolume

tech_ids = ['qwe1', 'qwe2', 'qwe3', 'qwe4', 'qwe5', 'qwe6', 'qwe7']

数组中的tech_id数量可以不同。 现在,我必须在tech_id和detail的组合中进行搜索,其中tech_id有所不同,而detail保持不变。此组合是or组合。最后,我希望搜索具有提供的tech_id和静态详细信息。

tech_ids = ['qwe1', 'qwe2', 'qwe3', 'qwe4', 'qwe5', 'qwe6', 'qwe7']
        "query": {
            "bool": {
                "must": [
                    {
                        "match": {
                            "detail": "calci"
                        }
                    },
                    {
                        "match_phrase": {
                            "tech_id": tech_ids[0]
                        }
                    }]
}

1 个答案:

答案 0 :(得分:1)

我想,您追求的是必须在布尔值内进行布尔值:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "detail": "calci"
          }
        },
        {
          "bool": {
            "should": 
              [{
                "match_phrase": { "tech_id": tid }
              } for tid in tech_ids]
          }
        }
      ]
    }
  }
}
相关问题