在elastic search中,如何过滤多个字段上的结果集

dsekswqp  于 2021-07-15  发布在  ElasticSearch
关注(0)|答案(1)|浏览(421)

有人可以帮助我的搜索查询如何过滤结果的基础上,2个领域?我已经建立了一个索引与1000的文件初始化和从用户界面,我们将调用这个索引,它由2个搜索字段组成
按zipcode搜索
按城市/州搜索
基于这些组合,我们只需要在该邮政编码内显示结果。
Map

{
  "mappings": {
    "properties": {
      "address": {
        "properties": {
          "city": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "state": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "zipcode": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      },
      "startdate": {
        "type": "date"
      },
      "enddate": {
        "type": "date"
      },
      "customerstatus": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "customerid": {
        "type": "long"
      }
    }
  },
  "settings": {
    "index": {
      "number_of_shards": "1",
      "number_of_replicas": "1"
    }
  }
}

查询

{
  "from": 0,
  "size": 100,
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "32081",
            "fields": ["address.zipcode" ]
          }
        },
        {
          "query_string": {
            "query": "FL",
            "fields": ["address.cityname","address.state" ]
          }
        }
      ]
    }
  }
}

结果集

{
    "customerid":1,
    "customerstatus": Active,
    "address": {
                        "city": "PONTE VEDRA",
                        "state": "FL",
                        "zipcode": "32081"
                    },
    "startdate": "2020-07-15",
    "enddate": "2021-07-15" 
},
{
    "customerid":2,
    "customerstatus": Pending,
    "address": {
                        "city": "PONTE VEDRA",
                        "state": "FL",
                        "zipcode": "32081"
                    },
    "startdate": "2018-01-01",
    "enddate": "2019-01-01" 
},
{
    "customerid":3,
    "customerstatus": Pending,
    "address": {
                        "city": "PONTE VEDRA",
                        "state": "FL",
                        "zipcode": "32081"
                    },
    "startdate": "2020-06-01",
    "enddate": "2021-06-01" 
},
{
    "customerid":4,
    "customerstatus": Pending,
    "address": {
                        "city": "PONTE VEDRA",
                        "state": "FL",
                        "zipcode": "32081"
                    },
    "startdate": "2021-01-01",
    "enddate": "2022-01-01" 
},
{
    "customerid":5,
    "customerstatus": Inactive,
    "address": {
                        "city": "PONTE VEDRA",
                        "state": "FL",
                        "zipcode": "32081"
                    },
    "startdate": "2020-07-15",
    "enddate": "2021-07-15" 
},
{
    "customerid":6,
    "customerstatus": cancelled,
    "address": {
                        "city": "PONTE VEDRA",
                        "state": "FL",
                        "zipcode": "32081"
                    },
    "startdate": "2020-07-15",
    "enddate": "2021-07-15" 
}

现在的要求是,
排除customerstatus处于非活动状态且已取消的结果(不应显示customer 5和customer 6)
仅显示活动和挂起
如果状态为“挂起”,则显示结束日期小于500天且结束日期不大于91天的客户
那么,如何在结果集中只得到customerid1和3呢。

ne5o7dgx

ne5o7dgx1#

您可以结合使用bool查询和range查询来查找基于天数范围的文档。请尝试下面的查询

{
  "from": 0,
  "size": 100,
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "match": {
                  "customerstatus": "pending"
                }
              },
              {
                "range": {
                  "enddate": {
                    "gt": "now-500d/d",
                    "lte": "now+91d/d"
                  }
                }
              }
            ]
          }
        },
        {
          "match": {
            "customerstatus": "active"
          }
        }
      ],
      "must_not": {
        "terms": {
          "customerstatus.keyword": [
            "Inactive",
            "cancelled"
          ]
        }
      }
    }
  }
}

搜索结果将是

"hits": [
      {
        "_index": "67260491",
        "_type": "_doc",
        "_id": "3",
        "_score": 1.6931472,
        "_source": {
          "customerid": 3,
          "customerstatus": "Pending",
          "address": {
            "city": "PONTE VEDRA",
            "state": "FL",
            "zipcode": "32081"
          },
          "startdate": "2020-06-01",
          "enddate": "2021-06-01"
        }
      },
      {
        "_index": "67260491",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.5404451,
        "_source": {
          "customerid": 1,
          "customerstatus": "Active",
          "address": {
            "city": "PONTE VEDRA",
            "state": "FL",
            "zipcode": "32081"
          },
          "startdate": "2020-07-15",
          "enddate": "2021-07-15"
        }
      }
    ]

相关问题