ElasticSearch 2.4 - 主要类型和嵌套类型之间的查询字符串或

ElasticSearch 2.4 - Query String OR between main type and nested

我正在使用 ElasticSearch 2.4

我需要在主对象和嵌套对象之间创建一个搜索查询,如果我使用 AND 条件它可以正常工作,但问题是如果我尝试在主对象和嵌套对象之间使用 OR 条件: 请查看下面的代码并告诉我是否有办法使用 OR 条件使其工作。

创建映射:

PUT /example_contact_purchases
{
  "mappings": {
    "contact": {
      "dynamic": false,
      "properties": {
        "name": {
          "type": "string"
        },
        "country": {
          "type": "string"
        },
        "purchases": {
          "type": "nested",
          "properties": {
            "uuid":{
              "type":"string"
            }
          }
        }
      }
    }
  }
}

映射结果:

    GET example_contact_purchases/_mapping
    {
  "example_contact_purchases": {
    "mappings": {
      "contact": {
        "dynamic": "false",
        "properties": {
          "country": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "purchases": {
            "type": "nested",
            "properties": {
              "uuid": {
                "type": "string"
              }
            }
          }
        }
      }
    }
  }
}

创建第一个项目:

POST example_contact_purchases/contact
{
  "name" : "Fran",
  "country": "ES",
  "purchases" : [
    {
      "uuid" : "23"
    }
  ]
}

创建第二个项目:

POST example_contact_purchases/contact
{
  "name" : "Jhon",
  "country": "UK",
  "purchases" : [
    {
      "uuid" : "45"
    }
  ]
}

创建第三个项目:

POST example_contact_purchases/contact
{
  "name" : "Leonardo",
  "country": "IT",
  "purchases" : [
    {
      "uuid" : "45"
    }
  ]
}

示例查询:Country == ES AND purchase.uuid == 23

GET example_contact_purchases/_search
{ 
   "query":{ 
      "filtered":{ 
         "query":{ 
            "query_string":{ 
               "query":"country:ES"
            }
         }
      }
   },
   "filter":{ 
      "nested":{ 
         "path":"purchases",
         "filter":{ 
            "query":{ 
               "query_string":{ 
                  "query":"(purchases.uuid:23)"
               }
            }
         }
      }
   }
}

结果:

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "example_contact_purchases",
        "_type": "contact",
        "_id": "AW_nkURti9zva2kl7ESR",
        "_score": 1,
        "_source": {
          "name": "Fran",
          "country": "ES",
          "purchases": [
            {
              "uuid": "23"
            }
          ]
        }
      }
    ]
  }
}

目标查询:国家== "ES" 或 purchase.uuid== 45

您可以使用 bool 查询来做到这一点。您的查询将如下所示:

POST example_contact_purchases/contact/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "query_string": {
            "query": "country:ES"
          }
        },
        {
          "nested": {
            "path": "purchases",
            "filter": {
              "query": {
                "query_string": {
                  "query": "(purchases.uuid:45)"
                }
              }
            }
          }
        }
      ]
    }
  }
}

请注意,在这种情况下 should == OR

此外,由于您正在按精确值查询这些字段(countrypurchases.uuid),您可以考虑将它们设置为 not_analyzed (or keyword in modern versions of Elasticsearch) and use exact match query like term

希望对您有所帮助!