【问题标题】:Elasticsearch Query with And & Or in the Same Query - Bool Query?在同一查询中使用 And & Or 进行 Elasticsearch 查询 - Bool 查询?
【发布时间】:2017-01-21 11:31:03
【问题描述】:

我使用的是 elastic 2.2 和 Sense。

我有以下问题要澄清我的一种想法。

Elasticsearch 是一个全文搜索引擎,而不是一个 SQL 数据库,但是在 ES 查询中仍然可以考虑代数(AND、OR、WHERE 等)。

我不在乎这方面的分数。

假设我有以下平面文档类型客户,其中所有字段都未分析,映射中的属性是:

"properties": {
          "address": {
            "type": "string",
            "index": "not_analyzed"
          },
          "age": {
            "type": "long"
          },
          "customerid": {
            "type": "string",
            "index": "not_analyzed"
          },
          "firstname": {
            "type": "string",
            "index": "not_analyzed"
          },
          "lastname": {
            "type": "string",
            "index": "not_analyzed"
          },
          "updated": {
            "type": "date",
            "format": "date_hour_minute_second_millis"
          }
        }

现在我索引:

PUT customers_ds/customers/1
{
  "customerid": "1",
  "firstname": "Marie",
  "address": "Brazil",
  "updated": "2017-01-13T02:36:37.292",
  "lastname": "Doe",
  "age": 20 
}



PUT customers_ds/customers/2
{
  "customerid": "1",
  "firstname": "John",
  "address": "North America",
  "updated": "2017-01-13T02:36:37.292",
  "lastname": "Doe",
  "age": 25 
}

我应该写什么查询:

我希望所有名字为 Marie 或居住在“北美”的客户

select * from customers where firstname = "Marie" or address = "North America"

这样的ES查询怎么写?

最后,怎么写:

select * from customers where firstname = "Marie" and lastname = "Doe" or address = "North America"

在最后一个查询中,我想从 ES 中返回:

玛丽和约翰,因为 OR。或 address = "North America",而 Mary 将在 AND 中匹配。让我知道这是否清楚。

???

【问题讨论】:

  • "分析所有字段的地方" => 映射中的所有字符串字段都是not_analyzed。你能澄清一下吗?
  • HI @Val - 我的意思是所有字段都没有被分析。我现在正在编辑它

标签: java sql elasticsearch lucene full-text-search


【解决方案1】:

如果你想要一个 OR 查询,你可以这样做:

{
   "query": {
      "bool": {
         "minimum_should_match": 1,
         "should": [
            {
               "term": {
                  "firstname": "Marie"
               }
            },
            {
               "term": {
                  "address": "North America"
               }
            }
         ]
      }
   }
}

如果你想要一个 AND 查询,你可以简单地将 should 替换为 filter(或者 must,如果你关心分数):

{
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "firstname": "Marie"
                }
              },
              {
                "term": {
                  "lastname": "Doe"
                }
              }
            ]
          }
        },
        {
          "term": {
            "address": "North America"
          }
        }
      ]
    }
  }
}

【讨论】:

  • 嗨@Val - 是的,如果我想在同一个查询中使用 OR 与 OR:从客户中选择 * 其中 firstname = "Marie" and lastname = "Doe" or address = "North America" 所以我应该回到这个例子,Marie 和 JOhn,因为 John 的 lastname = Doe,并且住在北美,那就是 OR
  • 你知道如何在同一个查询中获取 AND 和 OR,就像上面的例子一样吗?
  • 这就是我在第二个查询中所做的
  • 好的@Val,但是我没有通过运行这个查询看到两个文档,只有玛丽回来了。我也期待看到约翰(或地址 =“北美”)_____ 第二个查询只给了我玛丽,地址是“巴西”。
  • 我已添加"minimum_should_match": 1,请重试
【解决方案2】:

对于 OR 查询,请使用过滤器,我们不计分

{
    "filtered" : {
        "query" : {
            "term" : { "firstname" : "Marie" }
        },
        "filter" : {
            "or" : [
                {
                    "term" : { "address" : "north america" }
                }
            ]
        }
    }
}

【讨论】:

    猜你喜欢
    • 2023-03-18
    • 2018-08-04
    • 1970-01-01
    • 2021-12-23
    • 2022-10-04
    • 2020-07-11
    • 2018-06-03
    • 1970-01-01
    • 2016-10-08
    相关资源
    最近更新 更多