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"
}
}
}
}
Related
How do I write a query with two search terms which matches nested objects with inner hits highlighted.
Below is the sample usecase:
I have a mapping:
"mappings": {
"properties": {
"grocery_name": {
"type": "text"
},
"items": {
"type": "nested",
"properties": {
"name": {
"type": "text"
},
"stock": {
"type": "integer"
},
"category": {
"type": "text"
}
}
}
}
}
and the data looks like below
{
"grocery_name": "Elastic Eats",
"items": [
{
"name": "Red banana",
"stock": "12",
"category": "fruit"
},
{
"name": "Cavendish banana",
"stock": "10",
"category": "fruit"
},
{
"name": "peach",
"stock": "10",
"category": "fruit"
},
{
"name": "carrot",
"stock": "9",
"category": "vegetable"
},
{
"name": "broccoli",
"stock": "5",
"category": "vegetable"
}
]
}
here if i want a document which has peach and carrot both in nested docs
i can do search with multiple nested queries like below
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "items",
"query": {
"match_phrase": {
"items.name": {
"query": "carrot"
}
}
}
}
},
{
"nested": {
"path": "items",
"query": {
"match_phrase": {
"items.name": {
"query": "peach"
}
}
}
}
}
]
}
}
}
the above query works perfectly, but if i add inner hits i can't add for both nested queries , if i do i get following error
[inner_hits] already contains an entry for key[items]
i want to get each matched nested object highlighted, as _source highlighting is not supported in elasticsearch is there any way i can do highlighting for each nested object?
You certainly can have multiple inner_hits but they need to be appropriately named:
{
"query": {
"bool": {
"must": [
{
"nested": {
"inner_hits": {
"name": "carrot" <--
},
"path": "items",
"query": {
"match_phrase": {
"items.name": {
"query": "carrot"
}
}
}
}
},
{
"nested": {
"inner_hits": {
"name": "peach" <--
},
"path": "items",
"query": {
"match_phrase": {
"items.name": {
"query": "peach"
}
}
}
}
}
]
}
}
}
You were getting the error b/c if no name is provided, the system will default to the nested path which is items in both of your subqueries.
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"
}
}
}
}
}
}
I have this index mapping with nested fields:
"customerpropertieses": {
"_parent": {
"type": "customerprofile"
},
"_routing": {
"required": true
},
"properties": {
"id": {
"type": "string"
},
"parentId": {
"type": "string"
},
"properties": {
"type": "nested",
"properties": {
"extentionPropertyId": {
"type": "long"
},
"propertyName": {
"type": "string"
},
"value": {
"type": "string"
}
}
}
}
}
I want to find customerpropertieses where propertyName=criteria1 & value=value1 & propertyName=criteria2 & value=value2
The query I'm generating by hand is this:
{
"from": 0,
"size": 10,
"sort": [
{
"_score": {
"order": "desc"
}
}
],
"query": {
"nested": {
"query": {
"bool": {
"must": [
{
"match": {
"properties.propertyName": {
"query": "criteria1 "
}
}
},
{
"match": {
"properties.value": {
"boost": 10.0,
"query": "value1"
}
}
},
{
"match": {
"properties.propertyName": {
"query": "criteria2"
}
}
},
{
"match": {
"properties.value": {
"boost": 10.0,
"query": "value2"
}
}
}
]
}
},
"path": "properties",
"inner_hits": {
"explain": false
},
"_name": "nested_properties"
}
}
}
I get 0 results back and I certainly have data with those characteristics. The search works fine when I look only propertyName=criteria1 & value=value1 or with propertyName=criteria2 & value=value2'
The question is how can I stack search criteria using & in a nested query?
Try the following query. From the look of your query i take a guess you want to match documents which had two nested documents with values value1,criteria1 and value2,criteria2 in value and criteria field respectively.
{
"from": 0,
"size": 10,
"sort": [{
"_score": {
"order": "desc"
}
}],
"query": {
"bool": {
"must": [{
"nested": {
"query": {
"bool": {
"must": [{
"match": {
"properties.propertyName": {
"query": "criteria2"
}
}
}, {
"match": {
"properties.value": {
"boost": 10.0,
"query": "value2"
}
}
}]
}
},
"path": "properties",
"inner_hits": {
"explain": false
},
"_name": "nested_properties"
}
}, {
"nested": {
"query": {
"bool": {
"must": [{
"match": {
"properties.propertyName": {
"query": "criteria1 "
}
}
}, {
"match": {
"properties.value": {
"boost": 10.0,
"query": "value1"
}
}
}]
}
},
"path": "properties",
"inner_hits": {
"explain": false
},
"_name": "nested_properties"
}
}]
}
}
}
I indexed the following document making the guess and modified the query
The Following document will match the query above
{
"parentId" : "3434",
"properties" : [{
"extentionPropertyId" : 24,
"propertyName" : "criteria1",
"value" : "value1"
},{
"extentionPropertyId" : 24,
"propertyName" : "criteria2",
"value" : "value2"
}]
}
Hope this helps.
Thanks
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"
}
}
}
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.