试试搜索一下吧

关于集合类型多选的问题

Elasticsearch | 作者 meowCoder123 | 发布于2022年05月18日 | 阅读数:349

现在我有一组数据,其中的一个属性是以集合的形式保存在了es里 数据结构如下(sqzl的类型是keyword):



{
    "hits": [
        {
            "sampleType": "T",
            "operInfoJson": {
                "sqzl": [
                    "放疗",
                    "化疗"
                ]
            }
        },
        {
            "sampleType": "T",
            "operInfoJson": {
                "sqzl": [
                    "放疗",
                    "化疗"
                ]
            }
        },
        {
            "sampleType": "T",
            "operInfoJson": {
                "sqzl": [
                    "放疗"
                ]
            }
        },
        {
            "sampleType": "T",
            "operInfoJson": {
                "sqzl": [
                    "化疗"
                ]
            }
        }
    ]
}
 


现在我想通过多选将数据中有放疗或者有化疗的数据查出(同时包含化疗和放疗的也要查出来),但我通过termsQuery进行查询后发现并不能实现我想要的效果,查询语句如下



{
    "from": 0,
    "size": 10,
    "timeout": "60s",
    "query": {
        "bool": {
            "must": [
                {
                    "nested": {
                        "query": {
                            "bool": {
                                "must": [
                                    {
                                        "terms": {
                                            "operInfoJson.sqzl": [
                                                "放疗,化疗"
                                            ],
                                            "boost": 1.0
                                        }
                                    }
                                ],
                                "adjust_pure_negative": true,
                                "boost": 1.0
                            }
                        },
                        "path": "operInfoJson",
                        "ignore_unmapped": false,
                        "score_mode": "none",
                        "boost": 1.0
                    }
                }
            ],
            "adjust_pure_negative": true,
            "boost": 1.0
        }
    },
    "track_total_hits": 2147483647
}
 


 
使用多选的方式一条数据都查不出,但是将terms分成两个,一个查放疗,一个查化疗,就可以查出同时拥有化疗和放疗的数据,请求如下:



{
    "from": 0,
    "size": 10,
    "timeout": "60s",
    "query": {
        "bool": {
            "must": [
                {
                    "nested": {
                        "query": {
                            "bool": {
                                "must": [
                                    {
                                        "terms": {
                                            "operInfoJson.sqzl": [
                                                "放疗"
                                            ],
                                            "boost": 1.0
                                        }
                                    }
                                ],
                                "adjust_pure_negative": true,
                                "boost": 1.0
                            }
                        },
                        "path": "operInfoJson",
                        "ignore_unmapped": false,
                        "score_mode": "none",
                        "boost": 1.0
                    }
                },
                {
                    "nested": {
                        "query": {
                            "bool": {
                                "must": [
                                    {
                                        "terms": {
                                            "operInfoJson.sqzl": [
                                                "化疗"
                                            ],
                                            "boost": 1.0
                                        }
                                    }
                                ],
                                "adjust_pure_negative": true,
                                "boost": 1.0
                            }
                        },
                        "path": "operInfoJson",
                        "ignore_unmapped": false,
                        "score_mode": "none",
                        "boost": 1.0
                    }
                }
            ],
            "adjust_pure_negative": true,
            "boost": 1.0
        }
    },
    "track_total_hits": 2147483647
}
 


 
3 请问怎样才能在多选的同时查出 只有化疗的数据和只用放疗的数据,同时也能将化疗和放疗都有的数据查出来.难道只能用should吗,我想通过termsQuery那样的多选来解决,请问这种方式有什么解决办法吗
已邀请:

duanxiaobiao - 90 后IT男

赞同来自:

{
    "query": {
      "nested": {
          "query": {
              "terms": {
                  "operInfoJson.sqzl": [
                      "放疗",
                      "化疗"
                  ],
                  "boost": 1.0
              }
          },
          "path": "operInfoJson",
          "ignore_unmapped": false,
          "score_mode": "none",
          "boost": 1.0
      }
  }
}
 
========结果==============

nested-collection.png

 
 
 

要回复问题请先登录注册