elasticsearch,elastic-stack,Java,elasticsearch,Elastic Stack" /> elasticsearch,elastic-stack,Java,elasticsearch,Elastic Stack" />

在Java REST客户端[6.5]API上的ES 6.5中使用映射创建索引

在Java REST客户端[6.5]API上的ES 6.5中使用映射创建索引,java,elasticsearch,elastic-stack,Java,elasticsearch,Elastic Stack,我是弹性搜索的新手,并试图通过跟随文章为应用程序集成自动完成功能 我遵循以下方法来做同样的事情 事件类 public class Event { private Long eventId; private Long catalogId; private Long orgId; private String orgName; private String catalogName; pri

我是弹性搜索的新手,并试图通过跟随文章为应用程序集成自动完成功能

我遵循以下方法来做同样的事情

事件类

       public class Event {

        private Long eventId;
        private Long catalogId;
        private Long orgId;
        private String orgName;
        private String catalogName;
        private String name;
        private String eventStatus;
.....
    }
objectmapper用于将事件对象转换为json字符串。下面是插入文档的代码

public String createEventDocument(Event document) throws Exception {
    IndexRequest indexRequest = new IndexRequest(INDEX, TYPE, document.idAsString())
            .source(convertEventDocumentToMap(document));
    //create mapping with a complete field
    IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
    return indexResponse.getResult().name();
}
转换代码

private Map<String, Object> convertEventDocumentToMap(Event evt) {
    return objectMapper.convertValue(evt, Map.class);
}
私有映射convertEventDocumentToMap(事件evt){
返回objectMapper.convertValue(evt,Map.class);
}
我想创建一个索引,并为name_suggest字段设置完成建议器。我怎样才能达到同样的效果


非常感谢您的帮助

您可以做以下事情:

public static void main( String[] args )
    {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost( "192.168.1.245", 9200, "http" ) ) );
        try
        {
            createIndex( client );
            updateIndexMapping( client );
        }
        catch ( Exception e )
        {
            e.printStackTrace();
        }
    }

    private static void createIndex( RestHighLevelClient client ) throws IOException
    {

        //1. create index
        Map<String, String> map = new HashMap<String, String>();
        map.put( "eventId", "eventId" );
        map.put( "catalogId", "catalogId" );
        map.put( "orgId", "orgId" );
        map.put( "orgName", "orgName" );
        map.put( "catalogName", "catalogName" );
        map.put( "name", "name" );
        map.put( "eventStatus", "eventStatus" );

        IndexRequest indexRequest = new IndexRequest( "event", "event", "123" )
                .source( map );

        IndexResponse indexResponse = client.index( indexRequest, RequestOptions.DEFAULT );
        indexResponse.getResult().name();
    }

    private static void updateIndexMapping( RestHighLevelClient client ) throws IOException
    {
        //2. update index mapping to set the filed 'name_suggest' into type 'completion'
        PutMappingRequest request = new PutMappingRequest( "event" );
        request.type( "event" );

        XContentBuilder builder = XContentFactory.jsonBuilder();
        builder.startObject();
        {
            builder.startObject( "properties" );
            {
                builder.startObject( "name_suggest" );
                {
                    builder.field( "type", "completion" );
                }
                builder.endObject();
            }
            builder.endObject();
        }
        builder.endObject();
        request.source( builder );
        request.timeout( TimeValue.timeValueMinutes( 1 ) );

        AcknowledgedResponse acknowledgedResponse = client.indices().putMapping( request, RequestOptions.DEFAULT );

        //check if the request is sucess
        boolean acknowledged = acknowledgedResponse.isAcknowledged();
    }
publicstaticvoidmain(字符串[]args)
{
RestHighLevelClient=新的RestHighLevelClient(
RestClient.builder(
新的HttpHost(“192.168.1.245”,9200,“http”);
尝试
{
createIndex(客户端);
更新索引映射(客户端);
}
捕获(例外e)
{
e、 printStackTrace();
}
}
私有静态void createIndex(RestHighLevelClient客户端)引发IOException
{
//1.创建索引
Map Map=newhashmap();
map.put(“eventId”、“eventId”);
地图放置(“目录ID”、“目录ID”);
地图放置(“orgId”、“orgId”);
地图放置(“orgName”、“orgName”);
地图放置(“目录名”、“目录名”);
地图。放置(“名称”、“名称”);
map.put(“事件状态”、“事件状态”);
IndexRequest IndexRequest=新的IndexRequest(“事件”、“事件”、“123”)
.来源(地图);
IndexResponse IndexResponse=client.index(indexRequest,RequestOptions.DEFAULT);
indexResponse.getResult().name();
}
私有静态void updateIndexMapping(RestHighLevelClient客户端)引发IOException
{
//2.更新索引映射,将字段“name\u suggest”设置为“completion”类型
PutMappingRequest请求=新的PutMappingRequest(“事件”);
请求类型(“事件”);
XContentBuilder=XContentFactory.jsonBuilder();
builder.startObject();
{
建造商。startObject(“物业”);
{
建造商:startObject(“名称”);
{
建造商字段(“类型”、“完成”);
}
builder.endObject();
}
builder.endObject();
}
builder.endObject();
请求。来源(建造商);
请求超时(TimeValue.timeValueMinutes(1));
AcknowledgedResponse AcknowledgedResponse=client.index().putMapping(请求,RequestOptions.DEFAULT);
//检查请求是否成功
布尔已确认=acknowledgedResponse.isAcknowledged();
}

您可以执行以下操作:

public static void main( String[] args )
    {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost( "192.168.1.245", 9200, "http" ) ) );
        try
        {
            createIndex( client );
            updateIndexMapping( client );
        }
        catch ( Exception e )
        {
            e.printStackTrace();
        }
    }

    private static void createIndex( RestHighLevelClient client ) throws IOException
    {

        //1. create index
        Map<String, String> map = new HashMap<String, String>();
        map.put( "eventId", "eventId" );
        map.put( "catalogId", "catalogId" );
        map.put( "orgId", "orgId" );
        map.put( "orgName", "orgName" );
        map.put( "catalogName", "catalogName" );
        map.put( "name", "name" );
        map.put( "eventStatus", "eventStatus" );

        IndexRequest indexRequest = new IndexRequest( "event", "event", "123" )
                .source( map );

        IndexResponse indexResponse = client.index( indexRequest, RequestOptions.DEFAULT );
        indexResponse.getResult().name();
    }

    private static void updateIndexMapping( RestHighLevelClient client ) throws IOException
    {
        //2. update index mapping to set the filed 'name_suggest' into type 'completion'
        PutMappingRequest request = new PutMappingRequest( "event" );
        request.type( "event" );

        XContentBuilder builder = XContentFactory.jsonBuilder();
        builder.startObject();
        {
            builder.startObject( "properties" );
            {
                builder.startObject( "name_suggest" );
                {
                    builder.field( "type", "completion" );
                }
                builder.endObject();
            }
            builder.endObject();
        }
        builder.endObject();
        request.source( builder );
        request.timeout( TimeValue.timeValueMinutes( 1 ) );

        AcknowledgedResponse acknowledgedResponse = client.indices().putMapping( request, RequestOptions.DEFAULT );

        //check if the request is sucess
        boolean acknowledged = acknowledgedResponse.isAcknowledged();
    }
publicstaticvoidmain(字符串[]args)
{
RestHighLevelClient=新的RestHighLevelClient(
RestClient.builder(
新的HttpHost(“192.168.1.245”,9200,“http”);
尝试
{
createIndex(客户端);
更新索引映射(客户端);
}
捕获(例外e)
{
e、 printStackTrace();
}
}
私有静态void createIndex(RestHighLevelClient客户端)引发IOException
{
//1.创建索引
Map Map=newhashmap();
map.put(“eventId”、“eventId”);
地图放置(“目录ID”、“目录ID”);
地图放置(“orgId”、“orgId”);
地图放置(“orgName”、“orgName”);
地图放置(“目录名”、“目录名”);
地图。放置(“名称”、“名称”);
map.put(“事件状态”、“事件状态”);
IndexRequest IndexRequest=新的IndexRequest(“事件”、“事件”、“123”)
.来源(地图);
IndexResponse IndexResponse=client.index(indexRequest,RequestOptions.DEFAULT);
indexResponse.getResult().name();
}
私有静态void updateIndexMapping(RestHighLevelClient客户端)引发IOException
{
//2.更新索引映射,将字段“name\u suggest”设置为“completion”类型
PutMappingRequest请求=新的PutMappingRequest(“事件”);
请求类型(“事件”);
XContentBuilder=XContentFactory.jsonBuilder();
builder.startObject();
{
建造商。startObject(“物业”);
{
建造商:startObject(“名称”);
{
建造商字段(“类型”、“完成”);
}
builder.endObject();
}
builder.endObject();
}
builder.endObject();
请求。来源(建造商);
请求超时(TimeValue.timeValueMinutes(1));
AcknowledgedResponse AcknowledgedResponse=client.index().putMapping(请求,RequestOptions.DEFAULT);
//检查请求是否成功
布尔已确认=acknowledgedResponse.isAcknowledged();
}

以下是同样的解决方案。首先使用映射器创建索引并插入数据

 public String createEventDocument(Event document) throws Exception {
    GetIndexRequest request = new GetIndexRequest();
    request.indices(INDEX);
    boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
    if(!exists){
        createIndexWithMapping();
    }
    IndexRequest indexRequest = new IndexRequest(INDEX, TYPE, document.idAsString())
            .source(convertEventDocumentToMap(document));
    //create mapping with a complete field
    IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
    return indexResponse.getResult().name();
}

private boolean createIndexWithMapping() throws IOException {
            CreateIndexRequest createIndexRequest = new CreateIndexRequest(INDEX);
    XContentBuilder builder = XContentFactory.jsonBuilder();
    builder.startObject();
    {
        builder.startObject( "properties" );
        {
            builder.startObject( "name_suggest" );
            {
                builder.field( "type", "completion" );
            }
            builder.endObject();
        }
        builder.endObject();
    }
    builder.endObject();
    createIndexRequest.mapping(TYPE,builder);
    createIndexRequest.timeout(TimeValue.timeValueMinutes(2));
    CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
    return createIndexResponse.isAcknowledged();

}

下面是同样的解决方案。首先使用映射器创建索引并插入数据

 public String createEventDocument(Event document) throws Exception {
    GetIndexRequest request = new GetIndexRequest();
    request.indices(INDEX);
    boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
    if(!exists){
        createIndexWithMapping();
    }
    IndexRequest indexRequest = new IndexRequest(INDEX, TYPE, document.idAsString())
            .source(convertEventDocumentToMap(document));
    //create mapping with a complete field
    IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
    return indexResponse.getResult().name();
}

private boolean createIndexWithMapping() throws IOException {
            CreateIndexRequest createIndexRequest = new CreateIndexRequest(INDEX);
    XContentBuilder builder = XContentFactory.jsonBuilder();
    builder.startObject();
    {
        builder.startObject( "properties" );
        {
            builder.startObject( "name_suggest" );
            {
                builder.field( "type", "completion" );
            }
            builder.endObject();
        }
        builder.endObject();
    }
    builder.endObject();
    createIndexRequest.mapping(TYPE,builder);
    createIndexRequest.timeout(TimeValue.timeValueMinutes(2));
    CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
    return createIndexResponse.isAcknowledged();

}

这意味着你在
事件中有一个归档的
名称\u建议
,对吗?这意味着你在
事件中有一个归档的
名称\u建议
,对吗?谢谢。我会检查并更新。updateIndexMapping是一次性活动,还是我们需要对所有插入都执行相同的操作?我昨天尝试过类似的操作XContentBuilder mappingBuilder=jsonBuilder().startObject()。startObject(“您的类型名称”)。startObject(“属性”)。startObject(“您的完成字段”)。字段(“类型”、“完成”).endObject().endObject().endObject().endObject().endObject();普特