Mappings field empty in elasticsearch - java

I am trying to create elasticsearch index using Java API ,
if we see below index mappings filed is coming as empty .
# curl -XGET localhost:9200/initest2
{"initest2":{"aliases":{},**"mappings":{}**,"settings":{"index":{"keyspace":"keyspace_test","number_of_shards":"5","provided_name":"initest2","max_result_window":"10000000","creation_date":"1671025346350","analysis":{"normalizer":{"testnormalizer":{"filter":["asciifolding"],"type":"custom","char_filter":[]}}},"number_of_replicas":"0","uuid":"BK9oTTKETZeWqiuR_tGd8w","version":{"created":"6080499"}}}}}
JAVA code :
String indexQuery = "";
StringBuilder indexString = new StringBuilder("");
String test = "{\"settings\": {\"max_result_window\":10000000,\"keyspace\":\""+keyspaceName+'\u0022' +",\"analysis\": "
+ "{\"normalizer\":{\"testnormalizer\": {\"type\": \"custom\",\"char_filter\": [],\"filter\": [\"asciifolding\"]}}}},"
+ "\"mappings\":{\""+tableName+"\":{\"discover\" : \".*\",\"properties\":{";
indexString.append(test);
for (String i : indexColumns)
{
indexString.append('\u0022'+i+'\u0022' +":" +"{ \"type\": \"keyword\",\"normalizer\": \"testnormalizer\"}" + ",");
}
indexString.delete(indexString.length()-1, indexString.length()).append("}}}}");
indexQuery = indexString.toString();
logger.info("index query is " + indexQuery);
CreateIndexRequest request = new CreateIndexRequest(tableName);
request.source(indexQuery, XContentType.JSON);
CreateIndexResponse createIndexResponse= restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
while printing the query before calling restHighLevel client :
index query is
{"settings": {"max_result_window":10000000,"keyspace":"ndl","analysis": {"normalizer":{"testnormalizer": {"type": "custom","char_filter": [],"filter": ["asciifolding"]}}}},"mappings":{"initest2":{"discover" : ".*","properties":{"name":{ "type": "keyword","normalizer": "testnormalizer"}}}}}
Please assist if any method to be updated in java code / any properties to be updated in elassandra
Current versrion of elasticsearch : 6.8.4
Note : tried using request.settings() & request.mappings() but it didnt help

Related

How to convert csv to json with arrays

I have a csv file with an initial data for my app.
{
"id": 1,
"topic": "Архитектура",
"question": "Как называется буддийское архитектурное культовое сооружение?",
"rightanswer": "Ступа",
"wronganswer1": "Баба",
"wronganswer2": "Яга",
"wronganswer3": "Метла",
"passed": false,
"right": false
},
I need to parse it to json with the array of "answers", what options i have ?
Result should be like that :
{
"id": 1,
"topic": "Архитектура",
"question": "Как называется буддийское архитектурное культовое сооружение?",
"answers":[
"Ступа",
"Баба",
"Яга",
"Метла" ],
"passed": false,
"right": false
}
You are almost in the right direction but you have to use JSONArray for answers rather then adding them directly into the object with the name.
you can have an if where you will check if key contains the answer string then you can add into the JSONArray else add that key and value into JSONObject and add this JSONArray with the key answers into the main object once you done by adding all field.
This logic will keep your logic flexible.
it will help you to achieve your desire JSON.
EDIT: I would suggest you change your excel structure if you can. you should have all possible options in a single column (such as opt1,opt2,opt3,opt4) and correct answers in another column to gain simplicity and flexibility in your excel designa nd code.
I'm using gson
String str = "{\r\n" +
" \"id\": 1,\r\n" +
" \"topic\": \"Архитектура\",\r\n" +
" \"question\": \"Как называется буддийское архитектурное культовое сооружение?\",\r\n" +
" \"rightanswer\": \"Ступа\",\r\n" +
" \"wronganswer1\": \"Баба\",\r\n" +
" \"wronganswer2\": \"Яга\",\r\n" +
" \"wronganswer3\": \"Метла\",\r\n" +
" \"passed\": false,\r\n" +
" \"right\": false\r\n" +
" }"; //<== your json input
JsonParser parser = new JsonParser();
JsonObject input = parser.parse(str).getAsJsonObject(); //parser you Json to object
JsonObject output = new JsonObject();//my new jsonOutput
output.add("id", input.get("id"));
//other field .....
//the trick start here
JsonArray answer = new JsonArray();
answer.add(input.get("rightanswer"));
answer.add(input.get("wronganswer1"));
answer.add(input.get("wronganswer2"));
answer.add(input.get("wronganswer3"));
output.add("answers", answer);
System.out.println(output.toString());
result
{"id":1,"answers":["Ступа","Баба","Яга","Метла"]} // to lazy to parse other field sorry
Hope it helps

BasicDBObjectBuilder not appending mutiple criteria for a single object

I am using Java driver for mongo-db and trying to add multiple query criteria using BasicDBObjectBuilder. I have a text field where an XML is stored as String so we are using regex to form the query.
Below is my query and the output I am getting:
regexQuery.put("REQUEST_XML",BasicDBObjectBuilder
.start("$regex", ".*Main>[\r\n]<.?.?.?.?action>"+MainValue+".*")
.add("$regex", ".*Details>[\r\n]<.?.?.?.?action>" + DetailValue+ ".*").get());
regexQuery.put("NAME", "Video");
What I am getting as query is :
{ "REQUEST_XML" : { "$regex" : ".*Details>[\r\n]<.?.?.?.?action>Change.*"} , "NAME" : "Video"}
The first part with .start("$regex", ".Main>[\r\n]<.?.?.?.?action>"+MainValue+".") is not getting added to query.
Can you please let me know what is the issue ?
You are overwriting the key value pair. "$regex", ".*Details>[\r\n]<.?.?.?.?action>" + DetailValue+ ".*" overwrites "$regex", ".*Main>[\r\n]<.?.?.?.?action>"+MainValue+".*".
Use $or to pass both regex expression.
Something like
BasicDBObject regexQuery = new BasicDBObject();
regexQuery.put("$or", Arrays.asList(new BasicDBObject("REQUEST_XML", new BasicDBObject("$regex", ".*Main>[\r\n]<.?.?.?.?action>"+".*")),
new BasicDBObject("REQUEST_XML", new BasicDBObject("$regex", ".*Details>[\r\n]<.?.?.?.?action>"+".*"))));
regexQuery.put("NAME", "Video");
This should output query like
{ "$or" : [{ "REQUEST_XML" : { "$regex" : ".*Main>[\r\n]<.?.?.?.?action>.*" } }, { "REQUEST_XML" : { "$regex" : ".*Details>[\r\n]<.?.?.?.?action>.*" } }], "NAME" : "Video" }
Using 3.x driver
import static com.mongodb.client.model.Filters.or;
import static com.mongodb.client.model.Filters.regex;
Bson regexQuery = or(regex("REQUEST_XML", ".*Main>[\r\n]<.?.?.?.?action>"+".*"), regex("$regex", ".*Details>[\r\n]<.?.?.?.?action>"+".*"));

MongoDB Java - Fetching id in nested json

I have the following json structure. I am trying to retreive run the following mongo query in java where hData._id is not null.
MongoDb Query: db.Collection.find({},{"hData._id":1, "hData.createdBy":1} )
{
"_id" : ObjectId("55567e594e3256a23565ce58"),
"hData" : {
"isDeleted" : false,
"canDelete" : false,
"canUpdate" : false,
"createdBy" : “xyz”,
"createdDate" : "2015-05-15T15:05:30",
"_id" : "7"
},
"changeDate" : "2015-02-19T16:02:12",
}
The code i have written in java to fetch the hData._id is
MongoCursor<Document> cur = col.find(new BasicDBObject("hData._id", new BasicDBObject("$ne",null)))).iterator();
try{
while(cur.hasNext()){
System.out.println(cur.next().getObjectId("hData._id"));
i++;
}
}finally {
cur.close();
}
However, hData._id is returned as null. Could you help me with this ?
You can't get nested properties using dot notation, e.g. x.y.
So in your example you need to get hData first, then call get on the _id. Like this:
MongoCursor<Document> cur = col.find(new BasicDBObject("hData._id", new BasicDBObject("$ne",null))).iterator();
while(cur.hasNext()){
System.out.println(cur.next().get("hData", Document.class).getString("_id"));
}
Also note that in your example hData._id is shown as a String and not as an ObjectId, hence in my example I've used getString().
EDIT
Since it sounds like you may have mixed types for hData._id here's a more robust example with type checking and some extra debug output to illustrate:
MongoCursor<Document> cur = col.find(new BasicDBObject("hData._id", new BasicDBObject("$ne",null))).iterator();
while(cur.hasNext()){
Document doc = cur.next();
System.out.println("Document _id" + doc.get("_id"));
Document hdata = doc.get("hData", Document.class);
Object id = hdata.get("_id");
System.out.println("hData._id " + id);
// check type if you need to
if (id instanceof String) {
System.out.println("hData._id is String: " + id);
} else if (id instanceof ObjectId) {
System.out.println("hData._id is ObjectId: " + id);
} else {
System.out.println("hData._id is of type " + id.getClass().getName());
}
}
You can use Filters and Projections helper methods.
try (MongoCursor<Document> cur = coll.find(Filters.ne("hData._id", null)).projection(Projections.include("hData._id", "hData.createdBy")).iterator()) {
while(cur.hasNext()){
Document doc = cur.next();
Document hData = doc.get("hData", Document.class);
String id = hData.getString("_id");
String createdBy = hData.getString("createdBy");
}
}

ElasticSearch Jest client use: MalformedJsonException on any queries

I am trying to use a JEST Client to search the remotely located ElasticSearch index.
However I've ran into a problem - every single query, be it constructed using various builders or just default ES queries, everything returns
com.google.gson.stream.MalformedJsonException
Code:
String URL = "http://api.exiletools.com:80";
String API_KEY = "DEVELOPMENT-Indexer";
JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(new HttpClientConfig.Builder(URL)
.defaultCredentials("apikey", API_KEY)
.build());
JestClient client = factory.getObject();
qb = QueryBuilders
.boolQuery()
.must(QueryBuilders.termQuery("attributes.league", "Standard"))
.must(new TermQueryBuilder("attributes.equipType", "Ring"))
.must(new TermQueryBuilder("shop.verified", "yes"));
searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(qb);
query = searchSourceBuilder.toString();
search = new Search.Builder(query).build();
client.execute(search); // Here I get the error
As a final test I just copied the smallest query I could find from Jest integration test examples and just replaced the search terms there, to look like:
query = "{\n"
+ " \"query\" : {\n"
+ " \"term\" : { \"shop.chaosEquiv\" : \"167\" }\n"
+ " }\n"
+ "}";
This query when copied from the output stream looks like this:
{
"query" : {
"term" : { "shop.chaosEquiv" : "167" }
}
}
No trailing whitespaces or anything, looks valid to me.
Still getting the same error.
Can anyone tell what is going on?

Convert JSON to hash map with Jackson- Java

Before this is marked as a duplicate please read the question (I did look at similar ones). Thank you.
For simplicity, assume I have JSON like this:
{
"clients" : [
{
"name" : "client 1",
"id" : 1
},
{
"name" : "client 2",
"id" : 2
}
],
"other" : {
"something" : ""
}
...
}
So I want to create a hash map of only the clients and their fields. The basic question is how would I go about doing this using Jackson methods for a single JSON array like clients? I've tried to look online but all of the examples that I have seen either don't use Jackson or only are for a single JSON object like so:
HashMap<String, String>[] values = new ObjectMapper().readValue(jsonString, new TypeReference<HashMap<String, String>[]>() {});
I've also seen Gson examples and I know I can do some string parsing magic:
jsonSting = jsonString.substring(jsonString.indexOf("["), (jsonString.indexOf("]")+1))
to get it in a format that I can use, but I want to try it with Jackson to avoid importing another library. Any ideas?
Rephrasing the question:
So if I only had a list of clients like so:
jsonString = [{"name" : "client 1","id" : 1},{"name" : "client 2","id" : 2}]
then I could just do:
HashMap[] values = new ObjectMapper().readValue(jsonString, new TypeReference[]>() {});
to get what I want. I am basically asking if there is a way using Jackson methods to get the jsonString above from the large JSON section on top. I know I can easily do it with this example with string parsing but there will be more complex situations in the future and string parsing is not really considered best practice
You can extract a part of the JSON tree using the Jackson tree model API and then convert it to an array of maps.
Here is an example:
public class JacksonReadPart {
public static final String JSON = "{\n" +
" \"clients\" : [\n" +
" {\n" +
" \"name\" : \"client 1\",\n" +
" \"id\" : 1\n" +
" },\n" +
" {\n" +
" \"name\" : \"client 2\",\n" +
" \"id\" : 2\n" +
" }\n" +
"],\n" +
" \"other\" : {\n" +
" \"something\" : \"\"\n" +
" }\n" +
"\n" +
"}";
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(JSON).path("clients");
// non type safe
Map<String, Object>[] clients = mapper.treeToValue(node, Map[].class);
System.out.println(Arrays.toString(clients));
// type safe
JsonParser parser = mapper.treeAsTokens(node);
clients = parser.readValueAs(new TypeReference<Map<String, Object>[]>() {});
System.out.println(Arrays.toString(clients));
}
}
Output:
[{name=client 1, id=1}, {name=client 2, id=2}]
[{name=client 1, id=1}, {name=client 2, id=2}]

Categories

Resources