All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.thematchbox.river.data.DocumentContentDelegator Maven / Gradle / Ivy

Go to download

This project contains an abstract implementation of an ElasticSearch River and is used as a basis for custom river implementations.

There is a newer version: 1.1.3
Show newest version
package com.thematchbox.river.data;

/* Copyright 2015 theMatchBox

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

import com.thematchbox.river.sessions.files.FileDocument;
import org.elasticsearch.common.xcontent.XContentBuilder;

import java.io.IOException;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

@SuppressWarnings("UnusedDeclaration")
public class DocumentContentDelegator implements ContentDelegator {

    public static final String CONTENT_FIELD = "content";
    public static final String FILE_NAME_FIELD = "fileName";
    public static final String DOCUMENT_TYPE_FIELD = "documentType";
    private boolean useMatchBoxAnalyzer;
    private String analyzer;

    public DocumentContentDelegator() {
        this(false, "matchbox_language_analyzer_DUT");
    }

    public DocumentContentDelegator(boolean useMatchBoxAnalyzer, String analyzer) {
        this.useMatchBoxAnalyzer = useMatchBoxAnalyzer;
        this.analyzer = analyzer;
    }

    @Override
    public XContentBuilder getXContentBuilder(FileDocument fileDocument) throws IOException {
        XContentBuilder contentBuilder = jsonBuilder();

        contentBuilder.startObject();
        contentBuilder.field("id", fileDocument.getId());
        contentBuilder.field(FILE_NAME_FIELD, fileDocument.getFileName());
        contentBuilder.field("path", fileDocument.getPath());
        if (fileDocument.hasContent()) {
            contentBuilder.field(CONTENT_FIELD, fileDocument.getContent());
        }

        contentBuilder.field(DOCUMENT_TYPE_FIELD, fileDocument.getDocumentType().name());

        contentBuilder.endObject();

        return contentBuilder;
    }

    @Override
    public XContentBuilder getMapping(String typeName) throws IOException {
        XContentBuilder mappingBuilder = jsonBuilder();
        mappingBuilder.startObject();
        mappingBuilder.field(typeName);
        mappingBuilder.startObject();
        mappingBuilder.field("properties");
        mappingBuilder.startObject();

        createFacetField(mappingBuilder, DOCUMENT_TYPE_FIELD);

        if (useMatchBoxAnalyzer) {
            createMatchBoxedField(mappingBuilder, FILE_NAME_FIELD);
            createMatchBoxedField(mappingBuilder, CONTENT_FIELD);
        }

        mappingBuilder.endObject();
        mappingBuilder.endObject();

        mappingBuilder.endObject();

        return mappingBuilder;
    }

    public void createMatchBoxedField(XContentBuilder mappingBuilder, String fieldName) throws IOException {
        mappingBuilder.field(fieldName);
        mappingBuilder.startObject();
        mappingBuilder.field("type", "string");
        mappingBuilder.field("fields");
        mappingBuilder.startObject();
        mappingBuilder.field("matchboxed").startObject().field("type", "string").field("index", "analyzed").field("analyzer", analyzer).endObject();
        mappingBuilder.endObject();
        mappingBuilder.endObject();

    }

    public void createFacetField(XContentBuilder mappingBuilder, String fieldName) throws IOException {
        mappingBuilder.field(fieldName);
        {
            mappingBuilder.startObject();
            mappingBuilder.field("type", "string");
            mappingBuilder.field("index", "not_analyzed");
            mappingBuilder.endObject();
        }

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy