在为某些帖子建立索引和创建映射时,请使用最新版本的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”,或者省略了index,则索引,映射和帖子插入将起作用。

一旦我尝试在main_taxonomy和category路径字段上使用自定义分析器,如上面的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({})

但是要使用此索引,索引必须与映射一起存在,否则将引发错误“找不到索引”

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

最佳答案

所以最终的分析器是:

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

我确实确定,至少对于不能使用复杂嵌套或对象的路径分析器或模式分析器而言。展平的字段设置为“type”:“string”是使它起作用的唯一方法。

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

我选择使用模式分析器,因为它会在模式上中断而留下单独的术语,而路径以不同的方式分割路径,但不会创建单独的术语(我希望我说的没错。我将其建立在文档的基础上)。

希望这对别人有帮助!

史蒂夫

关于elasticsearch - 使用自定义分析器的Elasticsearch.js分析器错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42179874/

10-17 03:05