ElasticSearch:查询数组中的多个值

ElasticSearch:查询数组中的多个值

问题描述:

我正在尝试将弹性搜索的查询/过滤器放在一起。这里是什么结构看起来像ElasticSearch:查询数组中的多个值

{ 
    _id:"0872300234", 
    customers:[ 
     { 
     name:"bob", 
     priority:1, 
     type:"GoodUser" 
     }, 
     { 
     name:"dan", 
     priority:10, 
     type:"BadUser" 
     }, 
     { 
     name:"sam", 
     priority:10, 
     type:"GoodUser" 
     }, 
     { 
     name:"cam", 
     priority:2, 
     type:"BadUser" 
     } 
    ] 
} 

因此,让我们称之为“配置文件”文件/记录。我想找到所有拥有客户的配置文件,其优先级为10,并且是“goodUser”(同一客户),所以在示例中sam会匹配但dan不会。我收到了一个查询,该查询为我提供了一个配置文件,其中客户的优先级为10,而客户(不是同一个)的类型为GoodUser。

有没有一种方法来查询一个数组项目作为一个整体。

谢谢

您需要嵌套类型。有关嵌套对象的更多信息,请参见here,以及在嵌套字段之间的关联很重要的情况下需要它们的原因。在你的情况,映射需要看起来像这样:

{ 
    "mappings": { 
    "profile": { 
     "properties": { 
     "customers": { 
      "type": "nested", 
      "properties": { 
      "name": { 
       "type": "string" 
      }, 
      "priority": { 
       "type": "integer" 
      }, 
      "type": { 
       "type": "string" 
      } 
      } 
     } 
     } 
    } 
    } 
} 

和查询需要嵌套,以及:

{ 
    "query": { 
    "nested": { 
     "path": "customers", 
     "query": { 
     "bool": { 
      "must": [ 
      {"match": { 
       "customers.type": "goodUser" 
      }}, 
      {"term": { 
       "customers.priority": { 
       "value": 10 
       } 
      }} 
      ] 
     } 
     } 
    } 
    } 
} 
+0

嗯一直想知道有什么不同对象和嵌套之间。谢谢... – Pintac 2014-10-28 14:16:33