java - elasticsearch中的“匹配或空”查询
问题描述
假设我有一个包含 2010 年到 2019 年上映的所有电影的索引;如何在 SQL 中将此查询转换为 ElasticSearch?
Select *
From movies
Where
releaseDate between '2018-01-01' and '2019-01-01' and
gender is like 'action' and
(mainActorId = 42 or mainActorId is null)
我想要 2018 年的所有动作片,有特定的主要演员或根本没有主要演员。我如何将其转换为 ElasticSearch 查询?
从我目前在文档中阅读的内容来看,我可以使用如下内容:
{
"size":0,
"query":{
"bool":{
"must":[
{
"range":{
"releaseDate":{
"from":"2018-01-01T00:00:01.531Z",
"to":"2019-01-01T00:00:01.531Z",
"include_lower":true,
"include_upper":true,
"boost":1.0
}
}
},
{
"terms":{
"gender":[
"action"
],
"boost":1.0
}
},
{
"terms":{
"mainActorId":[
42,
56
],
"boost":1.0
}
}
],
"must_not":[
{
"exists":{
"field":"mainActorId",
"boost":1.0
}
}
],
"adjust_pure_negative":true,
"boost":1.0
}
}
}
但这并没有给我带来任何成功,即使 2018 年上映的动作片中有我想要的主要演员(或者根本没有主要演员)。如果我去掉“must_not exist”子句,查询就可以正常工作,并为我提供我想要的主要演员的动作片,但我也想要没有主要演员的动作片......
解决方案
到目前为止很好的开始!您快到了,请参阅下面的查询,它应该符合您的预期:
{
"size": 0,
"query": {
"bool": {
"filter": [
{
"range": {
"releaseDate": {
"from": "2018-01-01T00:00:01.531Z",
"to": "2019-01-01T00:00:01.531Z",
"include_lower": true,
"include_upper": true,
"boost": 1
}
}
},
{
"terms": {
"gender": [
"action"
],
"boost": 1
}
}
],
"should": [
{
"bool": {
"must_not": [
{
"exists": {
"field": "mainActorId"
}
}
]
}
},
{
"terms": {
"mainActorId": [
42,
56
]
}
}
],
"minimum_should_match": 1,
"adjust_pure_negative": true,
"boost": 1
}
}
}
推荐阅读
- c# - 如何使用 Xamarin 表单中的 POST 调用将文件对象发送到 Web api。?
- javascript - 有什么方法可以仅针对单个 Web 应用程序禁用 MAC 中的两个手指滚动?
- vb.net - 使用 TimerTick 的经过时间 - 时间格式
- asp.net - 如何删除实体框架中的通用列表
- java - 了解 ArrayList 的数组
- java - EHCache - org.ehcache.StateTransitionException:持久性目录已被另一个进程锁定
- android - React Native 3 按钮和 3 模态
- python - 按组进行的多变量回归不起作用 - 回归摘要重复相同的结果
- jquery - 无法提供固定位置以在下拉列表中添加新选项
- mysql - 如何在不使用子查询的情况下编写查询?