2

I'm getting started with ElasticSearch and I'm getting into troubles (I'm not understanding as It has to be) how to search.

First, I have this two documents:

{
    "took": 133
    "timed_out": false
    "_shards": {
        "total": 5
        "successful": 5
        "failed": 0
    }
    "hits": {
        "total": 2
        "max_score": 1
        "hits": [2]
            0:  {
                "_index": "app"
                "_type": "player"
                "_id": "AVcLCOgAi_gt2Fih02MK"
                "_score": 1
                "_source": {
                    "nickName": "sarasa"
                    "birthDate": "1994-11-05T13:15:30.000Z"
                    "state": "sarasa"
                    "adminState": "sarasa"
                    "id": ""
                    "account": {
                        "type": "yojuego"
                        "id": "asasdfa"
                        "password": "asd fasd"
                    }
                }
            }
            1:  {
                "_index": "app"
                "_type": "player"
                "_id": "AVcQ7JNVi_gt2Fih02MN"
                "_score": 1
                "_source": {
                    "nickName": "facundo"
                    "birthDate": "1994-11-05T13:15:30.000Z"
                    "state": "verdura"
                    "adminState": "sudo"
                    "id": ""
                    "account": {
                        "type": "yojuego"
                        "id": "facundo@facundo"
                        "password": "pepe"
                    }
                }
            }
        }
    }
}

I want to get where account.id = "facundo@facundo" and account.type = "yojuego". I'm doing this:

  client.search({
    index: 'app',
    type: 'player',

    query: {
      bool: {
        must: [
          { term: { "account.id": 'facundo@facundo' } },
          { term: { "account.type": 'yojuego' } }
        ],
      }
    }

  }, (error, response, status) => {
    if (error) {
      res.json(400, err);
    }
    else {
      res.json(200, response.hits.hits);
    }
  });

This search is retrieving all documents I have into the index. Any help?

Thanks!

PD: Here is how I created index and mapping:

  client.indices.create({ index: 'yojuego' }, (err, resp, respcode) => {
    if (!err) {
      client.indices.putMapping({
        index: 'app',
        type: "player",
        body: {
          properties: {
            nickName: { type: "string" },
            birthDate: { type: "string" },
            state: { type: "string" },
            adminState: { type: "string" },
            account: {
              type: "nested",
              properties: {
                id: { type: "string" },
                type: { type: "string" },
                password: { type: "string" }
              }
            }
          }
        }
      }, (err, resp, respcode) => {
        res.json(200, resp);
      });
    }
  });
1
  • can you show your mapping ? account has to be a nested field
    – blackmamba
    Commented Sep 10, 2016 at 2:23

1 Answer 1

1

make sure that account is a nested field and then apply this query,

    {
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "account",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "account.id": "facundo@facundo"
                    }
                  },
                  {
                    "match": {
                      "account.type": "yojuego"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}
1
  • Hi, this query seems to be not compiling. Commented Sep 11, 2016 at 22:26

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.