# syntax=docker/dockerfile:1.7

ARG ELASTIC_VERSION=8.7.0
ARG HANLP_REPO=https://gitee.com/xuxingchao/elasticsearch-analysis-hanlp.git
ARG HANLP_REF=master

FROM maven:3.9.9-eclipse-temurin-17 AS hanlp-builder

ARG ELASTIC_VERSION
ARG HANLP_REPO
ARG HANLP_REF

RUN apt-get update \
    && apt-get install -y --no-install-recommends git unzip \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /build

RUN git clone --depth 1 --branch "${HANLP_REF}" "${HANLP_REPO}" hanlp

WORKDIR /build/hanlp

RUN grep -q "<version>${ELASTIC_VERSION}</version>" pom.xml \
    && mvn -B -DskipTests package \
    && mkdir -p /plugin \
    && unzip -q target/releases/elasticsearch-analysis-hanlp-*.zip -d /plugin \
    && cp -a src/main/resources/data /plugin/analysis-hanlp/data \
    && test -f /plugin/analysis-hanlp/plugin-descriptor.properties

FROM docker.elastic.co/elasticsearch/elasticsearch:${ELASTIC_VERSION}

ARG ELASTIC_VERSION

LABEL org.opencontainers.image.title="zqyy/elasticsearch-hanlp" \
      org.opencontainers.image.description="Elasticsearch ${ELASTIC_VERSION} with HanLP analysis plugin" \
      org.opencontainers.image.source="https://gitee.com/xuxingchao/elasticsearch-analysis-hanlp"

RUN mkdir -p /usr/share/elasticsearch/config/analysis-hanlp

COPY --from=hanlp-builder --chown=elasticsearch:root /plugin/analysis-hanlp /usr/share/elasticsearch/plugins/analysis-hanlp
COPY --chown=elasticsearch:root config/hanlp.properties /usr/share/elasticsearch/config/analysis-hanlp/hanlp.properties
COPY --chown=elasticsearch:root config/hanlp-remote.xml /usr/share/elasticsearch/config/analysis-hanlp/hanlp-remote.xml
COPY --chown=elasticsearch:root config/hanlp.properties /usr/share/elasticsearch/plugins/analysis-hanlp/config/hanlp.properties
COPY --chown=elasticsearch:root config/hanlp-remote.xml /usr/share/elasticsearch/plugins/analysis-hanlp/config/hanlp-remote.xml
COPY --chown=elasticsearch:root dictionaries/custom/ /usr/share/elasticsearch/plugins/analysis-hanlp/data/dictionary/custom/

RUN /usr/share/elasticsearch/bin/elasticsearch-plugin list | grep -q '^analysis-hanlp$'
