弹性搜索别名是否有禁用字符?

时间:2016-12-23 15:50:51

标签: elasticsearch

我正在尝试fake index per user,但我的用户ID是电子邮件。当我的别名中有@时,搜索为空。

例如,如果我创建一个索引my_index和两个别名my_aliasmy@lias,如下所示,my_alias搜索工作正常,我的@ lias不是:

curl -XPUT 'elasticsearch:9200/my_index'
{"acknowledged":true}

curl -XPUT 'elasticsearch:9200/my_index/_alias/my_alias -d '{"routing": "my_alias", "filter": {"term": {"doc_id": "my_alias"}}}'
{"acknowledged":true}

curl -XPUT 'elasticsearch:9200/my_index/_alias/my@lias' -d '{"routing": "my@lias", "filter": {"term": {"doc_id": "my@lias"}}}'
{"acknowledged":true}

curl -XPUT 'elasticsearch:9200/my_alias/doc/1' -d '{doc_id:"my_alias", title: "hello world"}'
{"_index":"my_index","_type":"doc","_id":"1","_version":1,"_shards":{"total":2,"successful":1,"failed":0},"created":true}

curl -XPUT 'elasticsearch:9200/my@lias/doc/2' -d '{doc_id:"my@lias", title: "hello dude"}'
{"_index":"my_index","_type":"doc","_id":"2","_version":1,"_shards":{"total":2,"successful":1,"failed":0},"created":true}

curl -XGET 'elasticsearch:9200/my_alias/doc/_search?pretty=true' -d'{"query": {"match": {"title": "hello"}}}'
{
    "took" : 1,
    "timed_out" : false,
    "_shards" : {
        "total" : 1,
        "successful" : 1,
        "failed" : 0
    },
    "hits" : {
        "total" : 1,
        "max_score" : 0.19178301,
        "hits" : [ {
            "_index" : "my_index",
            "_type" : "doc",
            "_id" : "1",
            "_score" : 0.19178301,
            "_routing" : "my_alias",
            "_source" : {
                "doc_id" : "my_alias",
                "title" : "hello world"
            }
        } ]
    }
}

curl -XGET 'elasticsearch:9200/my@lias/doc/_search?pretty=true' -d'{"query": {"match": {"title": "hello"}}}'
{
    "took" : 1,
    "timed_out" : false,
    "_shards" : {
        "total" : 1,
        "successful" : 1,
        "failed" : 0
    },
    "hits" : {
        "total" : 0,
        "max_score" : null,
        "hits" : [ ]
    }
}

让我感到困惑的是,没有Invalid Alias Name Exception,搜索结果还可以,但是空的。

我还尝试使用url-encoding %40elasticsearch:9200/my%40lias,但它是一样的。

如果我搜索整个索引,我会看到这两个文件。如果我使用@创建索引,索引搜索也可以。如果我卷曲_aliases以查看ES状态,我可以看到它:

"my_index" : {
  "aliases" : {
    "my@lias" : {
      "filter" : {
        "term" : {
          "doc_id" : "my@lias"
        }
      },
      "index_routing" : "my@lias",
      "search_routing" : "my@lias"
    },
    "my_alias" : {
      "filter" : {
        "term" : {
          "doc_id" : "my_alias"
        }
      },
      "index_routing" : "my_alias",
      "search_routing" : "my_alias"
    }
  }
},

你认为这种行为有什么理由吗?我想念一下吗?

2 个答案:

答案 0 :(得分:0)

您只需对@符号进行网址编码,如下所示:

curl -XGET 'elasticsearch:9200/my%40lias/doc/_search?pretty=true'
                                  ^
                                  |
                             change this

答案 1 :(得分:0)

我在本地复制了您的索引,我认为您遇到的问题是您没有正确索引文档。

例如,看一下这个请求:

curl -XPUT 'elasticsearch:9200/my_alias/doc/1' -d '{doc_id:"my_alias", title: "hello world"}'

应该是

curl -XPUT "http://localhost:9200/my_alias/doc/1" -d'
{"doc_id":"my_alias", "title": "hello world"}'

请注意 {doc_id:&#34; my_alias&#34; {&#34; doc_id&#34;:&#34; my_alias&#34; < / strong>因为您缺少doc_id的引号。这也适用于标题。

完全固定的例子:

PUT /my_index
PUT /my_index/_alias/my_alias
{"routing": "my_alias", "filter": {"term": {"doc_id": "my_alias"}}}

PUT /my_index/_alias/my@lias
{"routing": "my@lias", "filter": {"term": {"doc_id": "my@lias"}}}

PUT /my_alias/doc/1
{
  "doc_id":"my_alias", "title": "hello world"

}

PUT /my@lias/doc/2
{
  "doc_id": "my@lias",
  "title": "hello dude"
}


GET /my_alias/doc/_search?pretty=true
{
  "query": {
    "match": {
      "title": "hello"
    }
  }
}

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.19178301,
    "hits": [
      {
        "_index": "my_index",
        "_type": "doc",
        "_id": "1",
        "_score": 0.19178301,
        "_routing": "my_alias",
        "_source": {
          "doc_id": "my_alias",
          "title": "hello world"
        }
      }
    ]
  }
}