Problem in executing query in Elasticsearch

0

When trying to search the elasticsearch using elasticsearch.js I used the following code below:

var cliente = new $.es.Client({
    host: 'http://localhost:9200',
    log: 'trace'
});

var cliente = new conectES();
cliente.search({
index: 'usuarios',
body: {
    query:
    {
        fields: ['login'],
        query: '2'  
    }
}// body
}).then(function (resp) {
var hits = resp.hits.hits;
console.log('response:   ' + JSON.stringify(hits));
}, function (err) {
    console.trace(err.message);
});

The expected response to this search was simply that documents with the loginUsuario field contain the 2 value to be returned.

  

Server response Bad request (400)

Expected JSON:

{
    "_index" : "Estrutura",
    "_type" : "usuarios",
    "_id" : "2",
    "_version" : 2,
    "found" : true,
    "_source" : {
        "json" : {
            "login" : "2",
            "nome" : "Foo",
            "sobrenome" : "Latex",
            "endereco" : "Rua 2 ",
            "telefone" : "55 99 999x0-928N",
            "foto" : ""
        }
    }
}
    
asked by anonymous 09.08.2017 / 15:07

1 answer

2

You can change your query to:

{
  "query": {
    "term": {
        "login": "2"
    }
  }
}

With elasticsearch.js

cliente.search({
index: 'usuarios',
body: {
  "query": {
    "term": {
        "login": "2"
    }
  }
}
})

Finally, you can initialize the client as follows:

var elasticsearch = require('elasticsearch');

var client = new elasticsearch.Client({
  host: 'localhost:9200',
  log: 'trace'
});
    
11.08.2017 / 19:43