2012-10-02 15 views
9

Ho un'istanza in memoria di ricerca elastica in esecuzione e faccio un po 'di codice esplorativo per imparare l'API java di ricerca. Sono in grado di inviare documenti all'indice e recuperarli tramite GET, ma quando provo una query di ricerca semplice, non ottengo alcun risultato.API jastics elasticsearch: matchAll query di ricerca non restituisce risultati?

// first, try a get request, to make sure there is something in the index 
GetResponse results = client.prepareGet(INDEX_NAME, INDEX_TYPE, testID) 
     .execute() 
     .actionGet(); 
// this assertion succeeds, as we expect it to. 
assertThat(results.getId()).isEqualTo(testID); 

// next, try the simplest possible search 
SearchResponse s1 = client.prepareSearch(INDEX_NAME).setQuery(matchAllQuery()) 
     .execute() 
     .actionGet(); 
// this assertion fails. why? answer: when we have an in-memory node, we have to 
// manually call refresh on the indexing, after submitting a document. 
assertThat(s1.getHits().totalHits()).isGreaterThanOrEqualTo(1); 

dopo alcune prove, penso che il problema è nel modo in cui sto installando il mio nodo e client associato (in memoria):

@BeforeMethod 
    public void setup() { 
     // set up elastic search to run locally. since the transaction 
     // log needs a filesystem, we can't run it as purely in memory, 
     // but we can set the data directories into "target", so that maven will 
     // clean up after the fact: http://bit.ly/OTN7Qf 
     Settings settings = ImmutableSettings.settingsBuilder() 
       .put("node.http.enabled", true) 
       .put("path.logs","target/elasticsearch/logs") 
       .put("path.data","target/elasticsearch/data") 
       .put("gateway.type", "none") 
       .put("index.store.type", "memory") 
       .put("index.number_of_shards", 1) 
       .put("index.number_of_replicas", 1).build(); 

     node = NodeBuilder.nodeBuilder().local(true).settings(settings).node(); 
     client = node.client(); 
    } 

risposta

22

Qualcuno sul elastico gruppo di ricerca Google è stato così gentile da aiutami qui Dopo aver inviato un documento a un nodo in memoria, ho bisogno di aggiornare l'indice:

 node.client().admin().indices().prepareRefresh().execute().actionGet(); 

chiamando aggiornamento ha risolto il problema.

+0

Grazie per aver postato qui! Mi ha salvato ore! – Alebon

+0

Sì, ES è quasi in tempo reale, non immediato come un database SQL tradizionale. – bradvido

Problemi correlati