【问题标题】:use wildcard with Terms in elasticsearch在弹性搜索中使用通配符和术语
【发布时间】:2018-02-27 14:37:33
【问题描述】:

我想模拟 SQL 的 IN 所以我使用了条件过滤器,但是条件不支持通配符,比如在 "*egypt*" 中添加 astrisck。

那么我怎样才能实现下面的查询呢?

PS:我正在使用elastica

{
  "query": {
    "bool": {
      "should": [
        {
          "terms": {
            "country_name": [
              "*egypt*",
              "*italy*"
            ]
          }
        }
      ]
    }
  },
  "sort": [
    {
      "rank": {
        "order": "desc"
      }
    }
  ]
}

【问题讨论】:

标签: elasticsearch


【解决方案1】:

terms 查询不支持通配符。您可以改用matchwildcard 查询。如果您的问题是要过滤多个值,您可以在 should 中组合查询,所以它看起来像这样

{
  "query": {
    "bool": {
      "should": [
        {
          "wildcard": {
            "country_name": "*egypt*"
          }
        },
        {
            "wildcard": {
                "country_name": "*italy*"
            }
        }
      ]
    }
  },
  "sort": [
    {
      "rank": {
        "order": "desc"
      }
    }
  ]
}

【讨论】:

    猜你喜欢
    • 2014-11-18
    • 2014-06-02
    • 2012-10-11
    • 2014-03-02
    • 2021-05-22
    • 1970-01-01
    • 2015-08-15
    • 2021-07-25
    • 1970-01-01
    相关资源
    最近更新 更多