2013-12-16 13 views
9

Il seguente range_query restituisce un risultato come previsto:query in elasticsearch con intervalli multipli su più date

{"query": { 
    "bool": { 
     "must": [ 
     { 
      "range": { 
      "created_at": { 
       "gte": "2013-12-09" 
      } 
      } 
     } 
     ] 
    } 
    } 
} 

Ma e-ing insieme diverse query gamma, restituisce nulla:

{"query": { 
    "bool":{ 
     "must": [ 
     { 
      "and": [ 
      { 
       "range": { 
       "created_at": { 
        "gte": "2013-12-09" 
       } 
       } 
      }, 
      { 
       "range": { 
       "happens_on": { 
        "lte": "2013-12-16" 
       } 
       } 
      }, 
      { 
       "range": { 
       "created_at": { 
        "lte": "2013-12-14" 
       } 
       } 
      } 
      ] 
     } 
     ] 
    } 
    } 
} 

Qual è la modo corretto di utilizzare più range_queries su più campi?

MODIFICA: Ah, ok, quindi è qui che utilizzo un filtro_filario invece che range_query? Sembrava promettente, quindi ho riscritto la mia query utilizzando solo un filtro a intervallo singolo. Pubblicando tutto qui, nel caso in cui sto rovinando la query da qualche altra parte. Sto eseguendo una GET, e tutto all'interno della chiave sorgente è in realtà JSON, ma ho rimosso il sfuggito i trattini per migliorare la leggibilità:

{ 
    "source": { 
    "filtered": { 
     "filter": { 
     "and": [ 
      { 
      "term": { 
       "restricted": false 
      } 
      }, 
      { 
      "not": { 
       "term": { 
       "deleted": true 
       } 
      } 
      }, 
      { 
      "range": { 
       "happens_on": { 
       "lte": "2013-12-16" 
       } 
      } 
      } 
     ] 
     }, 
     "query": { 
     "bool": { 
      "must": [ 
      ] 
     } 
     } 
    }, 
    "from": 0, 
    "size": 10 
    } 
} 

Purtroppo, il mio problema è sempre lo stesso: non sto ottenendo alcun risultato .

EDIT2: Quindi, scendendo nel vicolo di intervalli all'interno di una clausola obbligata come suggeriva Njal. Questo mi dà una query multi-range come questa:

{ 
    "source": { 
    "filter": { 
     "and": [ 
     { 
      "term": { 
      "restricted": false 
      } 
     }, 
     { 
      "not": { 
      "term": { 
       "deleted": true 
      } 
      } 
     } 
     ] 
    }, 
    "from": 0, 
    "query": { 
     "bool": { 
     "must": [ 
      { 
      "range": { 
       "happens_on": { 
       "gte": "2013-12-06" 
       } 
      } 
      }, 
      { 
      "range": { 
       "created_at": { 
       "gte": "2013-12-15" 
       } 
      } 
      }, 
      { 
      "range": { 
       "happens_on": { 
       "lte": "2013-12-17" 
       } 
      } 
      }, 
      { 
      "range": { 
       "created_at": { 
       "lte": "2013-12-17" 
       } 
      } 
      } 
     ] 
     } 
    }, 
    "size": 10 
    } 
} 

Ancora nessun risultato restituito. Sto facendo degli errori evidenti qui?

risposta

12

Sotto la clausola must delle query bool non è necessario eseguire il wrapping in un and. Non c'è una query and, forse stavi pensando allo and filter?

Example runnable play comandi come ricciolo per comodità:

#!/bin/bash 

export ELASTICSEARCH_ENDPOINT="http://localhost:9200" 

# Create indexes 

curl -XPUT "$ELASTICSEARCH_ENDPOINT/play" -d '{ 
    "settings": {}, 
    "mappings": { 
     "type": { 
      "properties": { 
       "created_at": { 
        "type": "date", 
        "format": "dateOptionalTime" 
       }, 
       "name": { 
        "type": "string" 
       }, 
       "happens_on": { 
        "type": "date", 
        "format": "dateOptionalTime" 
       } 
      } 
     } 
    } 
}' 


# Index documents 
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d ' 
{"index":{"_index":"play","_type":"type"}} 
{"name":"foo","created_at":"2013-12-09T00:00:00.000Z","happens_on":"2013-12-16T00:00:00.000Z"} 
{"index":{"_index":"play","_type":"type"}} 
{"name":"bar","created_at":"2013-12-08T00:00:00.000Z","happens_on":"2013-12-16T00:00:00.000Z"} 
{"index":{"_index":"play","_type":"type"}} 
{"name":"bar","created_at":"2013-12-09T00:00:00.000Z","happens_on":"2013-12-17T00:00:00.000Z"} 
' 

# Do searches 

curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d ' 
{ 
    "query": { 
     "bool": { 
      "must": [ 
       { 
        "range": { 
         "created_at": { 
          "gte": "2013-12-09T00:00:00.000Z" 
         } 
        } 
       }, 
       { 
        "range": { 
         "happens_on": { 
          "lte": "2013-12-16T00:00:00.000Z" 
         } 
        } 
       } 
      ] 
     } 
    } 
} 
' 
+0

Grazie per la risposta e scusa per la mia confusione su questo. Cercherò di annidare i miei intervalli all'interno della clausola must. – thomax

+0

Ok, ho provato il tuo suggerimento, vedi il mio EDIT2 nella domanda. Ancora nessun risultato. Potrebbe essere correlato alla formattazione della data? – thomax

+1

Riiiight. Così è venuto fuori il motivo per cui il suggerimento di Njals apparentemente non ha funzionato per me è che il mio test è stato incasinato. Ora il test è fisso e le rocce di soluzione. Grazie! – thomax

Problemi correlati