Elasticsearch sorting - not getting expected results - java

I have data in elastic field name as "Amit 111", "amit 111", "Amit 222".
I am trying to sort it using:
searchSourceBuilder.query(query).sort("name.keyword", SortOrder.ASC)
It returns result as:
"Amit 111", "Amit 222", "amit 111"
But I want results as :
"Amit 111", "amit 111", "Amit 222"
Please help.

Another approach is to use the fielddata as on text field you can apply the sort, more details on the linked URL.
Java code for that you need to change the index mapping as shown after java code.
searchSourceBuilder.query(query).sort("name", SortOrder.ASC)
Create index with field data enabled on the name field
{
"mappings": {
"properties": {
"name": {
"type": "text",
"fielddata": true
}
}
}
}
Index example documents
{
"name" : "amit 111"
}
{
"name" : "Amit 111"
}
{
"name" : "Amit 222"
}
Your search query with sort on name field
{
"sort": [
{
"name": "asc"
}
]
}
Result
"hits": [
{
"_index": "key",
"_type": "_doc",
"_id": "1",
"_score": null,
"_source": {
"name": "amit 111"
},
"sort": [
"111"
]
},
{
"_index": "key",
"_type": "_doc",
"_id": "2",
"_score": null,
"_source": {
"name": "Amit 111"
},
"sort": [
"111"
]
},
{
"_index": "key",
"_type": "_doc",
"_id": "3",
"_score": null,
"_source": {
"name": "Amit 222"
},
"sort": [
"222"
]
}
]

keyword fields are stored as it is so sorting on keyword fields is case sensitive.Normalizer with lowercase filter can be used to index keyword fields.
The normalizer property of keyword fields is similar to analyzer
except that it guarantees that the analysis chain produces a single
token.
Mapping:
{
"settings": {
"analysis": {
"normalizer": {
"my_normalizer": {
"type": "custom",
"filter": [
"lowercase"
]
}
}
}
},
"mappings": {
"properties": {
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"normalizer": "my_normalizer"
}
}
}
}
}
}
Query: Both sort on name.keyword and term query on name.keyword will be case insensitive
{
"query": {
"match_all": {}
},
"sort": [
{
"name.keyword": {
"order": "asc"
}
}
]
}
Result:"
"hits" : [
{
"_index" : "index84",
"_type" : "_doc",
"_id" : "SBvLT3IB8mx5yKbJQ7EC",
"_score" : null,
"_source" : {
"name" : "Amit 111"
},
"sort" : [
"amit 111"
]
},
{
"_index" : "index84",
"_type" : "_doc",
"_id" : "SRvLT3IB8mx5yKbJULFl",
"_score" : null,
"_source" : {
"name" : "amit 111"
},
"sort" : [
"amit 111"
]
},
{
"_index" : "index84",
"_type" : "_doc",
"_id" : "ShvLT3IB8mx5yKbJaLFg",
"_score" : null,
"_source" : {
"name" : "Amit 222"
},
"sort" : [
"amit 222"
]
}
]

Related

Optional Properties json schema

the items are only required for the order_type: ORDER, but I can't make a conditional that only requires this property for that specific case, that is, if any order_type different from ORDER comes without items it can be validated correctly, but if an ORDER comes without them I mark it incorrectly.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://json-schema.org/draft-07/schema#",
"title": "Validaciones sobre el esquema Order",
"type": "object",
"properties": {
"warehouse_id": {
"type": [
"string"
]
},
"operation_type": {
"type": [
"string"
]
},
"order_id": {
"type": [
"number"
]
},
"items": {
"type": [
"array"
],
"minItems": 1,
"items": {
"type": [
"object"
],
"properties": {
"sku": {
"type": [
"string"
],
"minLength": 1,
"maxLength": 50
},
"quantity": {
"type": [
"integer"
],
"minimum": 1
}
},
"required": [
"sku",
"quantity"
],
"additionalProperties": false
}
},
"attributes": {
"type": [
"object"
],
"properties": {
"order_type": {
"type": [
"string"
],
"enum": [
"ORDER",
"TRANSFER",
"WITHDRAWAL",
"DISPOSAL",
"FRESH"
]
},
"etd": {
"type": [
"string"
],
"pattern": "^(\\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])(?:T)(0[0-9]|1[0-9]|2[0-3]):(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]):(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])(?:Z)$"
},
"volume": {
"type": [
"integer", "null"
],
"minimum": 0
},
"typology": true
},
"required": [
"order_type"
],
"allOf": [
{
"if": {
"properties": {
"order_type": {
"const": "ORDER"
}
},
"required": [
"order_type",
"items"
]
},
"then": {
"properties": {
"order_type": true,
"etd": true,
"volume": true,
"typology": {
"type": [
"string", "null"
],
"enum": [
"CONVEYABLE",
"NON_CONVEYABLE"
]
}
},
"required": [
"order_type",
"etd"
],
"additionalProperties": false
}
},
{
"if": {
"properties": {
"order_type": {
"const": "TRANSFER"
}
},
"required": [
"order_type"
]
},
"then": {
"properties": {
"order_type": true,
"etd": true,
"volume": true
},
"required": [
"order_type",
"etd"
],
"additionalProperties": false
}
},
{
"if": {
"properties": {
"order_type": {
"const": "WITHDRAWAL"
}
},
"required": [
"order_type"
]
},
"then": {
"properties": {
"order_type": true,
"etd": true,
"volume": true
},
"required": [
"order_type",
"etd"
],
"additionalProperties": false
}
},
{
"if": {
"properties": {
"order_type": {
"const": "DISPOSAL"
}
},
"required": [
"order_type"
]
},
"then": {
"properties": {
"order_type": true,
"etd": true,
"volume": true
},
"required": [
"order_type",
"etd"
],
"additionalProperties": false
}
}
]
}
},
"required": [
"warehouse_id",
"operation_type",
"attributes"
],
"additionalProperties": false
}
try to make a conditional with an if at the end of the properties like this
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://json-schema.org/draft-07/schema#",
"title": "Validaciones sobre el esquema Order",
"type": "object",
"properties": {
"warehouse_id": {
"type": [
"string"
]
},
"operation_type": {
"type": [
"string"
]
},
"order_id": {
"type": [
"number"
]
},
"items": {
"type": [
"array"
],
"minItems": 1,
"items": {
"type": [
"object"
],
"properties": {
"sku": {
"type": [
"string"
],
"minLength": 1,
"maxLength": 50
},
"quantity": {
"type": [
"integer"
],
"minimum": 1
}
},
"required": [
"sku",
"quantity"
],
"additionalProperties": false
}
},
"attributes": {
"type": [
"object"
],
"properties": {
"order_type": {
"type": [
"string"
],
"enum": [
"ORDER",
"TRANSFER",
"WITHDRAWAL",
"DISPOSAL",
"FRESH"
]
},
"etd": {
"type": [
"string"
],
"pattern": "^(\\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])(?:T)(0[0-9]|1[0-9]|2[0-3]):(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]):(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])(?:Z)$"
},
"volume": {
"type": [
"integer", "null"
],
"minimum": 0
},
"typology": true
},
"required": [
"order_type"
],
"allOf": [
{
"if": {
"properties": {
"order_type": {
"const": "ORDER"
}
},
"required": [
"order_type",
"items"
]
},
"then": {
"properties": {
"order_type": true,
"etd": true,
"volume": true,
"typology": {
"type": [
"string", "null"
],
"enum": [
"CONVEYABLE",
"NON_CONVEYABLE"
]
}
},
"required": [
"order_type",
"etd"
],
"additionalProperties": false
}
},
{
"if": {
"properties": {
"order_type": {
"const": "TRANSFER"
}
},
"required": [
"order_type"
]
},
"then": {
"properties": {
"order_type": true,
"etd": true,
"volume": true
},
"required": [
"order_type",
"etd"
],
"additionalProperties": false
}
},
{
"if": {
"properties": {
"order_type": {
"const": "WITHDRAWAL"
}
},
"required": [
"order_type"
]
},
"then": {
"properties": {
"order_type": true,
"etd": true,
"volume": true
},
"required": [
"order_type",
"etd"
],
"additionalProperties": false
}
},
{
"if": {
"properties": {
"order_type": {
"const": "DISPOSAL"
}
},
"required": [
"order_type"
]
},
"then": {
"properties": {
"order_type": true,
"etd": true,
"volume": true
},
"required": [
"order_type",
"etd"
],
"additionalProperties": false
}
}
]
}
},
"allOf": [
{
"if": {
"properties": {
"order_type": {
"const": "ORDER"
}
},
"required": [
"order_type"
]
},
"then": {
"properties": {
"items": true,
"order_type": true,
"etd": true,
"volume": true
},
"required": [
"order_type",
"etd",
"items"
],
"additionalProperties": false
}
}
],
"required": [
"warehouse_id",
"operation_type",
"attributes"
],
"additionalProperties": false
}
this request should be marked as incorrect, cause dont have any items, that are required:
{
"warehouse_id": "ARTW01",
"operation_type": "outbound",
"order_id": 41789301078,
"attributes": {
"volume": 1350,
"etd": "2022-11-11T18:25:00Z",
"order_type": "ORDER"
}
}
Here's the assertion you described with all the unrelated parts removed. The trick is that the if need to describe the nested path to the value being checked.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"attributes": {
"type": "object",
"properties": {
"order_type": { "enum": ["ORDER", "TRANSFER", "WITHDRAWAL", "DISPOSAL", "FRESH"] }
},
"required": ["order_type"]
},
"items": { "type": "array" }
},
"allOf": [
{
"if": {
"type": "object",
"properties": {
"attributes": {
"type": "object",
"properties": {
"order_type": { "const": "ORDER" }
},
"required": ["order_type"]
}
},
"required": ["attributes"]
},
"then": { "required": ["items"] }
}
]
}

Recursively print all json elements and nested elements using gson

My program currently gets a response from an api which returns an extensive json script, usually around 400 lines or more. Within this JSON there are well over 20 elements, of which only about 2 are needed for other functions, the rest just needs to printed.
I have tried to make classes for each element but it became excessive, especially when the elements had different variations, and so i was told a recursive approach would be better suited. My current attempt involves using gson, although I find myself using instanceof in order to determine whether the next json element can be mapped using gson.
My JSON script looks as follows:
{
"id": "fox",
"metadata": {
"operation": "retrieve",
"provider": "Oxford University Press",
"schema": "RetrieveEntry"
},
"results": [
{
"id": "fox",
"language": "en-gb",
"lexicalEntries": [
{
"derivatives": [
{
"id": "foxlike",
"text": "foxlike"
}
],
"entries": [
{
"etymologies": [
"Old English, of Germanic origin; related to Dutch vos and German Fuchs"
],
"pronunciations": [
{
"audioFile": "https://audio.oxforddictionaries.com/en/mp3/fox_gb_1.mp3",
"dialects": [
"British English"
],
"phoneticNotation": "IPA",
"phoneticSpelling": "fɒks"
}
],
"senses": [
{
"definitions": [
"a carnivorous mammal of the dog family with a pointed muzzle and bushy tail, proverbial for its cunning."
],
"domainClasses": [
{
"id": "mammal",
"text": "Mammal"
}
],
"id": "m_en_gbus0384950.006",
"notes": [
{
"text": "Vulpes and three other genera, family Canidae: several species, including the red fox and the arctic fox",
"type": "technicalNote"
}
],
"semanticClasses": [
{
"id": "canid",
"text": "Canid"
}
],
"shortDefinitions": [
"carnivorous mammal"
],
"subsenses": [
{
"definitions": [
"the fur of a fox."
],
"domainClasses": [
{
"id": "mammal",
"text": "Mammal"
}
],
"id": "m_en_gbus0384950.010",
"notes": [
{
"text": "mass noun",
"type": "grammaticalNote"
}
],
"semanticClasses": [
{
"id": "fur",
"text": "Fur"
}
],
"shortDefinitions": [
"fur of fox"
]
}
],
"synonyms": [
{
"language": "en",
"text": "Reynard"
}
],
"thesaurusLinks": [
{
"entry_id": "fox",
"sense_id": "t_en_gb0006011.001"
}
]
},
{
"definitions": [
"a cunning or sly person"
],
"examples": [
{
"text": "a wily old fox"
}
],
"id": "m_en_gbus0384950.013",
"semanticClasses": [
{
"id": "disliked_person",
"text": "Disliked_Person"
}
],
"shortDefinitions": [
"cunning or sly person"
]
},
{
"definitions": [
"a sexually attractive woman."
],
"id": "m_en_gbus0384950.015",
"regions": [
{
"id": "north_american",
"text": "North_American"
}
],
"registers": [
{
"id": "informal",
"text": "Informal"
}
],
"semanticClasses": [
{
"id": "attractive_female",
"text": "Attractive_Female"
}
],
"shortDefinitions": [
"sexually attractive woman"
]
}
]
}
],
"language": "en-gb",
"lexicalCategory": {
"id": "noun",
"text": "Noun"
},
"text": "fox"
},
{
"derivatives": [
{
"id": "foxlike",
"text": "foxlike"
}
],
"entries": [
{
"grammaticalFeatures": [
{
"id": "transitive",
"text": "Transitive",
"type": "Subcategorization"
}
],
"pronunciations": [
{
"audioFile": "https://audio.oxforddictionaries.com/en/mp3/fox_gb_1.mp3",
"dialects": [
"British English"
],
"phoneticNotation": "IPA",
"phoneticSpelling": "fɒks"
}
],
"senses": [
{
"definitions": [
"baffle or deceive (someone)"
],
"examples": [
{
"text": "the abbreviation foxed me completely"
}
],
"id": "m_en_gbus0384950.017",
"registers": [
{
"id": "informal",
"text": "Informal"
}
],
"shortDefinitions": [
"baffle or deceive"
],
"subsenses": [
{
"definitions": [
"behave in a cunning or sly way"
],
"examples": [
{
"text": "to his mind everybody was dodging and foxing"
}
],
"id": "m_en_gbus0384950.023",
"notes": [
{
"text": "no object",
"type": "grammaticalNote"
}
],
"registers": [
{
"id": "dated",
"text": "Dated"
}
],
"shortDefinitions": [
"behave in cunning or sly way"
]
}
],
"synonyms": [
{
"language": "en",
"text": "baffle"
},
{
"language": "en",
"text": "bewilder"
},
{
"language": "en",
"text": "mystify"
},
{
"language": "en",
"text": "bemuse"
},
{
"language": "en",
"text": "perplex"
},
{
"language": "en",
"text": "puzzle"
},
{
"language": "en",
"text": "confuse"
},
{
"language": "en",
"text": "confound"
},
{
"language": "en",
"text": "nonplus"
},
{
"language": "en",
"text": "disconcert"
},
{
"language": "en",
"text": "throw"
},
{
"language": "en",
"text": "throw off balance"
},
{
"language": "en",
"text": "disorientate"
},
{
"language": "en",
"text": "take aback"
},
{
"language": "en",
"text": "set thinking"
}
],
"thesaurusLinks": [
{
"entry_id": "flummox",
"sense_id": "t_en_gb0005828.001"
}
]
}
]
}
],
"language": "en-gb",
"lexicalCategory": {
"id": "verb",
"text": "Verb"
},
"text": "fox"
}
],
"type": "headword",
"word": "fox"
},
{
"id": "Fox",
"language": "en-gb",
"lexicalEntries": [
{
"entries": [
{
"inflections": [
{
"grammaticalFeatures": [
{
"id": "plural",
"text": "Plural",
"type": "Number"
}
],
"inflectedForm": "Fox"
}
],
"pronunciations": [
{
"audioFile": "https://audio.oxforddictionaries.com/en/mp3/fox_gb_1.mp3",
"dialects": [
"British English"
],
"phoneticNotation": "IPA",
"phoneticSpelling": "fɒks"
}
],
"senses": [
{
"definitions": [
"a member of a North American people formerly living in southern Wisconsin, and now mainly in Iowa, Nebraska, and Kansas."
],
"domainClasses": [
{
"id": "people",
"text": "People"
},
{
"id": "amerindian",
"text": "Amerindian"
}
],
"id": "m_en_gbus0384960.006",
"semanticClasses": [
{
"id": "amerindian",
"text": "Amerindian"
}
],
"shortDefinitions": [
"member of North American people"
]
},
{
"definitions": [
"the Algonquian language of the Fox, now almost extinct."
],
"domainClasses": [
{
"id": "language",
"text": "Language"
},
{
"id": "amerindian",
"text": "Amerindian"
}
],
"id": "m_en_gbus0384960.010",
"notes": [
{
"text": "mass noun",
"type": "grammaticalNote"
}
],
"semanticClasses": [
{
"id": "amerindian_language",
"text": "Amerindian_Language"
}
],
"shortDefinitions": [
"Algonquian language of Fox, now extinct"
]
}
]
}
],
"language": "en-gb",
"lexicalCategory": {
"id": "noun",
"text": "Noun"
},
"text": "Fox"
},
{
"entries": [
{
"pronunciations": [
{
"audioFile": "https://audio.oxforddictionaries.com/en/mp3/fox_gb_1.mp3",
"dialects": [
"British English"
],
"phoneticNotation": "IPA",
"phoneticSpelling": "fɒks"
}
],
"senses": [
{
"definitions": [
"relating to the Fox or their language."
],
"domainClasses": [
{
"id": "amerindian",
"text": "Amerindian"
}
],
"id": "m_en_gbus0384960.013",
"shortDefinitions": [
"relating to Fox or their language"
]
}
]
}
],
"language": "en-gb",
"lexicalCategory": {
"id": "adjective",
"text": "Adjective"
},
"text": "Fox"
}
],
"type": "headword",
"word": "Fox"
}
],
"word": "fox"
}
If there are any edits I should make to my question please let me know, this is my first question here. Thanks for any help.
EDIT:
The output needs to be formatted neatly, so basically it must be line separated and should be displayed in an easy to read manner, but as well as this, some elements, such as synonyms, need to have their words hyperlinked so they must be identified separately. Hope this helps

Elastic Search query Not workng with java but with elastic URI

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

Looking for highlight search where it will return all matching fields from one record

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
]
}
}
]

How to filter ElasticSearch results basis the field value?

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"
}
}
]
}
}
}

Categories

Resources