ElasticSearch Multi Field Terms Facet doesn't seem to work - elasticsearch

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.

Related

elasticsearch with range sub query in nested query

I am trying to get a nested query filter inside of a nested.
here is my es mapping: there is one "id" field(long) and a nested field called "my_field" with four sub fields in it.
{
"my_index": {
"mappings": {
"dynamic": "strict",
"properties": {
"id": {
"type": "long"
},
"my_field": {
"type": "nested",
"properties": {
"x": {
"type": "long"
},
"y": {
"type": "long"
},
"z": {
"type": "long"
},
"a": {
"type": "double"
},
"b": {
"type": "long"
}
}
}
}
}
}
}
My question is how to retrive the document with nested es query which contains sub range query in it.
For example, I'm trying to get two document id :11111 and id:22222 with nested query restriction "x > 15" or "a > 0.5" and also with inner hit size limitation, which is 20 here.
{
"_source": false,
"query": {
"bool": {
"must": {
"nested": {
"inner_hits": {
"size": 20
},
"path": "my_field",
"query": {
"bool": {
"should": [
{
"range": {
"x": {
"from": 15,
"include_lower": true,
"include_upper": true,
"to": null
}
}
},
{
"range": {
"a": {
"from": 0.5,
"include_lower": true,
"include_upper": true,
"to": null
}
}
}
]
}
}
}
},
"should": [
{
"term": {
"id": 11111
}
},
{
"term": {
"id": 22222
}
}
]
}
},
"timeout": "5000ms",
"track_total_hits": true
}
However, there are no hits return
Please use the dot notation in your query to include the complete path, e.g.,
"range": {
"my_field.x": { "from": ... }
}

Nested aggregation: how to get properties of aggregated object

I have a "products" index with the nested field mapping. I perform a search query with nested aggregation and term aggregation by nested object's id. How to get "title" and "slug" properties from nested object in buckets?
PUT /products
{
"mappings": {
"properties": {
"categories": {
"type": "nested",
"properties": {
"id": { "type": "long" },
"title": { "type": "text" },
"slug": { "type": "keyword" }
}
}
}
}
}
POST /products/_doc
{
"name": "Acer Aspire 5 Slim Laptop",
"categories": [
{
"id": 1,
"title": "Laptops",
"slug": "/catalog/laptops"
},
{
"id": 2,
"title": "Ultrabooks",
"slug": "/catalog/ultrabooks"
}
]
}
GET /products/_search
{
"query": {
"match": { "name": "acer" }
},
"aggs": {
"categories": {
"nested": {
"path": "categories"
},
"aggs": {
"id": {"terms": {"field": "categories.id"}}
}
}
}
}
That's a great start!! All you need is to add a top_hits sub-aggregation like this:
GET /products/_search
{
"query": {
"match": {
"name": "acer"
}
},
"aggs": {
"categories": {
"nested": {
"path": "categories"
},
"aggs": {
"id": {
"terms": {
"field": "categories.id"
},
"aggs": {
"hits": {
"top_hits": {
"size": 1,
"_source": ["categories.title", "categories.slug"]
}
}
}
}
}
}
}
}

Filter query is not working in elastic search

I have document and search query as below elastic is not able to fetch the documents for the matched exception id's initially while creating the index i have done the mapping and after that it is not able to fetch the records
and my mapping looks like below
{
"mappings": {
"properties": {
"events": {
"type": "nested",
"properties": {
"data": {
"type": "nested",
"properties": {
"comments": {
"type": "nested",
"properties": {
"type": {
"type": "keyword"
}
}
}
}
}
}
}
}
}
}
here is my index document which i am testing against using the search query.
{
"id": "1",
"score": 1,
"comments": [{
"id": "1",
"type": "Delayed",
You cannot directly use query-string on nested fields, You need to use nested query for it
GET <index-name>/_search
{
"query": {
"bool": {
"filter": [
{
"nested": { --> note
"path": "events.recommendationData",
"query": {
"query_string": {
"query": "\"1\" OR \"2\"",
"fields": [
"events.recommendationData.exceptionId"
],
"type": "best_fields",
"default_operator": "or",
"max_determinized_states": 10000,
"enable_position_increments": true,
"fuzziness": "AUTO",
"fuzzy_prefix_length": 0,
"fuzzy_max_expansions": 50,
"phrase_slop": 0,
"escape": false,
"auto_generate_synonyms_phrase_query": true,
"fuzzy_transpositions": true,
"boost": 1
}
}
}
}
]
}
},
"size": 1, --> note, to return documents ,keep 0 for only aggs
"aggs": {
"genres": {
"nested": {
"path": "events.recommendationData.recommendations"
},
"aggs": {
"nested_comments_recomms": {
"terms": {
"field": "events.recommendationData.recommendations.recommendationType"
}
}
}
}
}
}

How to filter by ids together with filter fields value?

How to add additional filter to match category values in blog.post.notes all fields? First I want to filter by ids, then filter notes category, is it possible?
I can filter only by ids:
GET posts/posts/_search?fields=_id&_source=blog.post.notes
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"ids": {
"values": [
"100000000001234"
]
}
}
}
}
}
How to filter e.g. "test" category from current results:
{
"took": 58,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [{
"_index": "posts",
"_type": "posts",
"_id": "100000000001234",
"_score": 1,
"_source": {
"blog": {
"post": {
"notes": {
"main": [{
"message": "blablabla",
"category": "test"
}, {
"message": "blablabla",
"category": "other"
}],
"cart": [{
"message": "blablabla",
"category": "test"
}, {
"message": "blablabla",
"category": "other"
}]
}
}
}
}
}]
}
}
curl -XGET localhost:9200/posts/_mapping/posts
{
"posts": {
"mappings": {
"posts": {
"dynamic_templates": [{
"blog": {
"mapping": {
"index": "analyzed"
},
"path_match": "blog.*",
"path_unmatch": "*.medias.*"
}
}, {
"ids": {
"mapping": {
"index": "not_analyzed",
"type": "string"
},
"match": "_id|base_id",
"match_pattern": "regex"
}
}],
"_all": {
"enabled": false
},
"properties": {
"query": {
"properties": {
"filtered": {
"properties": {
"filter": {
"properties": {
"ids": {
"properties": {
"values": {
"type": "string"
}
}
}
}
},
"query": {
"properties": {
"match_all": {
"type": "object"
}
}
}
}
},
"match_all": {
"type": "object"
}
}
},
"source": {
"dynamic": "true",
"properties": {
"post": {
"dynamic": "true",
"properties": {
"_id": {
"type": "string",
"index": "not_analyzed"
},
"base_id": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
},
"blog": {
"properties": {
"post": {
"properties": {
"_id": {
"type": "string"
},
"notes": {
"properties": {
"main": {
"properties": {
"id": {
"type": "string"
},
"message": {
"type": "string"
},
"category": {
"type": "string"
}
}
},
"cart": {
"properties": {
"id": {
"type": "string"
},
"message": {
"type": "string"
},
"category": {
"type": "string"
}
}
}
}
}
}
}
}
}
}
}
}
}
}
You can use bool query with must on ids and terms
POST c1_2/Test/_search
{
"query": {
"bool": {
"must": [
{
"ids": {
"values": [
1,
2,
3
]
}
},
{
"terms": {
"blog.post.notes.main.category": [
"categoryfilter"
]
}
}
]
}
}
}
But since you have main and cart categories you must use filter on each of them, in my example i filter on main categories, if you need to do filter on both you need to use one more or filter which will filter on main or cart categories
Also you should know that category should be not_analyzed in order to filter on something like "my super category" other wise query will not be working properly.
Example
POST c1_2/Blog/1
{
"post": {
"notes": {
"main": [
{
"message": "blablabla",
"category": "test"
},
{
"message": "blablabla",
"category": "other"
}
],
"cart": [
{
"message": "blablabla",
"category": "test"
},
{
"message": "blablabla",
"category": "other"
}
]
}
}
}
POST c1_2/Blog/2
{
"post": {
"notes": {
"main": [
{
"message": "blablabla",
"category": "second"
},
{
"message": "blablabla",
"category": "third"
}
],
"cart": [
{
"message": "blablabla",
"category": "test"
},
{
"message": "blablabla",
"category": "other"
}
]
}
}
}
POST c1_2/Blog/_search
{
"query": {
"bool": {
"must": [
{
"ids": {
"values": [
1,
2,
3
]
}
},
{
"terms": {
"post.notes.main.category": [
"test"
]
}
}
]
}
}
}

Search for values of nested facet in elasticsearch

This represents my mapping:
{
"name": {"type": "string", "include_in_all": true},
"properties": {
"type": "nested",
"properties": {
"name": {"type": "string"},
"value": {"type": "string"}
}
}
How can I use facetted search for the value of 'properties.value'? Here is an example document:
{
"name": "Testproduct",
"properties": [{
"name": "Color",
"value": "Green"
}, {
"name": "Size",
"value": "M"
}]
}
I want to build a facetted list (only) by the "Color"-property. So the result should look like this:
Red: 7 times
Green: 5 times
Blue: 1 times
This is what I have tried so far:
{
"size": 1000,
"query": {
"query_string": {
"query": " ... ",
"default_operator": "AND"
}
},
"facets": {
"resolution": {
"nested": "properties",
"facet_filter": {
"term": {
"name": "Color"
}
},
"terms_stats": {
"key_field": "name",
"value_field": "value"
}
}
}
}
If I execute this search query, I get the following response:
FacetPhaseExecutionException[Facet [resolution]: value_field [value] isn't a number field, but a string];
I barely understand the response, but I have no idea what I have done wrong by concept.
Thats because you are using terms_stats. term_stats is for numbers. You should be using terms. Try this query instead:
{
"size": 1000,
"query": {
"query_string": {
"query": " ... ",
"default_operator": "AND"
}
},
"facets": {
"resolution": {
"nested": "properties",
"facet_filter": {
"term": {
"name": "Color"
}
},
"terms": {
"field": "properties.value"
}
}
}
}

Resources