json - Elasticsearch中的AND查询

标签 json search indexing elasticsearch lucene

我正在尝试按2个字段过滤查询,但始终会出错。我正在使用Elasticsearch文档建议的AND查询(实际上是“ bool(boolean) ”查询),在这里-
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"   }}
      }
    }
  }
}

这有效-
    GET /index_v1/user/_search
{
  "query": {
    "filtered": {
      "query": {
          "match": {
              "id": "101"               
           }
      }
    }
  }
}

并返回此-
    {
   "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"
               }
            }
         }
      ]
   }
}

这是映射文件-
{
   "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"
                     }
                  }
               }
            }
         }
      }
   }
}

另外,如何将其添加到AND条件
["tenants"]["id"]="12345"

最佳答案

您必须运行过滤查询才能使用过滤器。您需要的相关示例是here

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

大致上应该如此,不过我敢肯定您必须对其进行调整(我有些生疏了)。

为了使id字段完全匹配,您需要将那些字段设置为在映射中作为关键字进行分析,否则ES会尝试使其变得聪明,并为您带来意想不到的结果。

关于json - Elasticsearch中的AND查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35122467/

相关文章:

python - 如何在列表中搜索字典键

INSERT INTO...ON DUPLICATE UPDATE 的 MySQL 索引

postgresql - 如何让 date_part 查询命中索引?

json - 是否可以将 JSON 部分反序列化为结构?

java - 如何解决 JSONParser() 错误?

python - 在 Python 中加载 JSON 作为 header 字符集

database - ElasticSearch-在字段总术语中使用minimum_should_match百分比

java - ConceptNet 数据库与 Java 的连接

sql - 如何使用sql查找字符串中特定字符的出现次数?

python - 从字典中删除索引