0

使用最新版本的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 和 categories 路径字段上使用自定义分析器,如上面的 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 个帖子插入弹性搜索索引后提取了这个示例:

 "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({}) 

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

不知道从这里去哪里?感谢您的建议。

4

2 回答 2

1

所以最终的分析器是:

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 --------------------------^

我确实确定至少对于复杂嵌套或对象不能使用的路径或模式分析器。设置为“类型”的展平字段:“字符串”是使其工作的唯一方法。

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

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

希望这对其他人有帮助!

史蒂夫

于 2017-02-12T18:38:33.203 回答
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,所以我会玩弄这个看看。

我将改进模式搜索以以不同的方式格式化结果,但很高兴能做到这一点。

为了完整起见,一旦我完成了这个,我将放置我最终的自定义模式分析器、映射和结果。

问候,史蒂夫

于 2017-02-11T23:30:49.717 回答