2020-03-18 spring-data-elasticse

2020-03-18  本文已影响0人  FredWorks

关于使用 spring-data-elasticsearch 提供的注解来配置索引映射,来网上有很多文章,可能在老版本上可以运行,我没有在老版本上测试。但在 v7.6.1 上,这些方法都不能工作了。
目前的spring-boot-2.2.5.RELEASE + spring-data-elasticsearch 提供的 @Ducument、@Field、@FieldType,可以用来在文档对象上通过注解自动生成索引映射,但存在明显的严重BUG,只能适用于特地场景。下面会通过例子逐一说明。如果有需求,还是手动创建索引来的比较实在。
下面就详细介绍。

1. 通过 elasticsearchTemplate.putMapping配置。

网上提到的一种方法:、
给 elasticsearchTemplate 对象使用如下方法注入具体索引的映射:

    @BeforeEach
    void setUp() throws Exception {
        assertNotNull(this.esRestTemplate);
        this.esRestTemplate.putMapping(Question.class);

在文档对象上使用注解:

@Mapping(mappingPath = "Question.json")
@Document(indexName = "question")
public class Question implements Serializable {

在 /src/main/resoursces/Question.json中配置索引的映射:

{
  "mapping": {
      "properties": {
        "category": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_smart",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "formulas": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_smart",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "id": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "question": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_max_word",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "questionNo": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_max_word",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    }
}

事实证明不行。会报如下错误:

MapperParsingException[Root mapping definition has unsupported parameters:  [mapping : {properties={category={type=text, analyzer=ik_max_word, search_analyzer=ik_max_word, fields={keyword={type=keyword, ignore_above=256}}}, formulas={type=text, analyzer=ik_max_word, search_analyzer=ik_max_word, fields={keyword={type=keyword, ignore_above=256}}}, id={type=text, fields={keyword={type=keyword, ignore_above=256}}}, question={type=text, analyzer=ik_max_word, search_analyzer=ik_max_word, fields={keyword={type=keyword, ignore_above=256}}}, questionNo={type=text, analyzer=ik_max_word, search_analyzer=ik_max_word, fields={keyword={type=keyword, ignore_above=256}}}}}]]
    at org.elasticsearch.index.mapper.DocumentMapperParser.checkNoRemainingFields(DocumentMapperParser.java:148)
    at org.elasticsearch.index.mapper.DocumentMapperParser.parse(DocumentMapperParser.java:136)
    at org.elasticsearch.index.mapper.DocumentMapperParser.parse(DocumentMapperParser.java:88)
    at org.elasticsearch.index.mapper.MapperService.parse(MapperService.java:674)
    at org.elasticsearch.cluster.metadata.MetaDataMappingService$PutMappingExecutor.applyRequest(MetaDataMappingService.java:279)

存在的问题我还没有来得及去查,后面有时间再说。

2 通过 @Document 和 @Field、@MultiField、@InnerField 配置

通过反复实验,证实在最新的 spring-boot-2.2.5.RELEASE + elasticsearch@7.6.1的组合,特定情况下的注解配置存在严重BUG,但基本可用。
下面分别介绍可以成功的场景,和会导致失败的场景。

2.1. 不使用 FieldType.Auto,只使用 FieldType.Keyword、FieldType.Text) 并正确配置

这种情况下,可以正确生成索引映射。
文档注解如下:

@Document(indexName = "question")
public class Question implements Serializable {

    @Id
    @Field(type= FieldType.Keyword)
    private String id;
    
    /**
     * 题号
     */
    @Field(type=FieldType.Text)
    private String questionNo;
    
    /**
     * 题目类型
     */
    @MultiField(
            mainField = @Field(type=FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart"), 
            otherFields = @InnerField(suffix = "keyword", type=FieldType.Keyword))
    private String category;
    
    /**
     * 题目
     */
    @Field(type=FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
    private String question;
    
    /**
     * 从题目中提出取来的公式列表。
     */
    @Field(type = FieldType.Keyword)
    private List<String> formulas;

可以看到自动生成的映射如下:

{
  "mapping": {
    "question": {
      "properties": {
        "category": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword"
            }
          },
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_smart"
        },
        "formulas": {
          "type": "keyword"
        },
        "id": {
          "type": "keyword"
        },
        "question": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_smart"
        },
        "questionNo": {
          "type": "text"
        }
      }
    }
  }
}

可以看到,自动生成的映射有如下特点:

        "formulas": {
         "type": "keyword"
       },
       "id": {
         "type": "keyword"
       },

比如

   @Field(type=FieldType.Text)
   private String questionNo;

生成的映射是:

       "questionNo": {
         "type": "text"
       }

比如

   @Field(type=FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
   private String question;

生成的映射是:

       "question": {
         "type": "text",
         "analyzer": "ik_max_word",
         "search_analyzer": "ik_smart"
       },

所谓多字段,是说同一个字段同时建立多种索引,比如 text 和 keyword。注意,不能用 FieldType.Auto,会导致映射出错。具体参看 2.2.2
应当用 @MultiField 和 @InnerField 结合使用:

   @MultiField(
           mainField = @Field(type=FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart"), 
           otherFields = @InnerField(suffix = "keyword", type=FieldType.Keyword))
   private String category;

生成的映射是:

       "category": {
         "type": "text",
         "fields": {
           "keyword": {
             "type": "keyword"
           }
         },
         "analyzer": "ik_max_word",
         "search_analyzer": "ik_smart"
       },

2.2. 只要使用了 @Field(type=FieldType.Auto),自动索引映射就会出错。

在spring-data-elasticsearch 提供的注解 FieldType.Auto 本来代表了 对应字段应当同时支持类型 FieldType 和 FieldType.Keyword,但只要有一个属性使用了注解 FieldType.Auto ,会导致配置的分析器丢失,且其他属性无论配置什么类型,都会变成 FieldType.Auto

在文档上直接配置文档类型:

@Document(indexName = "question")
public class Question implements Serializable {

    @Id
    @Field(type= FieldType.Keyword)
    private String id;
    
    /**
     * 题号
     */
    @Field(type=FieldType.Text)
    private String questionNo;
    
    /**
     * 题目类型
     */
    @Field(type=FieldType.Auto, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
    private String category;
    
    /**
     * 题目
     */
    @Field(type=FieldType.Auto, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
    private String question;
    
    /**
     * 从题目中提出取来的公式列表。
     */
    @Field(type = FieldType.Auto)
    private List<String> formulas;

可以看到自动生成的索引映射如下:

{
  "mapping": {
    "question": {
      "properties": {
        "category": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "formulas": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "id": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "question": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "questionNo": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    }
  }
}

可以看到,这种混合使用FieldType.Auto/Keyword/Text的配置存在两个问题:

这显然是 spring-data-elasticsearch 的BUG。关于这个问题,我在网上搜索到了好多类似的问题反馈。

2.3. 给属性配置 @Field(type=FieldType.Keyword) 指定了分析器,也会导致自动索引映射出错。

显然,FieldType.Keyword 是不能有分析器的,因为它的含义本来就是不使用分析器而是作为一个整体来索引。如果给一个属性指定为类型 FieldType.Kyeword 还同时指定了分析器,那么自动索引映射就会出错,其效果同使用了 FieldType.Auto 一样。
比如,我们给文档对象使用如下配置:

@Document(indexName = "question")
public class Question implements Serializable {

    @Id
    @Field(type= FieldType.Keyword)
    private String id;
    
    /**
     * 题号
     */
    @Field(type=FieldType.Text)
    private String questionNo;
    
    /**
     * 题目类型
     */
    @Field(type=FieldType.Auto, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
    private String category;
    
    /**
     * 题目
     */
    @Field(type=FieldType.Auto, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
    private String question;
    
    /**
     * 从题目中提出取来的公式列表。
     */
    @Field(type = FieldType.Keyword, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
    private List<String> formulas;

其自动生成的索引映射是:

{
  "mapping": {
    "question": {
      "properties": {
        "category": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "formulas": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "id": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "question": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "questionNo": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    }
  }
}

显然省生成的映射出错了。其效果和有属性配置了 FieldType.Auto 一样:

上一篇下一篇

猜你喜欢

热点阅读