ElasticSearch JS客户端搜索 [英] ElasticSearch JS Client Search by

查看:122
本文介绍了ElasticSearch JS客户端搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开始使用ElasticSearch,而且我遇到麻烦(我不明白为止)如何搜索。

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"
                    }
                }
            }
        }
    }
}

我想要在哪里找到account.id =facundo @ facundo和account.type =yojuego。
我这样做:

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?

谢谢!

PD:我是如何创建索引和映射的:

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


推荐答案

确保该帐户是一个嵌套字段,然后应用这个查询

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"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

这篇关于ElasticSearch JS客户端搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆