Elasticsearch - terms lookup - filter - elasticsearch

I have two indices: users and cars
users contains user_id and car ratings of the user.
ratings objects represent car ratings (by the user)
"user_id": 3,
"ratings": [
{
"score": 10.0,
"car_id": "xxx"
},
{
"score": 50.0,
"car_id": "yyy"
}
]
I'm trying to build a query that fetches cars, rated by user 3 with score higher than 20.
That means, the query would return "yyy" car only (based on the document above) as user 3 has two ratings, but only of them has score greater than 20.
I've managed to build a query that returns all cars rated by a given user.
GET _search
{
"query": {
"bool": {
"filter": [
{
"terms": {
"car_id": {
"index": "users",
"type": "_doc",
"id": "3",
"path": "ratings.car_id"
}
}
}
]
}
}
}
The problem is that I can't figure out how to filter ratings by the ratings.score.
This query is not returning any car even if there are two cars rated by the user 3 with score greater than 20:
GET _search
{
"query": {
"bool": {
"filter": [
{
"terms": {
"car_id": {
"index": "users",
"type": "_doc",
"id": "3",
"path": "ratings.car_id"
}
}
},
{
"range": {
"ratings.score": {
"gte": 20
}
}
}
]
}
}
}
Can you tell me what's wrong and how to make it work?
MAPPINGS
users
{
"mappings": {
"_doc": {
"properties": {
"ratings": {
"type": "nested",
"properties": {
"car_id": {
"type": "text"
},
"score": {
"type": "float"
},
"type": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"user_id": {
"type": "integer"
}
}
}
}
}
cars
{
"mappings": {
"_doc": {
"properties": {
"name": {
"type": "text"
},
"color": {
"type": "boolean"
},
....

The field "ratings" is nested. Recommend use Nested Query.
I couldnt test this query:
"filter": [
{
"terms": {
"car_id": {
"index": "users",
"id": "3",
"path": "user_id"
}
}
},
{
"nested": {
"path": "ratings",
"query": {
"range": {
"ratings.score": {
"gte": 20
}
}
}
}
}
]

Related

Aggregation based off of nested document field with filters on nested document and parent

I have the following mapping:
{
"accountId": {
"type": "long"
},
"storeProductId": {
"type": "long"
},
"storeSchemaId": {
"type": "long"
},
"yoyoValues": {
"type": "nested",
"properties": {
"yoyoNameId": {
"type": "long"
},
"dataType": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"id": {
"type": "long"
},
"languageId": {
"type": "long"
},
"value_Number": {
"type": "float"
},
"value_Raw": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
and I'm trying to get the max and min values for value_number for all nested documents with yoyoNameId of 3 that also has a parent document with an accountId of 1285 and storeSchemaId of 241.
Everytime I've tried, I've been unable to properly filter the nested documents so it ends up being the min and max values for all nested documents with the correct parent document values.
I've tried several different queries but my most recent one is as follows:
{
"size": 0,
"aggs": {
"filter-layer": {
"filters": {
"filters": [
{
"term": {
"accountId": 1285
}
},
{
"term": {
"yoyoSchemaId": 241
}
},
{
"nested": {
"path": "yoyoValues",
"query": {
"bool": {
"filter": [
{
"term": {
"yoyoValues.yoyoNameId": 3
}
}
]
}
}
}
}
]
},
"aggs": {
"yoyoValues": {
"nested": {
"path": "yoyoValues"
},
"inner": {
"filter": {
"term": {
"yoyoValues.yoyoNameId": 3
}
},
"aggs": {
"min_value": {
"min": {
"field": "yoyoValues.value_Number"
}
},
"max_value": {
"max": {
"field": "yoyoValues.value_Number"
}
}
}
}
}
}
}
}
}
Can someone please help me correct this query? I'm limited to elastic v7.13.

Boost score based on integer value - Elasticsearch

I'm not very experienced with ElasticSearch and would like to know how to boost a search based on a certain integer value.
This is an example of a document:
{
"_index": "links",
"_type": "db1",
"_id": "mV32vWcBZsblNn1WqTcN",
"_score": 8.115617,
"_source": {
"url": "example.com",
"title": "Example website",
"description": "This is an example website, used for various of examples around the world",
"likes": 9,
"popularity": 543,
"tags": [
{
"name": "example",
"votes": 5
},
{
"name": "test",
"votes": 2
},
{
"name": "testing",
"votes": 1
}
]
}
}
Now in this particular search, the focus is on the tags and I would like to know how to boost the _score and multiply it by the integer in the votes under tags.
If this is not possible (or very hard to achieve), I would simply like to know how to boost the _score by the votes (not under tags)
Example, add 0.1 to the _score for each integer in votes
This is the current search query I'm using (for searching tags only):
{
"query": {
"nested": {
"path": "tags",
"query": {
"bool":{
"should":{
"match":{
"tags.name":"example,testing,something else"
}
}
}
}
}
}
}
I couldn't find much online, and hope someone can help me out.
How do I boost the _score with an integer value?
Update
For more info, here is the mapping:
{
"links": {
"mappings": {
"db1": {
"properties": {
"url": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"description": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"likes": {
"type": "long"
},
"popularity": {
"type": "long"
},
"tags": {
"type": "nested",
"properties": {
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"votes": {
"type": "long"
}
}
}
}
}
}
}
}
Update 2
Changed the tags.likes/tags.dislikes to tags.votes, and added a nested property to the tags
This took a long time to figure out. I have learnt so much on my way there.
Here is the final result:
{
"query": {
"nested": {
"path": "tags",
"query": {
"function_score": {
"query": {
"bool": {
"should": [
{
"match": {
"tags.name": "example"
}
},
{
"match": {
"tags.name": "testing"
}
},
{
"match": {
"tags.name": "test"
}
}
]
}
},
"functions": [
{
"field_value_factor": {
"field": "tags.votes"
}
}
],
"boost_mode": "multiply"
}
}
}
}
}
The array in should has helped a lot, and was glad I could combine it with function_score
You are looking at function score query: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html
And field value factor https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-field-value-factor.
Snippet from documentation:
GET /_search
{
"query": {
"function_score": {
"field_value_factor": {
"field": "tags.dislikes",
"factor": 1.2,
"modifier": "sqrt",
"missing": 1
}
}
}
}
Or with script score because your nested tags field (not sure if field value score works fine with nested structure).

Range value search in elastic search, not returning results

I have a problem.
I have tried to make plenty of queries and none have returned any documents.
My data format is something like:
{
"_index": "orders",
"_type": "order",
"_id": "AVad66hjiOD-asNwVILB",
"_score": 1,
"_source": {
"document": {
"orderID": "1337",
"sku": "awesomeSku",
"customerID": "7331",
"productID": "20490859",
"variantID": "97920239",
"createTime": "2016-07-13T13:23:19Z",
"retailPrice": "10000",
"costPrice": "10000",
"new": 123
}
}
}
My query:
{
"query": {
"bool": {
"filter": [
{ "range": { "new": { "gte": "20" } } }
]
}
}
}
I just want to start somewhere simply and find all documents which has the attribute "new" with a value above 20.
Any feedback would be awesome.
Edit:
Data formart in ES:
{
"orders": {
"mappings": {
"order": {
"properties": {
"document": {
"properties": {
"costPrice": {
"type": "string"
},
"createTime": {
"type": "string"
},
"customerID": {
"type": "string"
},
"new": {
"type": "long"
},
"orderID": {
"type": "string"
},
"productID": {
"type": "string"
},
"retailPrice": {
"type": "string"
},
"sku": {
"type": "string"
},
"variantID": {
"type": "string"
}
}
}
}
}
}
}
}
You need to make your query like this on the document.new field since all your fields are nested into the top-level document section:
{
"query": {
"bool": {
"filter": [
{
"range": {
"document.new": {
"gte": 20
}
}
}
]
}
}
}

function_score query in elasticsearch won't change score

I have an index with following doc structure: Company > Jobs (nested)
Company have name and jobs have address. I search jobs by address by default. Along with this, I'm trying to boost certain companies by their name using function_score query. But my query doesn't seem to be boosting anything or change scores.
{
"query": {
"filtered": {
"filter": {},
"query": {
"function_score": {
"query": {
"nested": {
"path": "active_jobs",
"score_mode": "max",
"query": {
"multi_match": {
"query": "United States",
"type": "cross_fields",
"fields": [
"active_jobs.address.city",
"active_jobs.address.country",
"active_jobs.address.state"
]
}
},
"inner_hits": {
"size": 1000
}
}
},
"functions": [
{
"filter": {
"term": {
"name": "Amazon"
}
},
"weight": 100
}
]
}
}
}
},
"size": 30,
"from": 0
}
[Update 1]
Here is the mapping for active_jobs property:
"active_jobs": {
"type": "nested",
"properties": {
"active": {
"type": "boolean"
},
"address": {
"properties": {
"city": {
"type": "string"
},
"country": {
"type": "string"
},
"state": {
"type": "string"
},
"state_code": {
"type": "string"
}
}
},
"id": {
"type": "long"
},
"title": {
"type": "string"
},
"updated_at": {
"type": "date",
"format": "dateOptionalTime"
}
}
}

ElasticSearch Multi Field Terms Facet doesn't seem to work

I'm trying to get the Multi-Field Terms Facet to work, but the results can't seem to do what the documentation (http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-facets-terms-facet.html#_multi_fields_2) is saying.
A bit about the setup and the queries I've tried:
I'm using ES 1.1.0:
"name": "William Baker",
"version": {
"number": "1.1.0",
...
}
The mapping:
{
"index_name": {
"mappings": {
"object": {
"properties": {
"nested_object": {
"type": "nested",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string",
"index": "not_analyzed"
}
}
}
...
}
}
}
}
}
The query JSON:
{
"query": {
"filtered": {
"query": {
"match_all": {}
}
}
},
"facets": {
"test4": {
"terms": {
"fields": ["nested_object.name", "nested_object.id"]
},
"nested": "nested_object"
}
}
}
The result:
"facets": {
"test4": {
"_type": "terms",
"missing": 0,
"total": 26,
"other": 12,
"terms": [
{
"term": "Port Anastasia College",
"count": 3
},
{
"term": "31",
"count": 3
},
{
"term": "Zorahaven College",
"count": 1
},
{
"term": "West Lonzoview College",
"count": 1
},
...
]
}
}
I don't understand why the query is returning only the first ID, and as a separate term, instead of an aggregation of the two, as described in the documentation.
I'd appreciate any idea on how to get this multi-field facet to work.
Thanks.

Resources