2017-04-04 100 views
0

我的搜索結果存在問題。彈性搜索 - 與範圍數組嵌套匹配

我有以下我的索引映射:

{ 
 
    "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": "[email protected]", 
 
    "phone": "+1 (833) 575-2171", 
 
    "name": "ISOPOP" 
 
}

然後,我使用以下查詢獲取運行時間範圍大於或等於指定時間的文檔(示例中爲730)。

730表示從午夜開始的分鐘數,即730是12:10 pm。

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

任何幫助犯規值至於我哪裏出錯會很好。

+0

你有什麼想說的,你能不能請重新檢查在查詢自己的價值觀。 – user3775217

+0

什麼林試圖說,是我想返回的文件必須和範圍覆蓋我通過一個int值。例如,上面的文檔對於dayOfWeek = 2的值爲700和720。如果我通過710,則必須退回文件。如果我通過730,則不得退回文件。 – TheDaveJay

回答

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 
              } 
             } 
            } 

           ] 
          } 
         } 
        } 
       }] 
      } 

     } 
    } 
} 

}

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

+0

問題在於我不知道lt/gt範圍。我所有的是730時間戳,所以我需要知道730是否落入到/從值之間。 – TheDaveJay