【问题标题】:Elasticsearch.js analyzer error using custom analyzer使用自定义分析器的 Elasticsearch.js 分析器错误
【发布时间】:2017-06-30 00:16:05
【问题描述】:

使用最新版本的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({}) 

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

不确定从这里去哪里?感谢您的建议。

【问题讨论】:

    标签: elasticsearch lucene analyzer elasticsearch.js


    【解决方案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 --------------------------^
    

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

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

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

    希望这对其他人有帮助!

    史蒂夫

    【讨论】:

      【解决方案2】:

      所以我让它工作了......我认为 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,所以我会玩弄这个看看。

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

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

      问候, 史蒂夫

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-24
        • 1970-01-01
        相关资源
        最近更新 更多