Elastic search multiple keywords in multiple fields - json

I'm trying to build a query in Elastic search to search inside the text of multiple fields for example:
Title and Description are the fields and I want to search for "obama", "world", "news" inside these 3 fields. I've tryed searching and found information in Elastic website here:
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html#_literal_fields_literal_and_per_field_boosting
But the problem is when I tried this structures it produce errors:
First:
{
"multi_match" : {
"query": "obama world news",
"type": "cross_fields",
"fields": [ "Title", "Description" ],
"operator": "and"
}
}
It produces error like this:
{
"error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][0]: SearchParseException[[republishan2][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [ {\n \"multi_match\" : {\n \"query\": \"obama world news\",\n \"type\": \"cross_fields\",\n \"fields\": [ \"Title\", \"Description\" ],\n \"operator\": \"and\"\n }\n }]]]; nested: SearchParseException[[republishan2][0]: from[-1],size[-1]: Parse Failure [No parser for element [multi_match]]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][3]: SearchParseException[[republishan2][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [ {\n \"multi_match\" : {\n \"query\": \"obama world news\",\n \"type\": \"cross_fields\",\n \"fields\": [ \"Title\", \"Description\" ],\n \"operator\": \"and\"\n }\n }]]]; nested: SearchParseException[[republishan2][3]: from[-1],size[-1]: Parse Failure [No parser for element [multi_match]]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][4]: SearchParseException[[republishan2][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [ {\n \"multi_match\" : {\n \"query\": \"obama world news\",\n \"type\": \"cross_fields\",\n \"fields\": [ \"Title\", \"Description\" ],\n \"operator\": \"and\"\n }\n }]]]; nested: SearchParseException[[republishan2][4]: from[-1],size[-1]: Parse Failure [No parser for element [multi_match]]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][1]: SearchParseException[[republishan2][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [ {\n \"multi_match\" : {\n \"query\": \"obama world news\",\n \"type\": \"cross_fields\",\n \"fields\": [ \"Title\", \"Description\" ],\n \"operator\": \"and\"\n }\n }]]]; nested: SearchParseException[[republishan2][1]: from[-1],size[-1]: Parse Failure [No parser for element [multi_match]]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][2]: SearchParseException[[republishan2][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [ {\n \"multi_match\" : {\n \"query\": \"obama world news\",\n \"type\": \"cross_fields\",\n \"fields\": [ \"Title\", \"Description\" ],\n \"operator\": \"and\"\n }\n }]]]; nested: SearchParseException[[republishan2][2]: from[-1],size[-1]: Parse Failure [No parser for element [multi_match]]]; }]",
"status": 400
}
I also have tried this query:
{
"sort": {
"re_max": {
"order": "desc"
}
},
"query": {
"multi_match" : {
"query": "obama world news",
"type": "cross_fields",
"fields": [ "Title", "Description" ],
"operator": "and"
}
},
"from": 0,
"size": 30}
}
And this is the result:
{
"error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][0]: SearchParseException[[republishan2][0]: from[-1],size[-1],sort[<custom:\"re_max\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource#1ef6a497>!]: Parse Failure [Failed to parse source [{\n\t\"sort\": {\n\t\t\"re_max\": {\n\t\t\t\"order\": \"desc\"\n\t\t\t}\n\t\t\t}, \n\t{\n \"multi_match\" : {\n \"query\": \"obama world news\",\n \"type\": \"cross_fields\",\n \"fields\": [ \"Title\", \"Description\" ],\n \"operator\": \"and\"\n }\n\t},\n\t\"from\": 0, \n\t\"size\": 30}\n}]]]; nested: JsonParseException[Unexpected character ('{' (code 123)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name\n at [Source: [B#52afc562; line: 7, column: 3]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][3]: SearchParseException[[republishan2][3]: from[-1],size[-1],sort[<custom:\"re_max\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource#364d93ed>!]: Parse Failure [Failed to parse source [{\n\t\"sort\": {\n\t\t\"re_max\": {\n\t\t\t\"order\": \"desc\"\n\t\t\t}\n\t\t\t}, \n\t{\n \"multi_match\" : {\n \"query\": \"obama world news\",\n \"type\": \"cross_fields\",\n \"fields\": [ \"Title\", \"Description\" ],\n \"operator\": \"and\"\n }\n\t},\n\t\"from\": 0, \n\t\"size\": 30}\n}]]]; nested: JsonParseException[Unexpected character ('{' (code 123)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name\n at [Source: [B#52afc562; line: 7, column: 3]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][4]: SearchParseException[[republishan2][4]: from[-1],size[-1],sort[<custom:\"re_max\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource#6e98bb14>!]: Parse Failure [Failed to parse source [{\n\t\"sort\": {\n\t\t\"re_max\": {\n\t\t\t\"order\": \"desc\"\n\t\t\t}\n\t\t\t}, \n\t{\n \"multi_match\" : {\n \"query\": \"obama world news\",\n \"type\": \"cross_fields\",\n \"fields\": [ \"Title\", \"Description\" ],\n \"operator\": \"and\"\n }\n\t},\n\t\"from\": 0, \n\t\"size\": 30}\n}]]]; nested: JsonParseException[Unexpected character ('{' (code 123)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name\n at [Source: [B#52afc562; line: 7, column: 3]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][1]: SearchParseException[[republishan2][1]: from[-1],size[-1],sort[<custom:\"re_max\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource#6c0543f7>!]: Parse Failure [Failed to parse source [{\n\t\"sort\": {\n\t\t\"re_max\": {\n\t\t\t\"order\": \"desc\"\n\t\t\t}\n\t\t\t}, \n\t{\n \"multi_match\" : {\n \"query\": \"obama world news\",\n \"type\": \"cross_fields\",\n \"fields\": [ \"Title\", \"Description\" ],\n \"operator\": \"and\"\n }\n\t},\n\t\"from\": 0, \n\t\"size\": 30}\n}]]]; nested: JsonParseException[Unexpected character ('{' (code 123)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name\n at [Source: [B#52afc562; line: 7, column: 3]]; }{[0CTkBM0pQGK1MzYEDlrpcQ][republishan2][2]: SearchParseException[[republishan2][2]: from[-1],size[-1],sort[<custom:\"re_max\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource#215084be>!]: Parse Failure [Failed to parse source [{\n\t\"sort\": {\n\t\t\"re_max\": {\n\t\t\t\"order\": \"desc\"\n\t\t\t}\n\t\t\t}, \n\t{\n \"multi_match\" : {\n \"query\": \"obama world news\",\n \"type\": \"cross_fields\",\n \"fields\": [ \"Title\", \"Description\" ],\n \"operator\": \"and\"\n }\n\t},\n\t\"from\": 0, \n\t\"size\": 30}\n}]]]; nested: JsonParseException[Unexpected character ('{' (code 123)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name\n at [Source: [B#52afc562; line: 7, column: 3]]; }]",
"status": 400
}
I'm using Postman Rest client to run the query. What's the problem with my code?

Try like this
{
"query": {
"multi_match": {
"query": "obama world news",
"type": "cross_fields",
"fields": ["Title", "Description"],
"operator": "and"
}
},
"from": 0,
"size": 20,
"sort": [
{
"re_max": {
"order": "desc"
}
}
]
}
HOpe it helps..!

Related

Access a JSON string value, undefined

I have this example where I am trying to access a JSON value, but I get undefined value for id.
My JSON:
{
"success": true,
"msg": "",
"obj": [
{
"remark": "test-1",
"settings": "{\n \"clients\": [\n {\n \"id\": \"430c867306d8\",\n \"alterId\": 0\n }\n ],\n \"disableInsecureEncryption\": false\n}",
},
{
"remark": "test-2",
"settings": "{\n \"clients\": [\n {\n \"id\": \"9831d43186de\",\n \"alterId\": 0\n }\n ],\n \"disableInsecureEncryption\": false\n}",
}
]
}
I want to fetch remark and id, so I wrote:
const remark = data.obj[i].remark;
const settings = data.obj[i].settings.clients[0].id;
Note: data is where my data is in actual code.
settings should be parsed before using
const settings = JSON.parse(data.obj[i].settings).clients[0].id;

Specific text in a string

I know there are at least hundreds of examples out there, but I just can't find the one that works best for me.
I have a string that is displayed like this (with all the spaces, the backslashes, etc):
string textInfo = "{\n \"code\": null,\n \"success\": true,\n \"data\": [\n {\n \"id\": 109322,\n \"name\": \"Name Surname\",\n \"display_name\": \"Name SURNAME\",\n \"city\": \"Bellair\\u00eb\",\n \"zip\": \"24000\",\n \"street\": \"boulevard 21\"\n }\n ],\n \"message\": null,\n \"total\": 1\n}"
i would like to extract for example only the id, or name or message on another string. For example:
string resultID = "109322" //for my id
string resultName = "Name Surname" //for my name
etc...
Thank you very much for your time and effort in helping me.
Kind Regards
What language are you tring to use?
As your string is in JSON format, try parsing it with a JSON parser and then reading from the object
As mentioned by Jon, this string is in JSON (Javascript Object Notation) format.
Each language has a method to convert from JSON into an object from which you can extract each attribute as a property. In javascript you can use JSON.stringify(textInfo) to represent the data in an object form.
The mechanics would look like the following:
jsonString = "{\n \"code\": null,\n \"success\": true,\n \"data\": [\n {\n \"id\": 109322,\n \"name\": \"Name Surname\",\n \"display_name\": \"Name SURNAME\",\n \"city\": \"Bellair\\u00eb\",\n \"zip\": \"24000\",\n \"street\": \"boulevard 21\"\n }\n ],\n \"message\": null,\n \"total\": 1\n}"
dataObject = JSON.parse(jsonString)
var resultID = dataObject.data.id //for my id
var resultName = dataObject.data.name //for my name
the rest of the properties can be similarly queried.
For reference, here is a "prettified" version of your JSON data:
{
"code":null,
"success":true,
"data":[
{
"id":109322,
"name":"Name Surname",
"display_name":"Name SURNAME",
"city":"Bellairë",
"zip":"24000",
"street":"boulevard 21"
}
],
"message":null,
"total":1
}
I feel so bad not trying harder. Anyway here is my working code for Windows Application C#
string jsonString = "{\n \"code\": null,\n \"success\": true,\n \"data\": [\n {\n \"id\": 109322,\n \"name\": \"Name Surname\",\n \"display_name\": \"Name SURNAME\",\n \"city\": \"Bellair\\u00eb\",\n \"zip\": \"24000\",\n \"street\": \"boulevard 21\"\n }\n ],\n \"message\": null,\n \"total\": 1\n}"
JObject jsonObj = JObject.Parse(jsonString);
return (string)jsonObj["data"][0]["id"];
And output is: 109322

Nativescript/Google Geocoding JSON Parse

I am reading a geocoding from Google API in my Nativescript code. The results are coming this way:
{"_bodyInit":"{\n \"results\" : [\n {\n \"address_components\" : [\n {\n \"long_name\" : \"Cooper City\",\n \"short_name\" : \"Cooper City\",\n \"types\" : [ \"locality\", \"political\" ]\n },\n {\n \"long_name\" : \"Broward County\",\n \"short_name\" : \"Broward County\",\n \"types\" : [ \"administrative_area_level_2\", \"political\" ]\n },\n {\n \"long_name\" : \"Florida\",\n \"short_name\" : \"FL\",\n \"types\" : [ \"administrative_area_level_1\", \"political\" ]\n },\n {\n \"long_name\" : \"United States\",\n \"short_name\" : \"US\",\n \"types\" : [ \"country\", \"political\" ]\n }\n ],\n \"formatted_address\" : \"Cooper City, FL, USA\",\n \"geometry\" : {\n
when reading from this code:
fetchModule.fetch(geoPlace, {
method: "GET"
})
.then(function(response) {
alert({title: "GET Response", message: JSON.stringify(response), okButtonText: "Close"});
console.log(JSON.stringify(response))
}, function(error) {
console.log(JSON.stringify(error));
})
My question is, how to get access, for example, to "Cooper City"?
I have tried (without success):
console.log(response.value["results"])
console.log(response[0])
console.log(response.results[0])
making a revision of the response I identify that it is not a successful JSON format, you should look for the way to convert it into JSON format to be able to use its code and get to the tag you need.
https://jsonformatter.curiousconcept.com/
Here's how I found the solution:
var m = JSON.parse(response._bodyInit)
console.log(m.results[0].address_components[1].short_name)
For some reason I don't know the original results did not return as a valid JSON.

How to do nested boolean filters for finding exact matcing values on same field in elasticsearch?

My Index mapping is shown below:
curl -X PUT localhost:9200/testing/listings/_mapping -d '{
"listings" : {
"properties" : {
"address" : {
"properties": {
"location": { "type" : "string",
"index" : "not_analyzed"
}
}
},
"suggest" : {
"type" : "completion",
"index_analyzer" : "simple",
"search_analyzer" : "simple",
"payloads" : true
}
}
}
}'
I need to perform the action of following query in elasticsearch
SELECT * FROM LISTINGS WHERE (published = true AND inActive = false) AND (residentialKind = "Villa" OR residentialKind = "Apartment");
To execute above query action i used the following nested bool query in elasticsearch.
{"query":{
"filtered": {
"filter": {
"bool":{
"must":[
{"match":{"published":true}},
{"match":{"inActive":false}},
{"bool":
{"should": [
{"term":{"residentialKind":"Villa"}},
{"term":{"residentialKind":"Apartment"}}
]
}
}
]
}
}
}
}
}
Its giving following error
{
"error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[B5SbtdlkQTGL0WU-dvg2yg][propgod][0]: SearchParseException[[propgod][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\n\t\"filtered\": {\n \t\"filter\": {\n \t\t\"bool\":{\n \t\t\t\"must\":[\n {\"match\":{\"published\":true}},\n {\"match\":{\"inActive\":false}},\n {\"bool\":\n \t\t{\"should\": [\n \t\t{\"term\":{\"residentialKind\":\"Villa\"}},\n {\"term\":{\"residentialKind\":\"Apartment\"}} \n \t]\n }\n }\n \t\t\t]\n \t\t\t}\n \t}\n }\n }\n}]]]; nested: QueryParsingException[[propgod] No filter registered for [match]]; }{[B5SbtdlkQTGL0WU-dvg2yg][propgod][1]: SearchParseException[[propgod][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\n\t\"filtered\": {\n \t\"filter\": {\n \t\t\"bool\":{\n \t\t\t\"must\":[\n {\"match\":{\"published\":true}},\n {\"match\":{\"inActive\":false}},\n {\"bool\":\n \t\t{\"should\": [\n \t\t{\"term\":{\"residentialKind\":\"Villa\"}},\n {\"term\":{\"residentialKind\":\"Apartment\"}} \n \t]\n }\n }\n \t\t\t]\n \t\t\t}\n \t}\n }\n }\n}]]]; nested: QueryParsingException[[propgod] No filter registered for [match]]; }{[B5SbtdlkQTGL0WU-dvg2yg][propgod][2]: SearchParseException[[propgod][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\n\t\"filtered\": {\n \t\"filter\": {\n \t\t\"bool\":{\n \t\t\t\"must\":[\n {\"match\":{\"published\":true}},\n {\"match\":{\"inActive\":false}},\n {\"bool\":\n \t\t{\"should\": [\n \t\t{\"term\":{\"residentialKind\":\"Villa\"}},\n {\"term\":{\"residentialKind\":\"Apartment\"}} \n \t]\n }\n }\n \t\t\t]\n \t\t\t}\n \t}\n }\n }\n}]]]; nested: QueryParsingException[[propgod] No filter registered for [match]]; }{[B5SbtdlkQTGL0WU-dvg2yg][propgod][3]: SearchParseException[[propgod][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\n\t\"filtered\": {\n \t\"filter\": {\n \t\t\"bool\":{\n \t\t\t\"must\":[\n {\"match\":{\"published\":true}},\n {\"match\":{\"inActive\":false}},\n {\"bool\":\n \t\t{\"should\": [\n \t\t{\"term\":{\"residentialKind\":\"Villa\"}},\n {\"term\":{\"residentialKind\":\"Apartment\"}} \n \t]\n }\n }\n \t\t\t]\n \t\t\t}\n \t}\n }\n }\n}]]]; nested: QueryParsingException[[propgod] No filter registered for [match]]; }{[B5SbtdlkQTGL0WU-dvg2yg][propgod][4]: SearchParseException[[propgod][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\n\t\"filtered\": {\n \t\"filter\": {\n \t\t\"bool\":{\n \t\t\t\"must\":[\n {\"match\":{\"published\":true}},\n {\"match\":{\"inActive\":false}},\n {\"bool\":\n \t\t{\"should\": [\n \t\t{\"term\":{\"residentialKind\":\"Villa\"}},\n {\"term\":{\"residentialKind\":\"Apartment\"}} \n \t]\n }\n }\n \t\t\t]\n \t\t\t}\n \t}\n }\n }\n}]]]; nested: QueryParsingException[[propgod] No filter registered for [match]]; }]",
"status": 400
}
So if i wrong with my query please help me to fix this query. Thanks in Advance.
"match" is a query, not a filter, so you're getting an error because you tried to use it as a filter. This should do what you want (assuming "residentialKind" isn't analyzed):
POST /test_index/_search
{
"query": {
"bool":{
"must":[
{"match":{"published":true}},
{"match":{"inActive":false}},
{"bool":
{"should": [
{"term":{"residentialKind":"Villa"}},
{"term":{"residentialKind":"Apartment"}}
]
}
}
]
}
}
}
If "residentialKind" IS analyzed with something like the standard analyzer, this should work:
POST /test_index/_search
{
"query": {
"bool":{
"must":[
{"match":{"published":true}},
{"match":{"inActive":false}},
{"bool":
{"should": [
{"match":{"residentialKind":"Villa"}},
{"match":{"residentialKind":"Apartment"}}
]
}
}
]
}
}
}
or even
POST /test_index/_search
{
"query":{
"filtered": {
"filter": {
"bool":{
"must":[
{"term":{"published":true}},
{"term":{"inActive":false}},
{"bool":
{"should": [
{"term":{"residentialKind":"villa"}},
{"term":{"residentialKind":"apartment"}}
]
}
}
]
}
}
}
}
}
Here is the code I used for testing:
http://sense.qbox.io/gist/bbed61ef297b058c92d9c7f1523479dfeb3c35b2

jsonschema.core.exceptions.InvalidSchemaException: fatal: core.invalidSchema level: "fatal"

My schema is :
{
"title": "Order",
"description": "An order from oms",
"type": "object",
"properties": {
"order_id": {
"description": "The unique identifier for an order",
"type": "number"
},
"order_bill_from_party_id": {
"description": "The unique identifier for a party",
"type": "string"
},
"test":{
"type":"integer"
}
},
"required": ["order_id"]
}
My input is :
{"order_bill_from_party_id":"abc",
"order_id":1234}
My validator code :
val factory : JsonSchemaFactory= JsonSchemaFactory.byDefault()
val validator: JsonValidator = factory.getValidator
val schemaJson: JsonNode = JsonNodeFactory.instance.textNode(schema)
val inputJson = JsonNodeFactory.instance.textNode(input)
println(schemaJson)
val report: ProcessingReport = validator.validate(schemaJson, inputJson)
EDIT :
The schemaJson takes form :
{\n
\"title\": \"Order\",\n
\"description\": \"An order from oms\",\n
\"type\": \"object\",\n
\"properties\": {\n
\"order_id\": {\n
\"description\": \"The unique identifier for an order\",\n
\"type\": \"number\"\n
},\n
\"order_bill_from_party_id\": {\n
\"description\": \"The unique identifier for a party\",\n
\"type\": \"string\"\n
},\n
\"test\":{\n
\"type\":\"integer\"\n
}\n
},\n
\"required\": [\"order_id\"]\n
}
However I am getting an exception :
org.specs.runner.SpecError: com.github.fge.jsonschema.core.exceptions.InvalidSchemaException: fatal: core.invalidSchema
level: "fatal"
org.specs.runner.UserError: com.github.fge.jsonschema.core.exceptions.InvalidSchemaException: fatal: core.invalidSchema
level: "fatal"
at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:86)
at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:48)
at com.github.fge.jsonschema.core.processing.ProcessingResult.of(ProcessingResult.java:78)
at com.github.fge.jsonschema.main.JsonValidator.validate(JsonValidator.java:103)
at com.github.fge.jsonschema.main.JsonValidator.validate(JsonValidator.java:123)
at com.flipkart.marketing.bro.core.ValidationSchema.validate(ValidationSchema.scala:25)
at com.flipkart.marketing.bro.core.ValidationSchemaTest$$anonfun$3$$anonfun$apply$1.apply$mcV$sp(ValidationSchemaTest.scala:29)
at com.flipkart.marketing.bro.core.ValidationSchemaTest$$anonfun$3$$anonfun$apply$1.apply(ValidationSchemaTest.scala:27)
at com.flipkart.marketing.bro.core.ValidationSchemaTest$$anonfun$3$$anonfun$apply$1.apply(ValidationSchemaTest.scala:27)
at org.specs.specification.LifeCycle$class.withCurrent(ExampleLifeCycle.scala:66)
at org.specs.specification.Examples.withCurrent(Examples.scala:52)
However on testing on :
http://json-schema-validator.herokuapp.com/
I am getting a
`success
[]`.
Can someone help me out? Testing sites imply my schema is correct but my code is giving excpetions. I think it is because of the escape characters. How can I fix this and remove the escape characters in the JsonNode?
Thanks in advance!
Use JsonLoader.fromString instead of JsonNodeFactory.instance.textNode to load your schema and your instance:
val schemaJson= JsonLoader.fromString(schema)
val inputJson = JsonLoader.fromString(input)
textNode constructs a single node containing a text value, which is not what you are after.