I'm trying to write a java JUnit test that will deploy template-defined resource groups using the java-sdk for azure.
I came across a couple examples and followed them to create this code segment:
public void azurePoC() throws Exception {
Azure azure = Azure.configure()
.withLogLevel(LogLevel.BODY)
.authenticate(new File("C:/somePath/credentials.azure"))
.withDefaultSubscription();
final ObjectMapper mapper = new ObjectMapper();
JsonNode template = mapper.readTree(new File("C:/somePath/template.json"));
Logger.report("template", template.toString());
JsonNode params = mapper.readTree(new File("C:/somePath/parameters.json"));
Logger.report("params", params.toString());
azure.deployments()
.define("myNewResource")
.withNewResourceGroup("myNewResourceGroup", Region.US_WEST2)
.withTemplate(template)
.withParameters(params)
.withMode(DeploymentMode.INCREMENTAL)
.create();
Logger.report("Request for deployment done.");
}
I have verified that the API allows me to lookup resources using azure.virtualMachines().list() method, therefore i conclude that my credentials are okay.
I am using one of the quick-start templates to for my PoC, just to see if i can deploy some resource.
My template.json file:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"adminUsername": {
"type": "string",
"metadata": {
"description": "User name for the Virtual Machine."
}
},
"adminPassword": {
"type": "securestring",
"metadata": {
"description": "Password for the Virtual Machine."
}
},
"dnsLabelPrefix": {
"type": "string",
"metadata": {
"description": "Unique DNS Name for the Public IP used to access the Virtual Machine."
}
},
"ubuntuOSVersion": {
"type": "string",
"defaultValue": "16.04.0-LTS",
"allowedValues": [
"12.04.5-LTS",
"14.04.5-LTS",
"15.10",
"16.04.0-LTS"
],
"metadata": {
"description": "The Ubuntu version for the VM. This will pick a fully patched image of this given Ubuntu version."
}
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "Location for all resources."
}
}
},
"variables": {
"storageAccountName": "[concat(uniquestring(resourceGroup().id), 'salinuxvm')]",
"imagePublisher": "Canonical",
"imageOffer": "UbuntuServer",
"nicName": "myVMNic",
"addressPrefix": "10.0.0.0/16",
"subnetName": "Subnet",
"subnetPrefix": "10.0.0.0/24",
"storageAccountType": "Standard_LRS",
"publicIPAddressName": "myPublicIP",
"publicIPAddressType": "Dynamic",
"vmName": "MyUbuntuVM",
"vmSize": "Standard_A1",
"virtualNetworkName": "MyVNET",
"subnetRef": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('virtualNetworkName'), variables('subnetName'))]"
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"name": "[variables('storageAccountName')]",
"apiVersion": "2018-07-01",
"location": "[parameters('location')]",
"sku": {
"name": "[variables('storageAccountType')]"
},
"kind": "Storage",
"properties": {}
},
{
"apiVersion": "2018-10-01",
"type": "Microsoft.Network/publicIPAddresses",
"name": "[variables('publicIPAddressName')]",
"location": "[parameters('location')]",
"properties": {
"publicIPAllocationMethod": "[variables('publicIPAddressType')]",
"dnsSettings": {
"domainNameLabel": "[parameters('dnsLabelPrefix')]"
}
}
},
{
"apiVersion": "2018-10-01",
"type": "Microsoft.Network/virtualNetworks",
"name": "[variables('virtualNetworkName')]",
"location": "[parameters('location')]",
"properties": {
"addressSpace": {
"addressPrefixes": [
"[variables('addressPrefix')]"
]
},
"subnets": [
{
"name": "[variables('subnetName')]",
"properties": {
"addressPrefix": "[variables('subnetPrefix')]"
}
}
]
}
},
{
"apiVersion": "2018-10-01",
"type": "Microsoft.Network/networkInterfaces",
"name": "[variables('nicName')]",
"location": "[parameters('location')]",
"dependsOn": [
"[resourceId('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]",
"[resourceId('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]"
],
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": {
"privateIPAllocationMethod": "Dynamic",
"publicIPAddress": {
"id": "[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPAddressName'))]"
},
"subnet": {
"id": "[variables('subnetRef')]"
}
}
}
]
}
},
{
"apiVersion": "2018-10-01",
"type": "Microsoft.Compute/virtualMachines",
"name": "[variables('vmName')]",
"location": "[parameters('location')]",
"dependsOn": [
"[resourceId('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]",
"[resourceId('Microsoft.Network/networkInterfaces/', variables('nicName'))]"
],
"properties": {
"hardwareProfile": {
"vmSize": "[variables('vmSize')]"
},
"osProfile": {
"computerName": "[variables('vmName')]",
"adminUsername": "[parameters('adminUsername')]",
"adminPassword": "[parameters('adminPassword')]"
},
"storageProfile": {
"imageReference": {
"publisher": "[variables('imagePublisher')]",
"offer": "[variables('imageOffer')]",
"sku": "[parameters('ubuntuOSVersion')]",
"version": "latest"
},
"osDisk": {
"createOption": "FromImage"
},
"dataDisks": [
{
"diskSizeGB": 1023,
"lun": 0,
"createOption": "Empty"
}
]
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces',variables('nicName'))]"
}
]
},
"diagnosticsProfile": {
"bootDiagnostics": {
"enabled": true,
"storageUri": "[concat(reference(concat('Microsoft.Storage/storageAccounts/', variables('storageAccountName')), '2016-01-01').primaryEndpoints.blob)]"
}
}
}
}
],
"outputs": {
"hostname": {
"type": "string",
"value": "[reference(variables('publicIPAddressName')).dnsSettings.fqdn]"
},
"sshCommand": {
"type": "string",
"value": "[concat('ssh ', parameters('adminUsername'), '#', reference(variables('publicIPAddressName')).dnsSettings.fqdn)]"
}
}
}
My parameters.json file:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"adminUsername": {
"value": "ghuser"
},
"adminPassword": {
"value": "GEN-PASSWORD"
},
"dnsLabelPrefix": {
"value": "GEN-UNIQUE"
}
}
}
Whenever I run the application I am confronted with this exception:
com.microsoft.azure.CloudException: The request content was invalid and could not be deserialized: 'Error converting value "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#" to type 'Microsoft.WindowsAzure.ResourceStack.Frontdoor.Data.Definitions.DeploymentParameterDefinition'. Path 'properties.parameters.$schema', line 1, position 4459.'.: The request content was invalid and could not be deserialized: 'Error converting value "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#" to type 'Microsoft.WindowsAzure.ResourceStack.Frontdoor.Data.Definitions.DeploymentParameterDefinition'. Path 'properties.parameters.$schema', line 1, position 4459.'.
at com.microsoft.azure.AzureClient.createExceptionFromResponse(AzureClient.java:740)
at com.microsoft.azure.AzureClient.access$100(AzureClient.java:33)
at com.microsoft.azure.AzureClient$3.call(AzureClient.java:160)
at com.microsoft.azure.AzureClient$3.call(AzureClient.java:157)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(OnSubscribeMap.java:69)
at retrofit2.adapter.rxjava.CallArbiter.deliverResponse(CallArbiter.java:120)
at retrofit2.adapter.rxjava.CallArbiter.emitResponse(CallArbiter.java:102)
at retrofit2.adapter.rxjava.CallExecuteOnSubscribe.call(CallExecuteOnSubscribe.java:46)
at retrofit2.adapter.rxjava.CallExecuteOnSubscribe.call(CallExecuteOnSubscribe.java:24)
at rx.Observable.unsafeSubscribe(Observable.java:10327)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
at rx.Observable.unsafeSubscribe(Observable.java:10327)
at rx.internal.operators.OnSubscribeSingle.call(OnSubscribeSingle.java:81)
at rx.internal.operators.OnSubscribeSingle.call(OnSubscribeSingle.java:27)
at rx.internal.operators.SingleToObservable.call(SingleToObservable.java:39)
at rx.internal.operators.SingleToObservable.call(SingleToObservable.java:27)
at rx.Observable.unsafeSubscribe(Observable.java:10327)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.Observable.unsafeSubscribe(Observable.java:10327)
at rx.internal.operators.DeferredScalarSubscriber.subscribeTo(DeferredScalarSubscriber.java:153)
at rx.internal.operators.OnSubscribeTakeLastOne.call(OnSubscribeTakeLastOne.java:32)
at rx.internal.operators.OnSubscribeTakeLastOne.call(OnSubscribeTakeLastOne.java:22)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.Observable.unsafeSubscribe(Observable.java:10327)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
at rx.Observable.unsafeSubscribe(Observable.java:10327)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
at rx.Observable.unsafeSubscribe(Observable.java:10327)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
at rx.Observable.unsafeSubscribe(Observable.java:10327)
at rx.internal.operators.OperatorSubscribeOn$SubscribeOnSubscriber.call(OperatorSubscribeOn.java:100)
at rx.internal.schedulers.CachedThreadScheduler$EventLoopWorker$1.call(CachedThreadScheduler.java:230)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: rx.exceptions.OnErrorThrowable$OnNextValue: OnError while emitting onNext value: retrofit2.Response.class
at rx.exceptions.OnErrorThrowable.addValueAsLastCause(OnErrorThrowable.java:118)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(OnSubscribeMap.java:73)
... 43 more
Any and all help in the matter will be greatly appreciated.
For anyone who comes across this question.
Turns out the problem was the parameter file json syntax.
The schema, parameters and version fields are redundant
this is the expected syntax using Azure Java SDK:
{
"adminUsername": {
"value": "ghuser"
},
"adminPassword": {
"value": "GEN-PASSWORD"
},
"dnsLabelPrefix": {
"value": "GEN-UNIQUE"
}
}
Related
I have a json schema from which I need to generate java classes
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://schema.softsense.io/analysis/vulnerabilities/v1alpha1/vulnerabilities.json",
"title": "Vulnerabilities",
"description": "SoftSense Vulnerabilities analysis schema v1alpha3",
"type": "object",
"allOf": [ { "$ref": "https://schema.softsense.io/analysis/v1alpha3/base.json" } ],
"properties": {
"vulnerabilities": {
"items": {
"$ref": "#/definitions/Vulnerability"
},
"type": "array"
}
},
"oneOf": [{ "required": ["vulnerabilities"] }, { "required": ["error"] }],
"definitions": {
"Vulnerability": {
"required": [
"id",
"description",
"sources"
],
"properties": {
"id": {
"description": "Identifier for the vulnerability, such as a CVE identifier (CVE-2021-12345).",
"type": "string"
},
"published": {
"description": "UTC time when the vulnerability was published",
"type": "string",
"format": "date-time"
},
"updated": {
"description": "UTC time when the vulnerability was updated",
"type": "string",
"format": "date-time"
},
"description": {
"description": "Description of the vulnerability",
"type": "string"
},
"sources": {
"description": "The collection of backing vulnerability sources that listed this vulnerability for the software component",
"items": {
"type": "string"
},
"type": "array"
},
"cwes": {
"description": "The collection of Common Weakness Enumeration (CWE) identifiers that characterize the vulnerability.",
"items": {
"$ref": "#/definitions/CWE"
},
"type": "array"
},
"references": {
"description": "External URLs related to the vulnerability, such as issue trackers, mailing list discussions, commits, or patches.",
"items": {
"$ref": "#/definitions/Reference"
},
"type": "array"
},
"impact": {
"description": "What impact this vulnerability has",
"$ref": "#/definitions/Impact"
}
},
"type": "object"
},
"Impact": {
"properties": {
"metricV3": {
"$ref": "#/definitions/ImpactMetricV3"
},
"metricV2": {
"$ref": "#/definitions/ImpactMetricV2"
},
"*": {
"type": "object"
}
},
"type": "object"
},
"ImpactMetricV3": {
"properties": {
"cvssV3": {
"$ref": "#/definitions/CVSSv3"
},
"exploitabilityScore": {
"type": "number"
},
"impactScore": {
"type": "number"
}
}
},
"CVSSv3": {
"required": [
"version",
"vectorString",
"baseScore",
"baseSeverity"
],
"properties": {
"version": {
"type": "string"
},
"vectorString": {
"type": "string"
},
"attackVector": {
"type": "string"
},
"attackComplexity": {
"type": "string"
},
"privilegesRequired": {
"type": "string"
},
"userInteraction": {
"type": "string"
},
"scope": {
"type": "string"
},
"confidentialityImpact": {
"type": "string"
},
"integrityImpact": {
"type": "string"
},
"availabilityImpact": {
"type": "string"
},
"baseScore": {
"type": "number"
},
"baseSeverity": {
"type": "string"
},
"exploitCodeMaturity": {
"type": "string"
},
"remediationLevel": {
"type": "string"
},
"reportConfidence": {
"type": "string"
},
"temporalScore": {
"type": "number"
},
"temporalSeverity": {
"type": "string"
},
"confidentialityRequirement": {
"type": "string"
},
"integrityRequirement": {
"type": "string"
},
"availabilityRequirement": {
"type": "string"
},
"modifiedAttackVector": {
"type": "string"
},
"modifiedAttackComplexity": {
"type": "string"
},
"modifiedPrivilegesRequired": {
"type": "string"
},
"modifiedUserInteraction": {
"type": "string"
},
"modifiedScope": {
"type": "string"
},
"modifiedConfidentialityImpact": {
"type": "string"
},
"modifiedIntegrityImpact": {
"type": "string"
},
"modifiedAvailabilityImpact": {
"type": "string"
},
"environmentalScore": {
"type": "number"
},
"environmentalSeverity": {
"type": "string"
}
},
"type": "object"
},
"ImpactMetricV2": {
"required": [
"cvssV2",
"severity",
"exploitabilityScore",
"impactScore",
"obtainAllPrivilege",
"obtainUserPrivilege",
"obtainOtherPrivilege",
"userInteractionRequired"
],
"properties": {
"cvssV2": {
"$ref": "#/definitions/CVSSv2"
},
"severity": {
"type": "string"
},
"exploitabilityScore": {
"type": "number"
},
"impactScore": {
"type": "number"
},
"acInsufInfo": {
"type": "boolean"
},
"obtainAllPrivilege": {
"type": "boolean"
},
"obtainUserPrivilege": {
"type": "boolean"
},
"obtainOtherPrivilege": {
"type": "boolean"
},
"userInteractionRequired": {
"type": "boolean"
}
},
"type": "object"
},
"CVSSv2": {
"required": [
"version",
"vectorString",
"baseScore"
],
"properties": {
"version": {
"type": "string"
},
"vectorString": {
"type": "string"
},
"accessVector": {
"type": "string"
},
"accessComplexity": {
"type": "string"
},
"authentication": {
"type": "string"
},
"confidentialityImpact": {
"type": "string"
},
"integrityImpact": {
"type": "string"
},
"availabilityImpact": {
"type": "string"
},
"baseScore": {
"type": "number"
},
"exploitability": {
"type": "string"
},
"remediationLevel": {
"type": "string"
},
"reportConfidence": {
"type": "string"
},
"temporalScore": {
"type": "number"
},
"collateralDamagePotential": {
"type": "string"
},
"targetDistribution": {
"type": "string"
},
"confidentialityRequirement": {
"type": "string"
},
"integrityRequirement": {
"type": "string"
},
"availabilityRequirement": {
"type": "string"
},
"environmentalScore": {
"type": "number"
}
},
"type": "object"
},
"CWE": {
"required": [
"name"
],
"properties": {
"name": {
"type": "string"
},
"description": {
"type": "string"
}
},
"type": "object"
},
"Reference": {
"required": [
"url"
],
"properties": {
"name": {
"type": "string"
},
"url": {
"type": "string"
},
"tags": {
"items": {
"type": "string"
},
"type": "array"
}
},
"type": "object"
}
}
}
I am trying to use the jsonschema2pojo as under.
<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
<version>0.4.34</version>
<configuration>
<sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
<targetPackage>com.test.gen</targetPackage>
<useCommonsLang3>true</useCommonsLang3>
</configuration>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
For some reason I get the following error when i use mvn generate-sources
[ERROR] Failed to execute goal org.jsonschema2pojo:jsonschema2pojo-maven-plugin:1.1.1:generate (default) on project hsk: Execution default of goal org.jsonschema2pojo:jsonschema2pojo-maven-plugin:1.1.1:generate failed: String index out of range: 0 -> [Help 1]
Any help is highly appreciated
Regards,
I created an Index for a few documents. However I am facing trouble while running for a search query as it is showing zero hits. Find the indexing file, indexed data and and Java code below:
Mapping.json:
{
"settings": {
"index": {
"max_ngram_diff": 39
},
"analysis": {
"normalizer": {
"custom_normalizer": {
"type": "custom",
"char_filter": [],
"filter": [
"lowercase",
"asciifolding"
]
}
},
"analyzer": {
"custom_analyzer": {
"tokenizer": "custom_tokenizer",
"filter": [
"lowercase"
]
},
"autocomplete_search": {
"type": "custom",
"tokenizer": "keyword",
"filter": "lowercase"
}
},
"tokenizer": {
"custom_tokenizer": {
"type": "ngram",
"min_gram": 1,
"max_gram": 40,
"token_chars": [
"letter",
"digit",
"whitespace",
"punctuation",
"symbol"
]
}
}
}
},
"mappings": {
"type_name": {
"properties": {
"primaryTerm": {
"type": "text",
"analyzer": "custom_analyzer",
"search_analyzer": "autocomplete_search",
"fielddata": "true",
"fields": {
"raw": {
"type": "keyword",
"normalizer": "custom_normalizer"
}
}
},
"entityType": {
"type": "keyword",
"normalizer": "custom_normalizer"
},
"variants": {
"type": "text",
"analyzer": "custom_analyzer",
"search_analyzer": "autocomplete_search",
"fielddata": "true",
"fields": {
"raw": {
"type": "keyword",
"normalizer": "custom_normalizer"
}
}
}
}
}
}
}
Elastic data.json:
{
"took": 82,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "relmap",
"_type": "type_name",
"_id": "X4NutXcBKX-KYFzehi4s",
"_score": 1,
"_source": {
"entityType": "Agency",
"primaryTerm": "K2IPR INTELLECTUAL PROPERTY ATTORNEYS",
"variants": [
"K2IPR",
"K2I.P.R. (PATENT & TRADEMARK ATTRONEYS)",
"K2I.P.R",
"K2.I.P.R",
"K2 I.P.R",
"K2 I.P.R",
"K2 IPR",
"K2 I. P. R."
]
}
},
{
"_index": "relmap",
"_type": "type_name",
"_id": "YINutXcBKX-KYFzeoy7V",
"_score": 1,
"_source": {
"entityType": "Agency",
"primaryTerm": "EPIPHANY IP SOLUTIONS PVT. LTD.",
"variants": [
"EPIPHANY IP SOLUTIONS PRIVATE LIMITED",
"EPIPHANY IP SOLUTIONS PVT. LTD"
]
}
},
{
"_index": "relmap",
"_type": "type_name",
"_id": "YYNutXcBKX-KYFzepC44",
"_score": 1,
"_source": {
"entityType": "Agency",
"primaryTerm": "Natural Remedies Private Limited",
"variants": [
"NATURAL REMEDIES PVT. LTD",
"NATURAL REMEDIES PVT LTD",
"NATURAL REMEDIES"
]
}
},
{
"_index": "relmap",
"_type": "type_name",
"_id": "YoNutXcBKX-KYFzepC5i",
"_score": 1,
"_source": {
"entityType": "Agency",
"primaryTerm": "NICHE, INTELLECTUAL PROPERTY OFFICES",
"variants": [
"NICHE INTELLECTUAL PROPERTY OFFICES",
"NICHE INTELLECTUAL PROPERTY OFFICECS",
"NICHE INTELLECTUAL PROPERTY LAW OFFICES"
]
}
}
]
}
}
Search query:
String query="{\"query_string\":{\"query\":\""+searchTerm.toUpperCase()+"\",\"default_operator\":\"AND\",\"default_field\":\"primaryTerm\"}}";
Java Code for elastic Query:
SearchResponse searchResponse=null;
try {
String queryJson=json;
System.out.println("queryJson:"+queryJson);
//System.out.println(indexName);
SearchRequest searchRequest = new SearchRequest(indexName);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.from(start)
.size(length)
.query(QueryBuilders.wrapperQuery(queryJson));
searchRequest.source(searchSourceBuilder);
searchResponse = elasticsearchRestTemplate.getClient().search(searchRequest, RequestOptions.DEFAULT);
For a Search Term = 'Ni' in an autosuggest drpDown I am getting zero hits for the field 'primaryTerm'
I also tried the termQuerybuilder instead of wrapper query:
SearchRequestBuilder searchRequestBuilder = client.prepareSearch("relmap");
SearchResponse searchResponse = searchRequestBuilder.
setQuery(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("variants", "NATURAL REMEDIES PVT. LTD"))).setFrom(0).setSize(1000).setExplain(true).setTrackTotalHits(true).execute().actionGet();
SearchHits searchHits = searchResponse.getHits();
Interestinlgy I am getting the desired output with elastic url.
Elastic URI code:
http://localhost:9201/relmap/_search?q=NIC&df=primaryTerm&explain=true&default_operator=AND
Can anyone help let me know the fault /correct way to get the hits from the query
In highlighting search for my application how search works for us is if we pass pageSize it will only return one matching field from record.
For example
There are 4 records
remaining information added in comments as I am unable add code here please advice how can I achieve this requirement
If you need to highlight the result of all matching fields of each document, then you can achieve this in the following way:
Adding a working example with index data, mapping, search query, and search result
Index Mapping:
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "my_tokenizer"
}
},
"tokenizer": {
"my_tokenizer": {
"type": "edge_ngram",
"min_gram": 2,
"max_gram": 20,
"token_chars": [
"letter",
"digit"
]
}
}
},
"max_ngram_diff": 50
},
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "my_analyzer"
},
"lastname": {
"type": "text",
"analyzer": "my_analyzer"
},
"firstname": {
"type": "text",
"analyzer": "my_analyzer"
}
}
}
}
Index Data:
{
"name": "jhon",
"Age": 24,
"lastname": "willam",
"firstname": "henry"
}
{
"name": "kellin",
"Age": 24,
"lastname": "kevin",
"firstname": "mathew"
}
{
"name": "keeper",
"Age": 24,
"lastname": "Jr",
"firstname": "gomez"
}
{
"name": "Asif",
"Age": 24,
"lastname": "peter",
"firstname": "willaim kemp"
}
Search Query:
{
"query": {
"multi_match": {
"query": "ke"
}
},
"highlight": {
"fields": {
"*": {
"type": "plain",
"fragment_size": 20,
"pre_tags": "<span class='bold'>",
"post_tags": "</span>",
"number_of_fragments": 1
}
}
}
}
Search Result:
"hits": [
{
"_index": "64996939",
"_type": "_doc",
"_id": "2",
"_score": 1.1374959,
"_source": {
"name": "kellin",
"Age": 24,
"lastname": "kevin",
"firstname": "mathew"
},
"highlight": {
"name": [
"<span class='bold'>ke</span>llin"
], <-- note this
"lastname": [
"<span class='bold'>ke</span>vin"
]
}
},
{
"_index": "64996939",
"_type": "_doc",
"_id": "4",
"_score": 0.9552834,
"_source": {
"name": "Asif",
"Age": 24,
"lastname": "peter",
"firstname": "willaim kemp"
},
"highlight": {
"firstname": [
"willaim <span class='bold'>ke</span>mp" <-- note this
]
}
},
{
"_index": "64996939",
"_type": "_doc",
"_id": "3",
"_score": 0.62883455,
"_source": {
"name": "keeper",
"Age": 24,
"lastname": "Jr",
"firstname": "gomez"
},
"highlight": {
"name": [
"<span class='bold'>ke</span>eper" <--note this
]
}
}
]
I have the following API defined in my config spec:
"paths": {
"/handler/v1/fetch/{mode}": {
"post": {
"operationId": "ApihandlerFetch",
"parameters": [
{
"name": "mode",
"in": "path",
"required": true,
"type": "string"
},
{
"name": "y",
"in": "query",
"required": false,
"type": "integer",
"format": "int32"
},
{
"name": "m",
"in": "query",
"required": false,
"type": "integer",
"format": "int32"
},
{
"name": "d",
"in": "query",
"required": false,
"type": "integer",
"format": "int32"
}
],
The following curl requests fails with the error message "global.badrequest"
curl -d '{}' "https://xxxxx.appspot.com/_ah/api/handler/v1/fetch/latest"
"error": {
"errors": [
{
"domain": "global",
"reason": "badRequest",
"message": "java.lang.IllegalArgumentException"
}
],
"code": 400,
"message": "java.lang.IllegalArgumentException"
}
}
It however works when the other query parameters are defined with a value like so:
curl -d '' "https://xxxx.appspot.com/_ah/api/handler/v1/fetch/date?y=2017&m=11&d=27"
Did I misunderstood the whole idea of a Query Parameter in Google App Engine Endpoints framework v2, and that the parameters should be part of the URL regardless if they have a value or not (ie: http://...?y=&m=&d).
Below is my example search response with 4 results retrieved.
{
"took": 13,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 0.41753215,
"hits": [
{
"_index": "google_a804f89b-d32e-426a-a79a-ea83d65c98ea",
"_type": "viz_dashlet",
"_id": "/shared/Report_google_Shared",
"_score": 0.41753215,
"fields": {
"lastModified": [
1461738428007
],
"dir": [
"/shared"
],
"filename": [
"Report_google_Shared"
]
},
"highlight": {
"filename": [
"Report_google_Shared"
]
}
},
{
"_index": "google_a804f89b-d32e-426a-a79a-ea83d65c98ea",
"_type": "viz_dashlet",
"_id": "/shared/Report_Gmail_Shared",
"_score": 0.41753215,
"fields": {
"lastModified": [
1461738618676
],
"dir": [
"/shared"
],
"filename": [
"Report_Gmail_Shared"
]
},
"highlight": {
"filename": [
"Report_Gmail_Shared"
]
}
},
{
"_index": "google_a804f89b-d32e-426a-a79a-ea83d65c98ea",
"_type": "viz_dashlet",
"_id": "/private/hitesh/Report_Gmail_Private",
"_score": 0.1883173,
"fields": {
"lastModified": [
1461738629888
],
"dir": [
"/private/hitesh"
],
"filename": [
"Report_Gmail_Private"
]
},
"highlight": {
"filename": [
"Report_Gmail_Private"
]
}
},
{
"_index": "google_a804f89b-d32e-426a-a79a-ea83d65c98ea",
"_type": "viz_dashlet",
"_id": "/private/dholaria/Report_google_Private",
"_score": 0.1883173,
"fields": {
"lastModified": [
1461738451720
],
"dir": [
"/private/dholaria"
],
"filename": [
"Report_google_Private"
]
},
"highlight": {
"filename": [
"Report_google_Private"
]
}
}
]
}
}
Now, I want to filter the above search results basis the specific "dir" field value as per the below criteria.
Include the search result in the response if and only if:
If "dir" field value equals to either of: "/shared" or "/private/hitesh"
Else if "dir" field value starts with either of: "/shared/" or
"/private/hitesh/"
How can I achieve the above functionality in ElasticSearch?
PS: Below is my example mapping.
{
"google_a804f89b-d32e-426a-a79a-ea83d65c98ea": {
"mappings": {
"viz_dashlet": {
"properties": {
"charts": {
"type": "string",
"index_analyzer": "report_index_analyzer",
"search_analyzer": "report_search_analyzer",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"columnLabels": {
"type": "string",
"index_analyzer": "report_index_analyzer",
"search_analyzer": "report_search_analyzer",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"columnNames": {
"type": "string",
"index_analyzer": "report_index_analyzer",
"search_analyzer": "report_search_analyzer",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"creator": {
"type": "string",
"index_analyzer": "report_index_analyzer",
"search_analyzer": "report_search_analyzer",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"dimensions": {
"type": "string",
"index_analyzer": "report_index_analyzer",
"search_analyzer": "report_search_analyzer",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"dir": {
"type": "string",
"index_analyzer": "report_index_analyzer",
"search_analyzer": "report_search_analyzer",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"expressions": {
"type": "string",
"index_analyzer": "report_index_analyzer",
"search_analyzer": "report_search_analyzer",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"filename": {
"type": "string",
"index_analyzer": "whitespace_index",
"search_analyzer": "whitespace_search",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"lastModified": {
"type": "date",
"format": "date_hour_minute_second"
},
"measures": {
"type": "string",
"index_analyzer": "report_index_analyzer",
"search_analyzer": "report_search_analyzer",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"promptFilters": {
"type": "string",
"index_analyzer": "report_index_analyzer",
"search_analyzer": "report_search_analyzer",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}
}
Try this query:
{
"query": {
"bool": {
"should": [
{
"term": {
"dir.raw": {
"value": "/shared"
}
}
},
{
"term": {
"dir.raw": {
"value": "/private/hitesh"
}
}
},
{
"match_phrase_prefix": {
"dir.raw": "/shared"
}
},
{
"match_phrase_prefix": {
"dir.raw": "/private/hitesh"
}
}
]
}
}
}