使用自定义分析器的Elasticsearch.js分析器错误

时间:2017-02-11 18:50:24

标签: elasticsearch lucene analyzer elasticsearch.js

使用最新版本的elasticsearch.js并尝试在索引和创建某些帖子的映射时创建自定义路径分析器。

目标是在路径的每个细分中创建关键字。然而,首先只是尝试让分析仪工作。

这是elasticsearch.js create_mapped_index.js,您可以在文件顶部附近看到自定义分析器:

var client = require('./connection.js');

client.indices.create({
index: "wcm-posts",
body: {
    "settings": {
        "analysis": {
            "analyzer": {
                "wcm_path_analyzer": {
                    "tokenizer": "wcm_path_tokenizer",
                    "type": "custom"
                }
            },
            "tokenizer": {
                "wcm_path_tokenizer": {
                    "type": "pattern",
                    "pattern": "/"
                }
            }
        }
    },
    "mappings": {
        "post": {
            "properties": {
                "id": { "type": "string", "index": "not_analyzed" },
                "titles": {
                    "type": "object",
                    "properties": {
                        "main": { "type": "string" },
                        "subtitle": { "type": "string" },
                        "alternate": { "type": "string"  },
                        "concise": { "type": "string" },
                        "seo": { "type": "string" }
                    }
                },
                "tags": {
                    "properties": {
                        "id": { "type": "string", "index": "not_analyzed" },
                        "name": { "type": "string", "index": "not_analyzed" },
                        "slug": { "type": "string" }
                    },
                },
                "main_taxonomies": {
                    "properties": {
                        "id": { "type": "string", "index": "not_analyzed" },
                        "name": { "type": "string", "index": "not_analyzed" },
                        "slug": { "type": "string", "index": "not_analyzed" },
                        "path": { "type": "string", "index": "wcm_path_analyzer" }
                    },
                },
                "categories": {
                    "properties": {
                        "id": { "type": "string", "index": "not_analyzed" },
                        "name": { "type": "string", "index": "not_analyzed" },
                        "slug": { "type": "string", "index": "not_analyzed" },
                        "path": { "type": "string", "index": "wcm_path_analyzer" }
                    },
                },
                "content_elements": {
                    "dynamic": "true",
                    "type": "nested",
                    "properties": {
                        "content": { "type": "string" }
                    }
                }
            }
        }
    }
  }
}, function (err, resp, respcode) {
    console.log(err, resp, respcode);
});

如果对wcm_path_analyzer的调用设置为" non_analyzed"或索引省略索引,映射和插入帖子的工作。

当我尝试在main_taxonomy和类别路径字段上使用自定义分析器时,如上面的json所示,我收到此错误:

  response: '{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"wrong value for index [wcm_path_analyzer] for field [path]"}],"type":"mapper_parsing_exception","reason":"Failed to parse mapping [post]: wrong value for index [wcm_path_analyzer] for field [path]","caused_by":{"type":"mapper_parsing_exception","reason":"wrong value for index [wcm_path_analyzer] for field [path]"}},"status":400}',
toString: [Function],
toJSON: [Function] } { error:
{ root_cause: [ [Object] ],
 type: 'mapper_parsing_exception',
 reason: 'Failed to parse mapping [post]: wrong value for index [wcm_path_analyzer] for field [path]',
 caused_by:
  { type: 'mapper_parsing_exception',
    reason: 'wrong value for index [wcm_path_analyzer] for field [path]' } },
  status: 400 } 400

以下是路径字段中需要自定义分析器的两个对象的示例。我在没有使用自定义分析器时将15个帖子插入elasticsearch索引后拉了这个例子:

 "main_taxonomies": [
        {
          "id": "123",
          "type": "category",
          "name": "News",
          "slug": "news",
          "path": "/News/"
        }
      ],
      "categories": [
        {
          "id": "157",
          "name": "Local News",
          "slug": "local-news",
          "path": "/News/Local News/",
          "main": true
        },

到目前为止,我曾搜索过类似的问题,大多数人都说人们错过了将分析仪放在设置中而不是将参数添加到身体中。我相信这是正确的。

我还查看了elasticsearch.js文档并尝试创建:

client.indices.putSettings({}) 

但是要使用它,索引需要与映射一起存在,否则会引发错误'没有找到索引'

不确定从哪里开始?您的建议表示赞赏。

2 个答案:

答案 0 :(得分:0)

所以我让它工作了......我认为json对象太复杂了,或者是将分析器添加到字段映射中的变化。

首先我压扁了:

要:

"main_taxonomies_path": "/News/",
"categories_paths": [ "/News/Local/", "/Business/Local/" ],
"search_tags": [ "montreal-3","laval-4" ],

然后我将分析仪更新为:

"settings": {
        "analysis": {
            "analyzer": {
                "wcm_path_analyzer": {
                    "tokenizer": "wcm_path_tokenizer",
                    "type": "custom"
                }
            },
            "tokenizer": {
                "wcm_path_tokenizer": {
                    "type": "pattern",
                    "pattern": "/",
                    "replacement": ","
                }
            }
        }
    },

请注意,分析器的“类型”设置为自定义。

然后将这些平面字段映射到:

"main_taxonomies_path": { "type": "string", "analyzer": "wcm_path_analyzer" },
"categories_paths": { "type": "string", "analyzer": "wcm_path_analyzer" },
"search_tags": { "type": "string" },

在搜索这些字段的产量时:

 "main_taxonomies_path": "/News/",
 "categories_paths": [ "/News/Local News/",  "/Business/Local Business/" ],
 "search_tags": [ "montreal-2", "laval-3" ],

因此,自定义分析器完成了在这种情况下设置的操作。

我不确定我是否可以将类型对象应用于main_taxonomies_path和categories_paths,因此我将使用它来查看。

我将改进模式搜索,以不同的方式格式化结果,但很高兴让它工作。

为了完整性,我将在完成此操作后放置我的最终自定义模式分析器,映射和结果。

此致 史蒂夫

答案 1 :(得分:0)

所以最终的分析仪是:

var client = require('./connection.js');

client.indices.create({
  index: "wcm-posts",
  body: {
    "settings": {
        "analysis": {
            "analyzer": {
                "wcm_path_analyzer": {
                    "type" : "pattern",
                    "lowercase": true,
                    "pattern": "/" 
                }
            }
        }
    },
    "mappings": {
        "post": {
            "properties": {
                "id": { "type": "string", "index": "not_analyzed" },
                "client_id": { "type": "string", "index": "not_analyzed" },
                "license_id": { "type": "string", "index": "not_analyzed" },
                "origin_id": { "type": "string" },
 ...
 ...
                "origin_slug": { "type": "string" },
                "main_taxonomies_path": { "type": "string", "analyzer": "wcm_path_analyzer", "search_analyzer": "standard" },
                "categories_paths": { "type": "string", "analyzer": "wcm_path_analyzer", "search_analyzer": "standard" },
                "search_tags": { "type": "string" },
                // See the custom analyzer set here --------------------------^

我确定至少对于路径或模式分析器来说,不能使用复杂的嵌套或对象。展平的字段设置为"键入":" string"是实现这一目标的唯一途径。

我最终不需要自定义标记器,因为模式分析器是全功能的并且已经包含一个标记器。

我选择使用模式分析器,因为它打破了模式,留下了单独的术语,而路径以不同的方式划分路径,但不创建单独的术语(我希望我说的是正确的。我基于它在文件上。)

希望这有助于其他人!

史蒂夫