From 1042c1930fc89c3e4ec62f526ab4010d065f37dd Mon Sep 17 00:00:00 2001 From: Luca Gallinari Date: Fri, 19 Jul 2024 15:20:12 +0200 Subject: [PATCH] Json encode twig variables --- .../query/common/aggs/attributes.json.twig | 2 +- templates/query/common/aggs/options.json.twig | 4 +- .../aggs/translated-attributes.json.twig | 4 +- .../query/common/search/attribute.json.twig | 4 +- .../query/common/search/option.json.twig | 2 +- .../search/translated-attribute.json.twig | 4 +- .../query/common/sort/createdAt.json.twig | 2 +- templates/query/common/sort/default.json.twig | 4 +- templates/query/common/sort/name.json.twig | 4 +- .../query/common/sort/position.json.twig | 4 +- templates/query/common/sort/price.json.twig | 2 +- .../completion-suggesters/query.json.twig | 4 +- templates/query/search/query.json.twig | 47 ++++++++++--------- templates/query/taxon/query.json.twig | 2 +- .../query/term-suggesters/query.json.twig | 4 +- 15 files changed, 47 insertions(+), 46 deletions(-) diff --git a/templates/query/common/aggs/attributes.json.twig b/templates/query/common/aggs/attributes.json.twig index b1e9eec..40b2981 100644 --- a/templates/query/common/aggs/attributes.json.twig +++ b/templates/query/common/aggs/attributes.json.twig @@ -29,7 +29,7 @@ }, "label": { "terms": { - "field": "attributes.name.{{localeCode}}" + "field": {{ ('attributes.name.' ~ localeCode)|json_encode|raw }} } } } diff --git a/templates/query/common/aggs/options.json.twig b/templates/query/common/aggs/options.json.twig index 050b69f..26f8126 100644 --- a/templates/query/common/aggs/options.json.twig +++ b/templates/query/common/aggs/options.json.twig @@ -34,7 +34,7 @@ "aggs": { "label": { "terms": { - "field": "product-options.values.name.{{localeCode}}" + "field": {{ ('product-options.values.name.' ~ localeCode)|json_encode|raw }} } } } @@ -43,7 +43,7 @@ }, "label": { "terms": { - "field": "product-options.name.{{localeCode}}" + "field": {{ ('product-options.name.' ~ localeCode)|json_encode|raw }} } } } diff --git a/templates/query/common/aggs/translated-attributes.json.twig b/templates/query/common/aggs/translated-attributes.json.twig index a5dcf39..9ce646f 100644 --- a/templates/query/common/aggs/translated-attributes.json.twig +++ b/templates/query/common/aggs/translated-attributes.json.twig @@ -24,12 +24,12 @@ "aggs": { "values": { "terms": { - "field": "translated-attributes.values.{{ localeCode }}.values" + "field": {{ ('translated-attributes.values.' ~ localeCode ~ '.values')|json_encode|raw }} } }, "label": { "terms": { - "field": "translated-attributes.name.{{localeCode}}" + "field": {{ ('translated-attributes.name.' ~ localeCode)|json_encode|raw }} } } } diff --git a/templates/query/common/search/attribute.json.twig b/templates/query/common/search/attribute.json.twig index 135141e..c5542d9 100644 --- a/templates/query/common/search/attribute.json.twig +++ b/templates/query/common/search/attribute.json.twig @@ -7,12 +7,12 @@ "filter": [ { "term": { - "attributes.code": "{{ filter.code }}" + "attributes.code": {{ filter.code|json_encode|raw }} } }, { "term": { - "attributes.values.values": "{{ filter.value }}" + "attributes.values.values": {{ filter.value|json_encode|raw }} } } ] diff --git a/templates/query/common/search/option.json.twig b/templates/query/common/search/option.json.twig index 7785030..cff5700 100644 --- a/templates/query/common/search/option.json.twig +++ b/templates/query/common/search/option.json.twig @@ -12,7 +12,7 @@ }, { "term": { - "variants.options.value.code": "{{ filter.value }}" + "variants.options.value.code": {{ filter.value|json_encode|raw }} } } ] diff --git a/templates/query/common/search/translated-attribute.json.twig b/templates/query/common/search/translated-attribute.json.twig index a535dae..80bce25 100644 --- a/templates/query/common/search/translated-attribute.json.twig +++ b/templates/query/common/search/translated-attribute.json.twig @@ -7,12 +7,12 @@ "filter": [ { "term": { - "translated-attributes.code": "{{ filter.code }}" + "translated-attributes.code": {{ filter.code|json_encode|raw }} } }, { "term": { - "translated-attributes.values.{{ localeCode }}.values": "{{ filter.value }}" + {{ ('translated-attributes.values.' ~ localeCode ~ '.values') }}: {{ filter.value|json_encode|raw }} } } ] diff --git a/templates/query/common/sort/createdAt.json.twig b/templates/query/common/sort/createdAt.json.twig index f4429fa..84756cd 100644 --- a/templates/query/common/sort/createdAt.json.twig +++ b/templates/query/common/sort/createdAt.json.twig @@ -1,5 +1,5 @@ { "created-at": { - "order": "{{ order }}" + "order": {{ order|json_encode|raw }} } } diff --git a/templates/query/common/sort/default.json.twig b/templates/query/common/sort/default.json.twig index 13ba1ef..3a73260 100644 --- a/templates/query/common/sort/default.json.twig +++ b/templates/query/common/sort/default.json.twig @@ -1,5 +1,5 @@ { - "{{ field }}": { - "order": "{{ order }}" + {{ field|json_encode|raw }}: { + "order": {{ order|json_encode|raw }} } } diff --git a/templates/query/common/sort/name.json.twig b/templates/query/common/sort/name.json.twig index 78213f5..0f64885 100644 --- a/templates/query/common/sort/name.json.twig +++ b/templates/query/common/sort/name.json.twig @@ -1,6 +1,6 @@ { - "name-as-keyword.{{ localeCode }}": { - "order": "{{ order }}", + {{ ('name-as-keyword.' ~ localeCode)|json_encode|raw }}: { + "order": {{ order|json_encode|raw }}, "nested": { "path": "name-as-keyword" } diff --git a/templates/query/common/sort/position.json.twig b/templates/query/common/sort/position.json.twig index e587e11..39a3f52 100644 --- a/templates/query/common/sort/position.json.twig +++ b/templates/query/common/sort/position.json.twig @@ -1,11 +1,11 @@ { "taxons.position": { - "order": "{{ order }}", + "order": {{ order|json_encode|raw }}, "nested": { "path": "taxons", "filter": { "term": { - "taxons.sylius-id": "{{ taxon.id }}" + "taxons.sylius-id": {{ taxon.id|json_encode|raw }} } } } diff --git a/templates/query/common/sort/price.json.twig b/templates/query/common/sort/price.json.twig index c29ba7b..66ab634 100644 --- a/templates/query/common/sort/price.json.twig +++ b/templates/query/common/sort/price.json.twig @@ -1,5 +1,5 @@ { "default-variant.price.price": { - "order": "{{ order }}" + "order": {{ order|json_encode|raw }} } } diff --git a/templates/query/completion-suggesters/query.json.twig b/templates/query/completion-suggesters/query.json.twig index 50c78ff..065e7f3 100644 --- a/templates/query/completion-suggesters/query.json.twig +++ b/templates/query/completion-suggesters/query.json.twig @@ -1,9 +1,9 @@ { "suggest": { - "prefix": "{{ searchTerm }}", + "prefix": {{ searchTerm|json_encode|raw }}, "completion": { "field": "suggest", - "size": {{ size }}, + "size": {{ size|json_encode|raw }}, "skip_duplicates": true, "fuzzy": { "fuzziness": 1, diff --git a/templates/query/search/query.json.twig b/templates/query/search/query.json.twig index cef596f..905dc30 100644 --- a/templates/query/search/query.json.twig +++ b/templates/query/search/query.json.twig @@ -1,3 +1,4 @@ +{% set searchTerm = searchTerm|json_encode %} { "bool": { "filter": [ @@ -15,7 +16,7 @@ "should": [ { "term": { - "code": "{{ searchTerm }}" + "code": {{ searchTerm }} } }, { @@ -23,7 +24,7 @@ "path": "variants", "query": { "term": { - "variants.code": "{{ searchTerm }}" + "variants.code": {{ searchTerm|raw }} } } } @@ -33,8 +34,8 @@ "path": "name", "query": { "match": { - "name.{{ localeCode }}": { - "query": "{{ searchTerm }}", + {{ ('name.' ~ localeCode)|json_encode|raw }}: { + "query": {{ searchTerm|raw }}, "boost": 10, "fuzziness": "AUTO", "fuzzy_transpositions": true, @@ -49,8 +50,8 @@ "path": "variants", "query": { "match": { - "variants.name.{{ localeCode }}": { - "query": "{{ searchTerm }}", + {{ ('variants.name.' ~ localeCode)|json_encode|raw }}: { + "query": {{ searchTerm|raw }}, "boost": 9, "fuzziness": "AUTO", "fuzzy_transpositions": true, @@ -65,8 +66,8 @@ "path": "description", "query": { "match": { - "description.{{ localeCode }}": { - "query": "{{ searchTerm }}", + {{ ('description.' ~ localeCode)|json_encode|raw }}: { + "query": {{ searchTerm|raw }}, "boost": 8, "fuzziness": "AUTO", "fuzzy_transpositions": true, @@ -81,8 +82,8 @@ "path": "short-description", "query": { "match": { - "short-description.{{ localeCode }}": { - "query": "{{ searchTerm }}", + {{ ('short-description.' ~ localeCode)|json_encode|raw }}: { + "query": {{ searchTerm|raw }}, "boost": 7, "fuzziness": "AUTO", "fuzzy_transpositions": true, @@ -97,8 +98,8 @@ "path": "slug", "query": { "match": { - "slug.{{ localeCode }}": { - "query": "{{ searchTerm }}", + {{ ('slug.' ~ localeCode)|json_encode|raw }}: { + "query": {{ searchTerm|raw }}, "boost": 6, "fuzziness": "AUTO", "fuzzy_transpositions": true, @@ -113,8 +114,8 @@ "path": "meta-description", "query": { "match": { - "meta-description.{{ localeCode }}": { - "query": "{{ searchTerm }}", + {{ ('meta-description.' ~ localeCode)|json_encode|raw }}: { + "query": {{ searchTerm|raw }}, "boost": 5, "fuzziness": "AUTO", "fuzzy_transpositions": true, @@ -129,8 +130,8 @@ "path": "meta-keywords", "query": { "match": { - "meta-keywords.{{ localeCode }}": { - "query": "{{ searchTerm }}", + {{ ('meta-keywords.' ~ localeCode)|json_encode|raw }}: { + "query": {{ searchTerm|raw }}, "boost": 4, "fuzziness": "AUTO", "fuzzy_transpositions": true, @@ -145,8 +146,8 @@ "path": "taxons", "query": { "match": { - "taxons.name.{{ localeCode }}": { - "query": "{{ searchTerm }}", + {{ ('taxons.name.' ~ localeCode)|json_encode|raw }}: { + "query": {{ searchTerm|raw }}, "boost": 3, "fuzziness": "AUTO", "fuzzy_transpositions": true, @@ -161,8 +162,8 @@ "path": "translated-attributes", "query": { "match": { - "translated-attributes.values.{{ localeCode }}.text-value": { - "query": "{{ searchTerm }}", + {{ ('translated-attributes.values.' ~ localeCode ~ '.text-value')|json_encode|raw }}: { + "query": {{ searchTerm|raw }}, "boost": 3, "fuzziness": "AUTO", "fuzzy_transpositions": true, @@ -178,7 +179,7 @@ "query": { "match": { "attributes.values.text-value": { - "query": "{{ searchTerm }}", + "query": {{ searchTerm|raw }}, "boost": 3, "fuzziness": "AUTO", "fuzzy_transpositions": true, @@ -193,8 +194,8 @@ "path": "product-options", "query": { "match": { - "product-options.values.name.{{ localeCode }}": { - "query": "{{ searchTerm }}", + {{ ('product-options.values.name.' ~ localeCode)|json_encode|raw }}: { + "query": {{ searchTerm|raw }}, "boost": 3, "fuzziness": "AUTO", "fuzzy_transpositions": true, diff --git a/templates/query/taxon/query.json.twig b/templates/query/taxon/query.json.twig index abc5961..d5ff11b 100644 --- a/templates/query/taxon/query.json.twig +++ b/templates/query/taxon/query.json.twig @@ -14,7 +14,7 @@ "filter": [ { "terms": { - "taxons.sylius-id": [{{ taxonIdsToSearch|join(', ') }}] + "taxons.sylius-id": {{ taxonIdsToSearch|json_encode|raw }} } } ] diff --git a/templates/query/term-suggesters/query.json.twig b/templates/query/term-suggesters/query.json.twig index 0cf63c4..5d225e4 100644 --- a/templates/query/term-suggesters/query.json.twig +++ b/templates/query/term-suggesters/query.json.twig @@ -1,8 +1,8 @@ { "suggest": { - "text": "{{ searchTerm }}", + "text": {{ searchTerm|json_encode|raw }}, "term": { - "field": "name.{{ localeCode }}", + "field": {{ ('name.' ~ localeCode)|json_encode|raw }}, "suggest_mode" : "missing" } }