2017-09-14 70 views
0

我遇到了一個奇怪的問題。我有一個文檔映射,其中一個屬性是嵌套對象。ElasticSearch查詢不會返回具有「空」嵌套屬性的文檔

{ 
    "userLog": { 
     "properties": { 
      "userInfo": { 
       "userId": { 
        "type": "text" 
       }, 
       "firstName": { 
        "type": "text" 
       }, 
       "lastName": { 
        "type": "text" 
       }, 
       "email": { 
        "type": "text" 
       } 
      }, 
      "violations": { 
       "type": "integer" 
      }, 
      "malfunctions": { 
       "type": "integer" 
      }, 
      "extensionsUsed": { 
       "type": "integer" 
      }, 
      "date": { 
       "type": "date", 
       "format": "yyyy-MM-dd||yyyy/MM/dd||yyyyMMdd||epoch_millis" 
      }, 
      "events": { 
       "type": "nested", 
       "properties": { 
        "editorId": { 
         "type": "text" 
        }, 
        "editorRole": { 
         "type": "text" 
        }, 
        "editedTimestamp": { 
         "type": "date", 
         "format": "epoch_millis" 
        }, 
        "createdTimestamp": { 
         "type": "date", 
         "format": "epoch_millis" 
        }, 
        "userId": { 
         "type": "text" 
        }, 
        "timestamp": { 
         "type": "date", 
         "format": "epoch_millis" 
        }, 
        "eventType": { 
         "type": "text" 
        } 
       } 
      } 
     } 
    } 
} 

某些用戶日誌有事件,有些用戶日誌沒有。我的查詢只返回有事件的用戶日誌,但是,我不知道爲什麼。在索引中肯定存在沒有事件的userLog。我可以在基巴納見到他們。他們只是沒有在搜索中返回。下面是我在競選的查詢:基於this discussion

我修改我的查詢

GET index_name/_search 
{ 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "range": { 
      "date": { 
       "gte": "20170913", 
       "format": "yyyyMMdd" 
      } 
      } 
     } 
     ], 
     "should": [ 
     { 
      "match_phrase": { 
      "userInfo.userId": "Xvo9qblajOVaM3bQQMaV4GKk7S42" 
      } 
     } 
     ], 
     "minimum_number_should_match": 1 
    } 
    } 
} 

是以下幾點:

GET one20_eld_portal/_search 
{ 
    "query": { 
    "bool": { 
     "must_not": [ 
     { 
      "nested": { 
      "path": "events", 
      "query": { 
       "bool": { 
       "filter": { 
        "exists": { 
        "field": "events.userId" 
        } 
       } 
       } 
      } 
      } 
     } 
     ], 
     "should": [ 
      { 
      "match_phrase": { 
       "userInfo.uid": "Xvo9qblajOVaM3bQQMaV4GKk7S42" 
      } 
      } 
     ], 
     "minimum_should_match": 1 
     } 
    } 
} 

但這並不返回任何結果 。任何幫助是極大的讚賞!

+1

您已發佈兩個查詢。至於我收集的內容,第一個查詢正在運行,第二個查詢不是?糾正我,如果我錯了。 – Richa

+0

@Richa我只是顯示我迄今爲止嘗試過的。第二個查詢不返回任何結果,但第一個只返回非空事件的結果 – beardo34

+0

還有一個問題。文檔應該滿足什麼條件?根據第一個查詢'date'應該大於一個給定的值並且userInfo.userId應該是'Xvo9qblajOVaM3bQQMaV4GKk7S42'。第二個查詢表示events.userId不能存在,userInfo.userId應該是'Xvo9qblajOVaM3bQQMaV4GKk7S42'。兩者都不同。你能簡單地解釋一下嗎?我們將嘗試將其轉換爲彈性查詢 – Richa

回答

0

原因是沒有返回「空」日誌是因爲沒有爲空日誌正確設置userId。