Ranking results based on field in nested array - elasticsearch

Suppose I have an elasticsearch index called 'neighborhood' and in there I am storing documents ( example far below) with the following settings and mappings.
There are many nested fields in the model and so I am using copy_to on many of them to make searching easier - e.g. I just do a match query on the _nayberz field.
The search works quite well, however I would like for matches where the ratedTags[n].rating (it can be 1-5) is higher to rank higher
Mappings:
{
"properties": {
"_nayberz": {
"type": "text",
"analyzer": "autocomplete",
"store" : true
},
"describedTagCombos": {
"type": "nested",
"properties": {
"tags": {
"type": "nested",
"properties": {
"keywords": {
"type": "text",
"analyzer": "autocomplete",
"boost": 5,
"copy_to": "_nayberz"
},
"name": {
"type": "text",
"analyzer": "autocomplete",
"boost": 5,
"copy_to": "_nayberz"
},
"synonym": {
"type": "text",
"analyzer": "autocomplete",
"boost": 5,
"copy_to": "_nayberz"
}
}
}
}
},
"name": {
"type": "text",
"fielddata" : true,
"copy_to": "_nayberz",
"boost": 1
},
"ratedTags": {
"type": "nested",
"properties": {
"tags": {
"type": "nested",
"properties": {
"keywords": {
"type": "text",
"boost": 5,
"copy_to": "_nayberz"
},
"name": {
"type": "text",
"boost": 5,
"copy_to": "_nayberz"
},
"synonym": {
"type": "text",
"boost": 5,
"copy_to": "_nayberz"
}
}
}
}
}
}
}
{
"id": 6475,
"neighborhoodId": 2495,
"name": "Some neighborhood name",
"xMin": -87.7351229157221,
"xMax": -87.687849915678,
"xAvg": -87.7114864157001,
"yMin": 41.9316410223988,
"yMax": 41.9466920224128,
"yAvg": 41.9391665224058,
"city": {
"id": 539,
"name": "SomeCity",
"county": "SomeCounty",
"state": {
"id": 174,
"name": "Illinois",
"abbreviation": "IL",
"country": "USA"
}
},
"ratedTags": [
{
"id": 11572,
"rating": 2,
"tags": [
{
"id": 2323,
"tagId": 36,
"name": "shopping options",
"synonym": "",
"keywords": "shopping options, shopping, shop, shopper, shoppers, shops"
}
]
},
{
"id": 11418,
"rating": 3,
"tags": [
{
"id": 2292,
"tagId": 5,
"name": "public transportation options",
"synonym": "",
"keywords": "public transport, public transportation"
}
]
},
{
"id": 11434,
"rating": 4,
"tags": [
{
"id": 2295,
"tagId": 8,
"name": "quiet",
"synonym": "",
"keywords": "quiet, chill, peaceful, not noisy, not loud, not too noisy, not too loud, relaxed"
}
]
},
{
"id": 11458,
"rating": 3,
"tags": [
{
"id": 2300,
"tagId": 13,
"name": "expensive relative to other neighborhoods",
"synonym": "costly",
"keywords": "upscale, chic"
}
]
},
{
"id": 11469,
"rating": 4,
"tags": [
{
"id": 2302,
"tagId": 15,
"name": "restaurants",
"synonym": "",
"keywords": "restaurants, restaurant, eatery, eateries"
}
]
},
{
"id": 11477,
"rating": 2,
"tags": [
{
"id": 2304,
"tagId": 17,
"name": "clean",
"synonym": "",
"keywords": "clean, cleanest, not dirty"
}
]
},
{
"id": 11603,
"rating": 3,
"tags": [
{
"id": 2329,
"tagId": 42,
"name": "safe compared to other neighborhoods",
"synonym": "",
"keywords": "safe, safety, safest, not dangerous"
}
]
},
{
"id": 11557,
"rating": 2,
"tags": [
{
"id": 2320,
"tagId": 33,
"name": "green space and parks",
"synonym": "",
"keywords": "green space, green, parks, open space, nature"
}
]
},
{
"id": 11577,
"rating": 2,
"tags": [
{
"id": 2324,
"tagId": 37,
"name": "tourist attractions",
"synonym": "sightseeing options",
"keywords": "tourist attractions, sightseeing, attractions, sightsee, tourist"
}
]
},
{
"id": 11582,
"rating": 2,
"tags": [
{
"id": 2325,
"tagId": 38,
"name": "entertainment options",
"synonym": "fun options",
"keywords": "fun, entertainment, good time, enjoyment, enjoy, pleasure"
}
]
},
{
"id": 11588,
"rating": 3,
"tags": [
{
"id": 2326,
"tagId": 39,
"name": "cafes",
"synonym": "coffee shops",
"keywords": "cafes, coffee shops, cafe, coffee shop, coffee houses, coffee house, café, cafés"
}
]
},
{
"id": 11594,
"rating": 4,
"tags": [
{
"id": 2327,
"tagId": 40,
"name": "dining options",
"synonym": "",
"keywords": "dining places, dining spots, dining options, restaurants, food options, food places, places to eat, eateries, dining, fast food, chain restaurant, chain restaurants, eatery"
}
]
}
],
"describedTagCombos": [
{
"id": 26842,
"descriptor": "Foodies: must try a polish sausage in this neighborhood",
"tags": [
{
"id": 2302,
"tagId": 15,
"name": "restaurants",
"synonym": "",
"keywords": "restaurants, restaurant, eatery, eateries"
},
{
"id": 2327,
"tagId": 40,
"name": "dining options",
"synonym": "",
"keywords": "dining places, dining spots, dining options, restaurants, food options, food places, places to eat, eateries, dining, fast food, chain restaurant, chain restaurants, eatery"
}
]
},
{
"id": 26843,
"descriptor": "Addison Mall in area with a Target, Starbucks, etc.",
"tags": [
{
"id": 2323,
"tagId": 36,
"name": "shopping options",
"synonym": "",
"keywords": "shopping options, shopping, shop, shopper, shoppers, shops"
}
]
}
]
}
Settings:
{
"analysis": {
"filter": {
"autocomplete_filter": {
"type": "edge_ngram",
"min_gram": 3,
"max_gram": 20
},
"english_stop": {
"type": "stop",
"stopwords": [
"a",
"an",
"and",
"are",
"as",
"at",
"be",
"but",
"by",
"for",
"if",
"in",
"into",
"is",
"it",
"no",
"not",
"of",
"on",
"or",
"such",
"that",
"the",
"their",
"then",
"there",
"these",
"they",
"this",
"to",
"was",
"will",
"with",
"have"
]
}
},
"analyzer": {
"autocomplete": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"autocomplete_filter",
"english_stop"
]
}
}
}
}

I think the best candidate here is the field_value_factor function:
GET neighborhoods/_search
{
"query": {
"bool": {
"should": [
{
"query_string": {
"default_field": "_nayberz",
"query": "enjoy"
}
},
{
"nested": {
"path": "ratedTags",
"query": {
"function_score": {
"functions": [
{
"field_value_factor": { <--
"field": "ratedTags.rating",
"factor": 1
}
}
],
"boost_mode": "sum"
}
}
}
}
]
}
}
}

Related

Is there a way to figure out the right dimensions for images to be used on Adaptive cards for a MS teams app?

We are trying to create an bot with a use of Adaptive cards on MS-Teams. The preview using adaptive card designer gives the proper image as required. But when used in the Teams app, the image looks totally distorted.
The preview image as in designer:
The image output as in the MS teams app:
Below is the Json used for the adaptive cards:
{
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"text": "Howdy!",
"weight": "Bolder",
"color": "Accent",
"size": "Large"
},
{
"type": "TextBlock",
"text": "How are you doing today? ",
"wrap": true
},
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"width": "stretch",
"items": [
{
"type": "Image",
"url": "https://howdy1.blob.core.windows.net/sample/2-removebg-preview.png",
"selectAction": {
"type": "Action.Submit",
"data": {
"rating": "awful",
"comment": ""
},
"title": "awful"
},
"size": "Large",
"width": "700px",
"height": "60px"
}
]
},
{
"type": "Column",
"width": "stretch",
"items": [
{
"type": "Image",
"url": "https://howdy1.blob.core.windows.net/sample/3-removebg-preview.png",
"selectAction": {
"type": "Action.Submit",
"data": {
"rating": "bad",
"comment": ""
},
"title": "bad"
},
"size": "Large",
"width": "700px",
"height": "60px",
"spacing": "None"
}
]
},
{
"type": "Column",
"width": "stretch",
"items": [
{
"type": "Image",
"url": "https://howdy1.blob.core.windows.net/sample/1-removebg-preview.png",
"selectAction": {
"type": "Action.Submit",
"data": {
"rating": "ok",
"comment": ""
},
"title": "ok"
},
"size": "Large",
"width": "700px",
"height": "60px"
}
]
},
{
"type": "Column",
"width": "stretch",
"items": [
{
"type": "Image",
"url": "https://howdy1.blob.core.windows.net/sample/5-removebg-preview.png",
"selectAction": {
"type": "Action.Submit",
"data": {
"rating": "good",
"comment": ""
},
"title": "good"
},
"size": "Large",
"width": "700px",
"height": "60px"
}
]
},
{
"type": "Column",
"width": "stretch",
"items": [
{
"type": "Image",
"url": "https://howdy1.blob.core.windows.net/sample/4-removebg-preview.png",
"selectAction": {
"type": "Action.Submit",
"data": {
"rating": "terrific",
"comment": ""
},
"title": "terrific"
},
"size": "Large",
"width": "700px",
"height": "60px"
}
]
}
]
},
{
"type": "Input.Text",
"placeholder": "Add a comment",
"isMultiline": true,
"id": "comment"
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.3",
"actions": [
{
"type": "Action.Submit",
"title": "OK",
"data": {
"rating": "",
"comment": ""
}
}
]}
Thanks in advance!!
Moving comment to answer :
This issue has been fixed, We have checked and its working fine. Attaching a piece of code for emoji height and width. Please keep the width inline with height.
{
"type": "AdaptiveCard",
"body": [
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"width": "stretch",
"items": [
{
"type": "Image",
"url": "https://howdy1.blob.core.windows.net/sample/2-removebg-preview.png",
"selectAction": {
"type": "Action.Submit",
"data": {
"rating": "awful",
"comment": ""
},
"title": "awful"
},
"size": "Large",
"width": "70px",
"height": "60px"
}
]
}
]
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.3",
}

Complex nested arrays search with boosting

So I have an elastic index with a few thousand docuemnts each with the following rather complex structure which I need to search as follows:
lets say I have the following search string : "I am single and have a dog", I need to be able to
search all doc.ratedTags.tags.keywords and all doc.describedTagCombos.tags.keywords, but giving a boost to matched doc.ratedTags.tags.keywords where its rating sibling has a higher value
tbh i don't think its even possible
{
"_index": "neighborhood",
"_type": "neighborhood",
"_id": "2338",
"_version": 1,
"_seq_no": 0,
"_primary_term": 1,
"found": true,
"_source": {
"id": 2338,
"neighborhoodId": 4427,
"name": "East Village",
"xMin": -73.9926238051035,
"xMax": -73.9718363715137,
"xAvg": -73.9822300883086,
"yMin": 40.7192801452175,
"yMax": 40.7337200243512,
"yAvg": 40.7265000847843,
"city": {
"id": 393,
"name": "New York City-Manhattan",
"county": "New York",
"state": {
"id": 118,
"name": "New York",
"abbreviation": "NY",
"country": "USA"
}
},
"ratedTags": [
{
"id": 1064,
"rating": 4,
"tags": [
{
"id": 213,
"tagId": 15,
"name": "restaurants",
"synonym": "",
"keywords": "restaurants, restaurant, eatery, eateries"
}
]
},
{
"id": 995,
"rating": 5,
"tags": [
{
"id": 199,
"tagId": 1,
"name": "artists and creative types",
"synonym": "",
"keywords": "artsy, artistic, artists, creative types, musicians, artist, creative type, musician"
}
]
}
],
"describedTagCombos": [
{
"id": 9524,
"descriptor": "Area has tons of excellent bars, restaurants, and fun that appeals to post-college, students, and creative types",
"tags": [
{
"id": 213,
"tagId": 15,
"name": "restaurants",
"synonym": "",
"keywords": "restaurants, restaurant, eatery, eateries"
},
{
"id": 219,
"tagId": 21,
"name": "nightlife",
"synonym": "",
"keywords": "nightlife, bars, nightclubs, lounge, lounges, nightclub, bar, clubs, club, party, parties, night, pubs, tavern, sports bar, taverns, pub, wine bar, night life, night club, night clubs, sportsbar, sportsbars, night light, night lights, nightlight, nite light"
},
{
"id": 226,
"tagId": 28,
"name": "bar scene",
"synonym": "",
"keywords": "bar scene, bars, lounges, wine bar, lounge, pub, pubs, tavern, taverns"
},
{
"id": 238,
"tagId": 40,
"name": "dining options",
"synonym": "",
"keywords": "dining places, dining spots, dining options, restaurants, food options, food places, places to eat, eateries, dining, fast food, chain restaurant, chain restaurants, eatery"
}
]
},
{
"id": 9525,
"descriptor": "Traveler Tip: walk around St Marks and check out the shops, people, & vibe",
"tags": [
{
"id": 235,
"tagId": 37,
"name": "tourist attractions",
"synonym": "sightseeing options",
"keywords": "tourist attractions, sightseeing, attractions, sightsee, tourist"
},
{
"id": 236,
"tagId": 38,
"name": "entertainment options",
"synonym": "fun options",
"keywords": "fun, entertainment, good time, enjoyment, enjoy, pleasure"
}
]
}
]
}
}
"_nayberz": {
"type": "text",
"analyzer": "autocomplete",
"store": true
},
...
"keywords": {
"type": "text",
"analyzer": "autocomplete",
"copy_to": "_nayberz"
},

Jolt transformation for spatial data (from geojson to arcGIS json)

I'm new to Jolt transformation and am trying to write a spec for spatial data. I'm unable to get the
expected output especially with the data for the coordinates. Any help would greatly be appreciated.
Below is the input I get:
{
"type": "Feature Collection",
"c rs": {
"type": "name",
"properties": {
"name": "4326"
}
},
"features": [
{
"type": "Feature",
"id": 1,
"geometry": {
"type": "Point",
"coordinates": [
-122.682207049,
45.526159497
]
},
"properties": {
"F ID": 1,
"Place Name": "Boyd Coffee",
"Place Address": "404 NW 11th Ave Portland Oregon",
"Score": 100,
"latitude": 45.526159497,
"longitude": -122.682207049
}
},
{
"type": "Feature",
"id": 2,
"geometry": {
"type": "Point",
"coordinates": [
-122.677518466,
45.525246078
]
},
"properties": {
"F ID": 2,
"Place Name": "John's Coffee Shop",
"Place Address": "301 NW Broadway St Portland Oregon",
"Score": 100,
"lat": 45.525246078,
"l on": -122.677518466
}
},
{
"type": "Feature",
"id": 3,
"geometry": {
"type": "Point",
"coordinates": [
-122.68287749,
45.526496967
]
},
"properties": {
"F ID": 3,
"Place Name": "Starbucks",
"Place Address": "1134 NW St Portland Oregon",
"Score": 100,
"latitude": 45.526496967,
"longitude": -122.68287749
}
}
]
}
The expected output is -
"features": [
{
"attributes": {
"F ID": 1,
"Place Name": "Boyd Coffee",
"Place Address": "404 NW 11th Ave Portland Oregon",
"Score": 100,
"latitude": 45.526159497,
"longitude": -122.682207049
},
"geometry": {
"x": -122.68220704900002,
"y": 45.526159496999998
}
},
{
"attributes": {
"FID": 2,
"Place Name": "John's Coffee Shop",
"Place Address": "301 NW Broadway St Portland Oregon",
"Score": 100,
"latitude": 45.525246078,
"longitude": -122.677518466
},
"geometry": {
"x": -122.677518466,
"y": 45.52524607799999
}
}
]
I've come up with spec as below, and not getting the expected out. Can anyone help please?
The spec is:
[
{
"operation": "shift",
"spec": {
"features": {
"*": {
"properties": "features[&1].attributes",
"geometry": {
"coordinates": {
"0": "features[&].geometry.x",
"1": "features[&].geometry.y"
}
}
}
}
}
}
]
Change the below in your spec
"0": "features[&].geometry.x", -> "0": "features[&3].geometry.x",
"1": "features[&].geometry.y", -> "1": "features[&3].geometry.y",
[
{
"operation": "shift",
"spec": {
"features": {
"*": {
"properties": "features[&1].attributes",
"geometry": {
"coordinates": {
"0": "features[&3].geometry.x",
"1": "features[&3].geometry.y"
}
}
}
}
}
}
]

BotFramework Adaptive Cards - Container Actions are not Rendered

How can I add actions for containers?
According to the documentation Container type has an "actions" object, but when testing the card it in the adaptive cards visualizer or in the bot-framework emulator no button is displayed.
Attached an example for the kind of card I'm trying to generate.
Thanks for your help.
{
"type": "AdaptiveCard",
"body": [
{
"style":"normal",
"type": "Container",
"separation" : "strong",
"actions": [
{
"type": "Action.OpenUrl",
"url": "http://foo.bar.com",
"title": "adaptivecards1"
}
],
"items": [
{
"type": "ColumnSet",
"separation": "strong",
"columns": [
{
"type": "Column",
"size":1,
"items": [
{
"type": "TextBlock",
"text": "Title",
"size": "large",
"isSubtle": true
},
{
"type": "TextBlock",
"text": "Model: ABC",
"size": "small"
}
]
},
{
"type": "Column",
"size": "1",
"items": [
{
"type": "TextBlock",
"text": " "
},
{
"type": "Image",
"url": "https://path/to/image.jpg",
"size": "large",
"horizontalAlignment" :"right"
}
]
}
]
}
]
},
{
"style":"normal",
"type": "Container",
"separation" : "strong",
"actions": [
{
"type": "Action. OpenUrl",
"url": "http://foo.bar.com",
"title": "adaptivecards2"
}
],
"items": [
{
"type": "ColumnSet",
"separation": "strong",
"columns": [
{
"type": "Column",
"size":1,
"items": [
{
"type": "TextBlock",
"text": "Another Title",
"size": "large",
"isSubtle": true
},
{
"type": "TextBlock",
"text": "Model: XYZ",
"size": "small"
} ]
},
{
"type": "Column",
"size": "1",
"items": [
{
"type": "TextBlock",
"text": " "
},
{
"type": "Image",
"url": "https://path/to/other/image.jpg",
"size": "large",
"horizontalAlignment" :"right"
}
]
}
]
}
]
}
]}
Per this GitHub issue, it seems that there is an error in the documentation and that the actions property doesn't exist on Container.
Instead, you should add an item of type ActionSet to your items array, with the list of actions.
Following your sample, it should look like:
{
"type": "AdaptiveCard",
"body": [
{
"style": "normal",
"type": "Container",
"separation": "strong",
"items": [
{
"type": "ActionSet",
"actions": [
{
"type": "Action.OpenUrl",
"url": "http://foo.bar.com",
"title": "adaptivecards1"
}
]
},
{
"type": "ColumnSet",
"separation": "strong",
"columns": [
{
"type": "Column",
"size": 1,
"items": [
{
"type": "TextBlock",
"text": "Title",
"size": "large",
"isSubtle": true
},
{
"type": "TextBlock",
"text": "Model: ABC",
"size": "small"
}
]
},
{
"type": "Column",
"size": "1",
"items": [
{
"type": "TextBlock",
"text": " "
},
{
"type": "Image",
"url": "https://path/to/image.jpg",
"size": "large",
"horizontalAlignment": "right"
}
]
}
]
}
]
},
{
"style": "normal",
"type": "Container",
"separation": "strong",
"items": [
{
"type": "ActionSet",
"actions": [
{
"type": "Action.OpenUrl",
"url": "http://foo.bar.com",
"title": "adaptivecards2"
}
]
},
{
"type": "ColumnSet",
"separation": "strong",
"columns": [
{
"type": "Column",
"size": 1,
"items": [
{
"type": "TextBlock",
"text": "Another Title",
"size": "large",
"isSubtle": true
},
{
"type": "TextBlock",
"text": "Model: XYZ",
"size": "small"
}
]
},
{
"type": "Column",
"size": "1",
"items": [
{
"type": "TextBlock",
"text": " "
},
{
"type": "Image",
"url": "https://path/to/other/image.jpg",
"size": "large",
"horizontalAlignment": "right"
}
]
}
]
}
]
}
]
}
This is also discussed here.

add score to elasticsearch completion suggester inputs

I need to implement elasticsearch completion suggester.
I have an index mapped like this:
{
"user": {
"properties": {
"username": {
"index": "not_analyzed",
"analyzer": "simple",
"type": "string"
},
"email": {
"index": "not_analyzed",
"analyzer": "simple",
"type": "string"
},
"name": {
"index": "not_analyzed",
"analyzer": "simple",
"type": "string"
},
"name_suggest": {
"payloads": true,
"type": "completion"
}
}
}
}
I add documents to the index like this:
{
"doc": {
"id": 1,
"username": "jack",
"name": "Jack Nicholson",
"email": "nick#myemail.com",
"name_suggest": {
"input": [
"jack",
"Jack Nicholson",
"nick#myemail.com"
],
"payload": {
"id": 1,
"name": "Jack Nicholson",
"username": "jack",
"email": "nick#myemail.com"
},
"output": "Jack Nicholson (jack) - nick#myemail.com"
}
},
"doc_as_upsert": true
}
And I send this request to my_index/_suggest:
{
"user": {
"text": "jack",
"completion": {
"field": "name_suggest"
}
}
}
I get the resulting options that look like this:
[
{
"text": "John Smith",
"score": 1.0,
"payload": {
"id": 11,
"name": "John Smith",
"username": "jack",
"email": "john#myemail.com"
}
},
{
"text": "Jack Nickolson",
"score": 1.0,
"payload": {
"id": 1,
"name": "Jack Nickolson",
"username": "jack.n",
"email": "nickolson#myemail.com"
}
},
{
"text": "Jackson Jermaine",
"score": 1.0,
"payload": {
"id": 10,
"name": "Jackson Jermaine",
"username": "jermaine",
"email": "jermaine#myemail.com"
}
},
{
"text": "Tito Jackson",
"score": 1.0,
"payload": {
"id": 9,
"name": "Tito Jackson",
"username": "tito",
"email": "jackson#myemail.com"
}
},
{
"text": "Michael Jackson",
"score": 1.0,
"payload": {
"id": 6,
"name": "Michael Jackson",
"username": "michael_jackson",
"email": "jackson_michael#myemail.com"
}
}
]
This works fine but, I need to have the options sorted that way that those that have username matched come first. I can do it manually, but that would prevent me to use length and offset and would be slower.
Is it possible to add scoring to the individual inputs (not the whole suggests), and that way affect the sorting? With the approach that I use it seems it is not.
Another related question, is it possible to specify in the input an array of fields instead of an array of values, and that way avoid the duplication? If yes, would setting the score on the fields be taken into account when ES generates suggestions?
You can add score to your input with the weight option.
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters-completion.html#indexing

Resources