使用弹性搜索设计搜索服务

时间:2018-03-03 00:51:00

标签: rest web-services elasticsearch design-patterns

我有一个要求。我正在为社交网络建立搜索服务。搜索服务应返回有人搜索的用户的名称。现在它仅限于用户域搜索。我打算使用弹性搜索来保留索引(用户域详细信息)。然后我将从我的搜索服务中调用EL(搜索服务在nodejs上)。我无法想到如何为EL创建索引的设计。我应该使用批处理来创建索引,还是在创建用户期间我将创建索引。

赞赏一个好的指针或好的设计。

1 个答案:

答案 0 :(得分:0)

你应该在新用户创建时创建索引

简单的例子可能很有用

您的用户数据如: `

[
 {
    "_id": "1",
    "status": true,
    "username": "mak",
    "userdomain": "mydomain.com",
    "name": "mak doe"
 },
 {
    "_id": "2",
    "status": true,
    "username": "janny",
    "userdomain": "mydomain.com",
    "name": "janny"
 },
 {
    "_id": "3",
    "status": true,
    "username": "mac",
    "userdomain": "newdomain.com",
    "name": "mac peter"
 },
 {
    "_id": "4",
    "status": true,
    "username": "mak",
    "userdomain": "mydomain.com",
    "name": "mak peter"
 },
 {
       "id": "5",            
       "status": true,
       "username": "mak",
       "userdomain": "newdomain.com",
       "name": "mak peter"
 },
]

`

弹性模式如下所示: `

PUT socialdata
{
    "mappings": {
          "users": {
         "properties": {
            "status": {
               "type": "boolean"
            },
            "name": {
               "type": "text"
            },
            "username": {
               "type": "string",
               "fields": {
                  "raw": {
                     "type": "string",
                     "analyzer": "keyword_lowercase_analyzer"
                  },
                  "english": {
                     "type": "text",
                     "analyzer": "english"
                  }
               }
            },
            "userdomain": {
               "type": "string",
               "fields": {
                  "raw": {
                     "type": "string",
                     "analyzer": "keyword_lowercase_analyzer"
                  },
                  "english": {
                     "type": "text",
                     "analyzer": "english"
                  }
               }
            }
        }
       }
    }
}

`

批量上传: `

POST socialdata/users/_bulk
{ "index": { "_index": "socialdata","_type": "users", "_id": 1 }}
{"status":true,"username": "mak","userdomain": "mydomain.com","name": "mak doe"}
{ "index": { "_index": "socialdata","_type": "users", "_id": 2 }}
{"status":true,"username": "janny","userdomain": "mydomain.com","name": "janny"}
{ "index": { "_index": "socialdata","_type": "users", "_id": 3 }}
{"status":true,"username": "mac","userdomain": "newdomain.com","name": "mac peter"}
{ "index": { "_index": "socialdata","_type": "users", "_id": 4 }}
{"status":true,"username": "mak","userdomain": "mydomain.com","name": "mak peter"}
{ "index": { "_index": "socialdata","_type": "users", "_id": 5 }}
{"status":true,"username": "mak","userdomain": "newdomain.com","name": "mak peter"}

`

单个索引的

: `

POST socialdata/users/_bulk
{ "index": { "_index": "socialdata","_type": "users", "_id": 1 }}
{"status":true,"username": "mak","userdomain": "mydomain.com","name": "mak doe"}

`

弹性查询: 它只会返回两个记录 `

POST socialdata/users/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "username": "mak"
          }
        },
        {
          "match": {
            "userdomain": "mydomain.com"
          }
        }
      ]
    }
  }
}

`

它只会返回一条记录 `

POST socialdata/users/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "username": "mak"
          }
        },
        {
          "match": {
            "userdomain": "newdomain.com"
          }
        }
      ]
    }
  }
}

`