Skip to content

Instantly share code, notes, and snippets.

@shyjuzz
Last active January 29, 2019 07:13
Show Gist options
  • Save shyjuzz/4a1820a74cbfd214f7a1c7a3bf670828 to your computer and use it in GitHub Desktop.
Save shyjuzz/4a1820a74cbfd214f7a1c7a3bf670828 to your computer and use it in GitHub Desktop.
while creating
{
"timestamp": "2019-01-29T07:00:29.963+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Elasticsearch exception [type=illegal_argument_exception, reason=mapper [name_suggest] of different type, current_type [text], merged_type [completion]]",
"path": "/event/create"
}
but the documents is created as shown below
{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "events",
"_type" : "event",
"_id" : "14774",
"_score" : 1.0,
"_source" : {
"eventId" : 233,
"catalogId" : 133,
"orgId" : 1313,
"orgName" : "DUMMY",
"catalogName" : "AAA",
"name" : "DUMMY NAME",
"eventStatus" : "PUBLISHED",
"eventStartTs" : "01-23-2019 03:00",
"eventEndTs" : "01-23-2019 03:30",
"regStartTs" : "01-22-2019 02:30",
"regEndTs" : "01-23-2019 03:30",
"isFree" : true,
"dateType" : "MULTI_DAY",
"tagText" : null,
"eventPrice" : 0.0,
"currency" : "$",
"orgBanner" : "/urler-fms.jpg",
"orgLogo" : "url//c48C_2940112016_054023_blob",
"name_suggest" : "DUMMY name"
}
}
]
}
}
SEARCH ERROR
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Jan 29 12:30:53 IST 2019
There was an unexpected error (type=Internal Server Error, status=500).
Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]
@Service
public class EventService {
String INDEX = "events";
String TYPE = "event";
private RestHighLevelClient client;
private ObjectMapper objectMapper;
@Autowired
public EventService(RestHighLevelClient client, ObjectMapper objectMapper) {
this.client = client;
this.objectMapper = objectMapper;
}
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);
updateIndexMapping();
return indexResponse.getResult().name();
}
private void updateIndexMapping() throws IOException
{
// CreateIndexRequest createIndexRequest = new CreateIndexRequest(INDEX);
PutMappingRequest request = new PutMappingRequest( INDEX );
request.type(TYPE);
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("events",builder);
// createIndexRequest.timeout(TimeValue.timeValueMinutes(2));
// CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
request.source( builder );
request.timeout( TimeValue.timeValueMinutes( 1 ) );
AcknowledgedResponse acknowledgedResponse = client.indices().putMapping( request, RequestOptions.DEFAULT );
System.out.println("Mapping Created :"+acknowledgedResponse.isAcknowledged());
}
public String suggestSearch() throws IOException {
SearchRequest searchRequest = new SearchRequest( INDEX );
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
CompletionSuggestionBuilder completionSuggestionBuilder = SuggestBuilders
.completionSuggestion( "name_suggest" )
.prefix( "m" ).skipDuplicates(true);
SuggestBuilder suggestBuilder = new SuggestBuilder();
suggestBuilder.addSuggestion( "name_suggest", completionSuggestionBuilder );
searchSourceBuilder.suggest( suggestBuilder );
searchRequest.source( searchSourceBuilder );
SearchResponse searchResponse = null;
try
{
searchResponse = client.search( searchRequest );
}
catch ( IOException e )
{
e.printStackTrace();
}
Suggest suggest = searchResponse.getSuggest();
CompletionSuggestion entries = suggest.getSuggestion( "name_suggest" );
for ( CompletionSuggestion.Entry entry : entries )
{
for ( CompletionSuggestion.Entry.Option option : entry.getOptions() )
{
String suggestText = option.getText().string();
System.out.println( suggestText );
}
}
return "SUCCESS";
}
}
{
"eventId": 23232,
"catalogId": 23,
"orgId": 233,
"orgName": "DUMMY",
"catalogName": "AAA",
"name": "Dummy Name",
"eventStatus": "PUBLISHED",
"eventStartTs": "01-23-2019 03:00",
"eventEndTs": "01-23-2019 03:30",
"regStartTs": "01-22-2019 02:30",
"regEndTs": "01-23-2019 03:30",
"isFree": true,
"dateType": "MULTI_DAY",
"eventPrice": 0.00,
"currency": "$",
"orgBanner": "url/.jpg",
"orgLogo": "url/c48C_2940112016_054023_blob",
"name_suggest" : "Dummy name"
}
@shyjuzz
Copy link
Author

shyjuzz commented Jan 29, 2019

Dummy values are used..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment