2020-03-18 spring-data-elasticsearch 如何使用注解配置自動索引映射

關(guān)于使用 spring-data-elasticsearch 提供的注解來配置索引映射,來網(wǎng)上有很多文章,可能在老版本上可以運行,我沒有在老版本上測試。但在 v7.6.1 上,這些方法都不能工作了。
目前的spring-boot-2.2.5.RELEASE + spring-data-elasticsearch 提供的 @Ducument、@Field、@FieldType,可以用來在文檔對象上通過注解自動生成索引映射,但存在明顯的嚴(yán)重BUG,只能適用于特地場景。下面會通過例子逐一說明。如果有需求,還是手動創(chuàng)建索引來的比較實在。
下面就詳細(xì)介紹。

1. 通過 elasticsearchTemplate.putMapping配置。

網(wǎng)上提到的一種方法:、
給 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 配置

通過反復(fù)實驗,證實在最新的 spring-boot-2.2.5.RELEASE + elasticsearch@7.6.1的組合,特定情況下的注解配置存在嚴(yán)重BUG,但基本可用。
下面分別介紹可以成功的場景,和會導(dǎo)致失敗的場景。

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"
        }
      }
    }
  }
}

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

  • 映射配置為 FieldType.Keyword 的,映射為 keyword
        "formulas": {
         "type": "keyword"
       },
       "id": {
         "type": "keyword"
       },
  • 映射配置為 FieldType.Text 且沒有指定分析器的,將映射為使用默認(rèn)分析器

比如

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

生成的映射是:

       "questionNo": {
         "type": "text"
       }
  • 映射配置為 FieldType.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"
       },
  • 映射配置為@MultiField 的,會映射為多字段,并使用對應(yīng)的分析器。、

所謂多字段,是說同一個字段同時建立多種索引,比如 text 和 keyword。注意,不能用 FieldType.Auto,會導(dǎo)致映射出錯。具體參看 2.2.2
應(yīng)當(dāng)用 @MultiField 和 @InnerField 結(jié)合使用:

   @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"
       },
  • 如果映射為 FeildType.Keyword 還指定分析器,自動生成的映射會出錯,效果和映射為 FeildType.Auto 一樣。具體參看 2.2

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

在spring-data-elasticsearch 提供的注解 FieldType.Auto 本來代表了 對應(yīng)字段應(yīng)當(dāng)同時支持類型 FieldType 和 FieldType.Keyword,但只要有一個屬性使用了注解 FieldType.Auto ,會導(dǎo)致配置的分析器丟失,且其他屬性無論配置什么類型,都會變成 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的配置存在兩個問題:

  • 只要有一個屬性使用了 FieldType.Auto,不管其他屬性配置的是 FieldType.Auto,還是 FieldType.Keyword,還是 FieldType.Text,最后都是同時映射了 text 和 keyword,也就是 FieldType.Auto 的效果。
  • 不管你有沒有指定,或者指定的 analyzer 和 searchAnalyzer 是什么,最后都丟失了分析器映射。

這顯然是 spring-data-elasticsearch 的BUG。關(guān)于這個問題,我在網(wǎng)上搜索到了好多類似的問題反饋。

2.3. 給屬性配置 @Field(type=FieldType.Keyword) 指定了分析器,也會導(dǎo)致自動索引映射出錯。

顯然,F(xiàn)ieldType.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 一樣:

  • 不管你配置了什么類型,都變成 FieldType.Auto 了。
  • 指定的解析器都丟失了。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容