弹性搜索 - 范围为

时间:2017-04-04 12:28:32

标签: elasticsearch

我的搜索结果存在问题。

我的索引上有以下映射:



{
  "index": {
    "mappings": {
      "shop": {
        "properties": {
          "about": {
            "type": "text"
          },
          "address": {
            "type": "text"
          },
          "description": {
            "type": "text"
          },
          "email": {
            "type": "text"
          },
          "location": {
            "type": "geo_point"
          },
          "name": {
            "type": "text"
          },
          "operationHours": {
            "type": "nested",
            "include_in_parent": true,
            "properties": {
              "dayOfWeek": {
                "type": "long"
              },
              "timeRanges": {
                "type": "nested",
                "include_in_parent": true,
                "properties": {
                  "from": {
                    "type": "long"
                  },
                  "to": {
                    "type": "long"
                  }
                }
              }
            }
          },
          "phone": {
            "type": "text"
          },
          "profileImageName": {
            "type": "text"
          }
        }
      }
    }
  }
}




然后我添加了以下文件:



{
    "operationHours": [
      {
        "timeRanges": [
          {
            "to": 720,
            "from": 600
          },
          {
            "to": 1500,
            "from": 840
          }
        ],
        "dayOfWeek": 0
      },
      {
        "timeRanges": [
          {
            "to": 720,
            "from": 700
          },
          {
            "to": 960,
            "from": 840
          }
        ],
        "dayOfWeek": 2
      },
      {
        "timeRanges": [
          {
            "to": 720,
            "from": 600
          },
          {
            "to": 1500,
            "from": 840
          }
        ],
        "dayOfWeek": 4
      },
      {
        "timeRanges": [
          {
            "to": 720,
            "from": 600
          },
          {
            "to": 1500,
            "from": 840
          }
        ],
        "dayOfWeek": 5
      },
      {
        "timeRanges": [
          {
            "to": 720,
            "from": 600
          },
          {
            "to": 1500,
            "from": 840
          }
        ],
        "dayOfWeek": 6
      },
      {
        "timeRanges": [
          {
            "to": 720,
            "from": 600
          },
          {
            "to": 1500,
            "from": 840
          }
        ],
        "dayOfWeek": 7
      }
    ],
    "location": {
      "lon": "-68.72307",
      "lat": "25.463178"
    },
    "profileImageName": "ACRUEX.jpg",
    "address": "620 Classon Avenue, Hendersonville, Mississippi, 4076",
    "description": "Tempor culpa dolore Lorem fugiat dolore esse. Ullamco ipsum dolore amet dolor laboris eu nisi consequat Lorem non mollit minim exercitation. Voluptate ipsum mollit culpa aute sunt consectetur minim anim cupidatat dolor quis labore do amet. Non id voluptate dolore nostrud laboris voluptate consequat aliqua labore.",
    "about": "Non labore culpa do consectetur fugiat velit. Reprehenderit cupidatat nulla veniam exercitation adipisicing amet. Mollit irure voluptate dolor est veniam nulla fugiat elit. Non et deserunt excepteur non officia enim non voluptate qui amet adipisicing quis enim exercitation.",
    "email": "undefined.undefined@undefined.io",
    "phone": "+1 (833) 575-2171",
    "name": "ISOPOP"
}




然后,我使用以下查询来获取操作小时范围大于或等于指定时间的文档(示例中为730)。

730表示从午夜起的分钟数,即730表示下午12:10。



GET index/shop/1/_explain
{   
   "query": { 
     "nested" : {
       "path" : "operationHours",
       "query": {
         "bool" : {
           "must" : [
             { "match" : { "operationHours.dayOfWeek" : 2 } },
             { "range": {"operationHours.timeRanges.from": { "lte": 730 }}},
             { "range": {"operationHours.timeRanges.to": { "gt": 730 }}}
                ]
             
         }
         
       }
     }
   }
}




它出现了匹配,它不应该像' 730'不在700到720和840到960的范围内。

任何有关我出错的地方的帮助都会很棒。

2 个答案:

答案 0 :(得分:0)

“它出现了匹配,它不应该这样做,因为'730'的值不在700到720和840到960的范围内。”

这是检查文档是否与给定范围700到720和840到960的操作时间匹配的正确查询。(我正在考虑它们之间的OR)并且您的文档将不符合此查询。

{
    "query": {
        "nested": {
            "path": "operationHours",
            "query": {
                "bool": {
                    "must": [{
                        "match": {
                            "operationHours.dayOfWeek": 2
                        }
                    }, {
                        "bool": {
                            "should": [{
                                    "range": {
                                        "operationHours.timeRanges.from": {
                                            "lt": 720,
                                            "gt": 700
                                        }
                                    }
                                }, {
                                    "range": {
                                        "operationHours.timeRanges.from": {
                                            "lt": 840,
                                            "gt": 960
                                        }
                                    }
                                }

                            ],
                            "minimum_number_should_match": 1
                        }
                    }]

                }

            }
        }
    }
}

您可以根据用例调整gt to gte

答案 1 :(得分:0)

我明白了。以下内容完全符合我的需要

{
"query": {
    "nested": {
        "path": "operationHours",
        "query": {
            "bool": {
                "must": [{
                    "match": {
                        "operationHours.dayOfWeek": 2
                    }
                }, {
                    "nested": {
                        "path": "operationHours.timeRanges",
                        "query": {
                            "bool": {
                                "must": [{
                                        "range": {
                                            "operationHours.timeRanges.to": {
                                                "gt": 600
                                            }
                                        }
                                    }, {
                                        "range": {
                                            "operationHours.timeRanges.from": {
                                                "lte": 600
                                            }
                                        }
                                    }

                                ]
                            }
                        }
                    }
                }]
            }

        }
    }
}

}