亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 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 關注
  • 176 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號