2014-11-04 24 views
9

Ho un problema con l'aggregazione dei risultati dopo averli filtrati. Penso di essere sulla strada giusta ma mi sento come se stessi inseguendo la mia coda.ElasticSearch - Filter Nested Aggregation

Ecco come appare:

PUT /my_index 
{ 
    "mappings": { 
    "reporting": { 
     "properties": { 
     "events": { 
      "type": "nested", 
      "properties": { 
      "name": { "type": "string", "index" : "not_analyzed" }, 
      "date": { "type": "date" } 
      } 
     } 
     } 
    } 
    } 
} 

Quindi, il mio documento si presenta come:

{ 
    "events": [ 
    { "name": "INSTALL", "date": "2014-11-01" }, 
    { "name": "UNINSTALL", "date": "2014-11-03" }, 
    { "name": "INSTALL", "date": "2014-11-04" }, 
    ... 
    ] 
} 

Ora, quando indice di alcuni dati, ad esempio:

PUT /my_index/reporting/1 
{ 
    "events": [ 
    { 
     "name": "INSTALL", 
     "date": "2014-11-01" 
    }, 
    { 
     "name": "UNINSTALL", 
     "date": "2014-11-05" 
    } 
] 
} 

PUT /my_index/reporting/2 
{ 
    "events": [ 
    { 
     "name": "INSTALL", 
     "date": "2014-11-01" 
    }, 
    { 
     "name": "UNINSTALL", 
     "date": "2014-11-03" 
    } 
] 
} 

PUT /my_index/reporting/3 
{ 
    "events": [ 
    { 
     "name": "INSTALL", 
     "date": "2014-11-01" 
    }, 
    { 
     "name": "UNINSTALL", 
     "date": "2014-11-02" 
    } 
] 
} 

PUT /my_index/reporting/4 
{ 
    "events": [ 
    { 
     "name": "INSTALL", 
     "date": "2014-11-01" 
    }, 
    { 
     "name": "UNINSTALL", 
     "date": "2014-11-02" 
    }, 
    { 
     "name": "INSTALL", 
     "date": "2014-11-03" 
    } 
] 
} 

PUT /my_index/reporting/5 
{ 
    "events": [ 
    { 
     "name": "INSTALL", 
     "date": "2014-11-01" 
    }, 
    { 
     "name": "UNINSTALL", 
     "date": "2014-11-03" 
    }, 
    { 
     "name": "INSTALL", 
     "date": "2014-11-03" 
    } 
] 
} 

PUT /my_index/reporting/6 
{ 
    "events": [ 
    { 
     "name": "INSTALL", 
     "date": "2014-11-03" 
    }, 
    { 
     "name": "UNINSTALL", 
     "date": "2014-11-03" 
    }, 
    { 
     "name": "INSTALL", 
     "date": "2014-11-05" 
    } 
] 
} 

PUT /my_index/reporting/7 
{ 
    "events": [ 
    { 
     "name": "INSTALL", 
     "date": "2014-11-02" 
    }, 
    { 
     "name": "UNINSTALL", 
     "date": "2014-11-03" 
    }, 
    { 
     "name": "INSTALL", 
     "date": "2014-11-05" 
    } 
] 
} 

PUT /my_index/reporting/8 
{ 
    "events": [ 
    { 
     "name": "INSTALL", 
     "date": "2014-11-01" 
    } 
] 
} 

I desidera ottenere il numero di coloro che INSTALLATO dopo (compreso) 2014-11-02 e non ha disinstallato (quindi, UNINSTALL era prima 2014-11-02 o non c'è U Evento NINSTALL) e raggrupparli in un significato data_histogram (per avere bucket con dati "date" -> "count").

Sono riuscito a scrivere il filtro su questi dati nidificati, così posso ottenere quel risultato filtrato, ma continuo a rincorrere la mia coda quando si tratta di quell'istogramma di aggregazione.

Questo è il punto in cui mi sono bloccato.

GET /my_index/reporting/_search 
{ 
    "query": { 
    "filtered": { 
     "query": { 
     "match_all": {} 
     }, 
     "filter": { 
     "bool": { 
      "must": [ 
      { 
       "nested": { 
       "path": "events", 
       "filter": { 
        "bool": { 
        "must": [ 
         { 
         "term": { 
          "name": "INSTALL" 
         } 
         }, 
         { 
         "range": { 
          "date": { 
          "gte": "2014-11-02" 
          } 
         } 
         } 
        ] 
        } 
       } 
       } 
      }, 
      { 
       "nested": { 
       "path": "events", 
       "filter": { 
        "bool": { 
        "should": [ 
         { 
         "bool": { 
          "must_not": [ 
          { 
           "term": { 
           "name": "UNINSTALL" 
           } 
          } 
          ] 
         } 
         }, 
         { 
         "bool": { 
          "must": [ 
          { 
           "term": { 
           "name": "UNINSTALL" 
           } 
          }, 
          { 
           "range": { 
           "date": { 
            "lt": "2014-11-02" 
           } 
           } 
          } 
          ] 
         } 
         } 
        ] 
        } 
       } 
       } 
      } 
      ] 
     } 
     } 
    } 
    }, 
    "aggregations": { 
    "filtered_result": { 
     "filter": { 
     "bool": { 
      "must": [ 
      { 
       "nested": { 
       "path": "events", 
       "filter": { 
        "bool": { 
        "must": [ 
         { 
         "term": { 
          "name": "INSTALL" 
         } 
         }, 
         { 
         "range": { 
          "date": { 
          "gte": "2014-11-02" 
          } 
         } 
         } 
        ] 
        } 
       } 
       } 
      }, 
      { 
       "nested": { 
       "path": "events", 
       "filter": { 
        "bool": { 
        "should": [ 
         { 
         "bool": { 
          "must_not": [ 
          { 
           "term": { 
           "name": "UNINSTALL" 
           } 
          } 
          ] 
         } 
         }, 
         { 
         "bool": { 
          "must": [ 
          { 
           "term": { 
           "name": "UNINSTALL" 
           } 
          }, 
          { 
           "range": { 
           "date": { 
            "lt": "2014-11-02" 
           } 
           } 
          } 
          ] 
         } 
         } 
        ] 
        } 
       } 
       } 
      } 
      ] 
     } 
     }, 
     "aggs": { 
     "result": { 
      "nested": { 
      "path": "events" 
      }, 
      "aggs": { 
      "NAME": { 
       "terms": { 
       "field": "events.date", 
       "format": "yyyy-MM-dd", 
       "order": { 
        "_term": "asc" 
       } 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 

E il mio risultato è simile:

... omitted 4 documents that match filter criteria ... 
    "aggregations": { 
     "filtered_result": { 
     "doc_count": 4, <---- this is ok, I really have 4 docs that match criteria 
     "result": { 
      "doc_count": 12, <---- those 4 documents really have 12 events (together) 
      "NAME": { 
       "buckets": [ 
        { 
        "key": 1414800000000, 
        "key_as_string": "2014-11-01", 
        "doc_count": 2 
        }, 
        { 
        "key": 1414886400000, 
        "key_as_string": "2014-11-02", 
        "doc_count": 2 
        }, 
        { 
        "key": 1414972800000, 
        "key_as_string": "2014-11-03", 
        "doc_count": 6 
        }, 
        { 
        "key": 1415145600000, 
        "key_as_string": "2014-11-05", 
        "doc_count": 2 
        } 
       ] 
      } 
     } 
     } 
    } 

e volevo ottenere qualcosa di simile:

"buckets": [ 
{ 
    "key_as_string": "2014-11-02", 
    "doc_count": 0 
}, 
{ 
    "key_as_string": "2014-11-03", 
    "doc_count": 2 
}, 
{ 
    "key_as_string": "2014-11-04", 
    "doc_count": 0 
}, 
{ 
    "key_as_string": "2014-11-05", 
    "doc_count": 2 
} 
] 

In sostanza, 4 documenti che hanno abbinato i criteri sono distribuiti da date in cui si è verificato che i criteri , 2 documenti su "2011-11-03" e due documenti su "2014-11-05" (4 documenti che hanno l'evento "installa" dopo il 2014-11-02 e non hanno avuto l'evento di disinstallazione dopo quello (sono ancora installato)

risposta

1

Questa è una risposta parziale.

C'è un problema principale: in base ai dati, v'è in realtà nessun documento che avrebbe soddisfare le vostre esigenze, così ho aggiunto un po ':

curl -XPUT 'localhost:9200/my_index/reporting/9' -d '{ 
    "events": [ 
    { 
     "name": "INSTALL", 
     "date": "2014-11-03" 
    } 
] 
}' 

curl -XPUT 'localhost:9200/my_index/reporting/10' -d '{ 
    "events": [ 
    { 
     "name": "INSTALL", 
     "date": "2014-11-03" 
    }, 
    { 
     "name": "UNINSTALL", 
     "date": "2014-11-01" 
    } 
    ] 
}' 

Per essere in grado di applicare la logica, ho cambiato lo schema in modo che gli eventi siano inclusi anche nel genitore - in questo modo è possibile cercare "non ci sono eventi UNINSTALL". Perché la cosa è che, in una ricerca annidata, stai sempre guardando un singolo evento, quindi non puoi fare ricerche di tipo "a livello di report".

curl -XPUT 'localhost:9200/my_index' -d '{ 
    "mappings": { 
    "reporting": { 
     "properties": { 
     "events": { 
      "type": "nested", "include_in_root": true, 
      "properties": { 
      "name": { "type": "string", "index" : "not_analyzed" }, 
      "date": { "type": "date" } 
      } 
     } 
     } 
    } 
    } 
}' 

E ora alla query stessa. Sembra che quando si utilizza un filtro annidato, non si può andare direttamente al "filtro". Devi prima fare la cosa "interrogare> filtrare> filtro".

Un consiglio per scrivere query elasticsearch lunghe in generale - ricordando che "e" e "o" gli operatori oltre a "must" e "must_not" - è semplicemente scrivere codice simile. Nel tuo caso:

has_one(event.name == 'INSTALL' && event.date >= '2014-11-02') 
&& has_none(event.name == 'UNINSTALL') 
&& has_none(event.name == 'UNINSTALL' && event.date >= '2014-11-02') 

Oppure:

has_one(event.name == 'INSTALL' && event.date >= '2014-11-02') 
&& (has_none(event.name == 'UNINSTALL') 
    || has_only(event.name == 'UNINSTALL' && event.date >= '2014-11-02')) 

sono stato in grado di applicare tutti, ma l'ultimo has_only/has_none. Per questo, potresti provare a utilizzare i documenti figlio.Qui puoi almeno utilizzare il filtro has_child in un must_not bool.

La query corrente:

GET /my_index/reporting/_search 
{ 
    "query": { 
    "filtered": { 
     "query": { 
     "match_all": {} 
     }, 
     "filter": { 
     "and": { 
      "filters": [ 
      { 
       "or": { 
       "filters": [ 
        { 
        "bool": { 
         "must_not": [ 
         { 
          "term": { 
          "events.name": "UNINSTALL" 
          } 
         } 
         ] 
        } 
        }, 
        { 
        "nested": { 
         "path": "events", 
         "query": { 
         "filtered": { 
          "filter": { 
          "bool": { 
           "must": [ 
           { 
            "term": { 
            "name": "UNINSTALL" 
            } 
           }, 
           { 
            "range": { 
            "date": { 
             "lt": "2014-11-02" 
            } 
            } 
           } 
           ] 
          } 
          } 
         } 
         } 
        } 
        } 
       ] 
       } 
      }, 
      { 
       "nested": { 
       "path": "events", 
       "query": { 
        "filtered": { 
        "filter": { 
         "bool": { 
         "must": [ 
          { 
          "term": { 
           "name": "INSTALL" 
          } 
          }, 
          { 
          "range": { 
           "date": { 
           "gte": "2014-11-02" 
           } 
          } 
          } 
         ] 
         } 
        } 
        } 
       } 
       } 
      } 
      ] 
     } 
     } 
    } 
    }, 
    "aggregations": { 
    "filtered_result": { 
     "filter": { 
     "and": { 
      "filters": [ 
      { 
       "or": { 
       "filters": [ 
        { 
        "bool": { 
         "must_not": [ 
         { 
          "term": { 
          "events.name": "UNINSTALL" 
          } 
         } 
         ] 
        } 
        }, 
        { 
        "nested": { 
         "path": "events", 
         "query": { 
         "filtered": { 
          "filter": { 
          "bool": { 
           "must": [ 
           { 
            "term": { 
            "name": "UNINSTALL" 
            } 
           }, 
           { 
            "range": { 
            "date": { 
             "lt": "2014-11-02" 
            } 
            } 
           } 
           ] 
          } 
          } 
         } 
         } 
        } 
        } 
       ] 
       } 
      }, 
      { 
       "nested": { 
       "path": "events", 
       "query": { 
        "filtered": { 
        "filter": { 
         "bool": { 
         "must": [ 
          { 
          "term": { 
           "name": "INSTALL" 
          } 
          }, 
          { 
          "range": { 
           "date": { 
           "gte": "2014-11-02" 
           } 
          } 
          } 
         ] 
         } 
        } 
        } 
       } 
       } 
      } 
      ] 
     } 
     }, 
     "aggs": { 
     "result": { 
      "nested": { 
      "path": "events" 
      }, 
      "aggs": { 
      "NAME": { 
       "terms": { 
       "field": "date", 
       "format": "yyyy-MM-dd", 
       "order": { 
        "_term": "asc" 
       } 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
}