2012-10-02 15 views
9

Tengo una instancia en la memoria de ejecución de búsqueda elástica y estoy haciendo una codificación exploratoria para aprender la API de búsqueda de Java. Puedo enviar documentos al índice y recuperarlos usando GET, pero cuando intento una consulta de búsqueda simple, no obtengo ningún resultado.elasticsearch java API: ¿coinciden todas las consultas de búsqueda no arrojan resultados?

// 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); 

después de algunas pruebas, creo que el problema está en cómo me he fijado mi nodo y el cliente asociado (en la 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(); 
    } 

Respuesta

22

Alguien en la elástica grupo de búsqueda Google fue tan amable de ayudame aqui. Después de enviar un documento a un nodo en la memoria, necesito actualizar el índice:

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

llamando a refresh corrigió el problema.

+0

¡Gracias por publicar su email aquí! Me ahorró horas! – Alebon

+0

Sí, ES es casi en tiempo real, no es inmediato como una base de datos sql tradicional. – bradvido

Cuestiones relacionadas