简体   繁体   English

Elasticsearch中的AND查询

[英]AND query in Elasticsearch

I'm trying to filter my query by 2 fields, but keep getting error. 我正在尝试按2个字段过滤查询,但始终会出错。 I'm using the AND query as suggested by Elasticsearch docs (it's actually a 'bool' query), here- https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-post-filter.html 我正在使用Elasticsearch文档建议的AND查询(实际上是一个“布尔”查询),这里-https : //www.elastic.co/guide/en/elasticsearch/reference/current/search-request-post- filter.html

GET /index_v1/user/_search
{
  "query": {
    "bool": {
      "filter": {
        { "term": { "id": "101"   }},
        { "term": { "firstName": "John"   }}
      }
    }
  }
}

This works- 这有效-

    GET /index_v1/user/_search
{
  "query": {
    "filtered": {
      "query": {
          "match": {
              "id": "101"               
           }
      }
    }
  }
}

and returns this- 并返回此-

    {
   "took": 24,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 3.442347,
      "hits": [
         {
            "_index": "index_v1",
            "_type": "user",
            "_id": "1",
            "_score": 3.442347,
            "_source": {
               "id": "101",
               "firstName": "John",
               "guid": "1001",
               "lastName": "Doe",
               "email": "john.doe@company.com",
               "entitlements": {
                  "id": "en2"
               }
            }
         },
         {
            "_index": "index_v1",
            "_type": "user",
            "_id": "2",
            "_score": 3.140066,
            "_source": {
               "id": "101",
               "firstName": "John",
               "guid": "1001",
               "lastName": "Doe",
               "email": "john.doe@company.com",
               "tenants": [
                  {
                     "id": "12345",
                     "roles": [
                        "PrimaryAdmin"
                     ]
                  }
               ],
               "entitlements": {
                  "id": "en2"
               }
            }
         }
      ]
   }
}

Here's the mapping document- 这是映射文件-

{
   "index_v1": {
      "mappings": {
         "user": {
            "properties": {
               "email": {
                  "type": "string"
               },
               "entitlements": {
                  "properties": {
                     "id": {
                        "type": "string"
                     }
                  }
               },
               "firstName": {
                  "type": "string"
               },
               "guid": {
                  "type": "string"
               },
               "id": {
                  "type": "string"
               },
               "lastName": {
                  "type": "string"
               },
               "tenants": {
                  "properties": {
                     "id": {
                        "type": "string"
                     },
                     "roles": {
                        "type": "string"
                     }
                  }
               }
            }
         }
      }
   }
}

Also, how can I add this to AND condition 另外,如何将其添加到AND条件

["tenants"]["id"]="12345"

You have to run a filtered query to use filters. 您必须运行过滤查询才能使用过滤器。 The relevant example you'll want is here . 您想要的相关示例在这里

GET /index_v1/user/_search
{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "and": [
          { "term": { "id": "101" }},
          { "term": { "firstName": "John" }},
          { "term": { "tenants.id": "12345" }}
        ]
      }
    }
  }
}

That should be roughly it, though I'm sure you'll have to tweak it (I'm a little rusty). 大概就可以了,尽管我确定您必须对其进行调整(我有些生疏了)。

In order for the id fields to match exactly, you'll want to set those fields to be analyzed as keywords in the mapping, otherwise ES will try to get smart with it and give you unexpected results. 为了使id字段完全匹配,您需要将那些字段设置为要在映射中作为关键字进行分析,否则ES会尝试使其变得更聪明并为您带来意想不到的结果。

The query posted by Nick Larson should work fine, but as far as exactly what is wrong with your query, you are using curly brackets where you should be using square brackets (it's actually invalid JSON syntax, in it's current form). 尼克·拉尔森(Nick Larson)发布的查询应该可以正常工作,但是就查询到底有什么问题,您使用括号,而应使用括号(实际上是无效的JSON语法,采用当前格式)。 "filter" should be an array, so you have to use square brackets: "filter"应为数组,因此必须使用方括号:

GET /index_v1/user/_search
{
  "query": {
    "bool": {
      "filter": [
        { "term": { "id": "101"   }},
        { "term": { "firstName": "John"   }}
      ]
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM