Query execution problem in Elasticsearch

Asked

Viewed 64 times

0

When trying to do a search on elasticsearch using the 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 research was simply that the documents with the field loginUsuario, contain the value 2 were returned.

Server response Bad request (400)

JSON expected:

{
    "_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" : ""
        }
    }
}

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'
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.