为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

呼唤远方 2022-05-20 18:37:17
我是弹性搜索的新手,并尝试按照文章https://www.elastic.co/blog/you-complete-me为应用程序集成自动完成功能。我已经按照下面的方法做同样的事情。事件类       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);}我想创建一个索引,并为 name_suggest 字段设置完成建议。我怎样才能达到同样的效果?任何帮助表示赞赏
查看完整描述

2 回答

?
呼如林

TA贡献1798条经验 获得超3个赞

这是执行相同操作的解决方案。首先使用映射器创建索引并插入数据


 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();


}


查看完整回答
反对 回复 2022-05-20
?
噜噜哒

TA贡献1784条经验 获得超7个赞

你可以做一些事情,比如,


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();

    }


查看完整回答
反对 回复 2022-05-20
  • 2 回答
  • 0 关注
  • 244 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号