Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Incorrect sort field when ordering by named meta query in WP_Query #3369

Closed
1 task done
selim13 opened this issue Mar 3, 2023 · 1 comment · Fixed by #3469
Closed
1 task done

BUG: Incorrect sort field when ordering by named meta query in WP_Query #3369

selim13 opened this issue Mar 3, 2023 · 1 comment · Fixed by #3469
Assignees
Labels
bug Something isn't working
Milestone

Comments

@selim13
Copy link

selim13 commented Mar 3, 2023

Describe the bug

When ordering by named meta query, ElasticPress uses the key of the meta query for the sort field, rather then the key of the meta field resulting in No mapping found for [named_meta_query_key] in order to sort on error message.

Steps to Reproduce

Having a custom post type product with a numeric ACF field price, I'm making a query to get the most expensive product:

$q = new WP_Query(
    [
        'ep_integrate' => true,
        'posts_per_page' => 1,
        'post_type' => 'product',
        'post_status' => 'publish',
        'meta_query' => [
            'price_clause' => [
                'key' => 'price',
                'compare' => 'EXISTS',
                'type' => 'NUMERIC',
            ],
        ],
        'orderby' => [
            'price_clause' => 'DESC',
        ],
    ]
);

This will make a query with sort field like this

    "sort": [
        {
            "price_clause": {
                "order": "DESC"
            }
        }
    ]

rather then

"sort": [
        {
            "meta.price.long": {
                "order": "DESC"
            }
        }
    ]
Query body
{
    "from": 0,
    "size": 1,
    "post_filter": {
        "bool": {
            "must": [
                {
                    "bool": {
                        "must": [
                            {
                                "exists": {
                                    "field": "meta.price"
                                }
                            }
                        ]
                    }
                },
                {
                    "terms": {
                        "post_type.raw": [
                            "product"
                        ]
                    }
                },
                {
                    "term": {
                        "post_status": "publish"
                    }
                }
            ]
        }
    },
    "query": {
        "match_all": {
            "boost": 1
        }
    },
    "sort": [
        {
            "price_clause": {
                "order": "DESC"
            }
        }
    ]
}

And results in a No mapping found for [price_clause] in order to sort on error message.

Query Resultz
{
    "error": {
        "root_cause": [
            {
                "type": "query_shard_exception",
                "reason": "No mapping found for [price_clause] in order to sort on",
                "index_uuid": "5mBi9ZWVTBW1B0_1G3UHxw",
                "index": "newfloralndosite-post-1"
            }
        ],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "can_match",
        "grouped": true,
        "failed_shards": [
            {
                "shard": 0,
                "index": "newfloralndosite-post-1",
                "node": "oXKkNnTrQRCuBqHyI5RzdA",
                "reason": {
                    "type": "query_shard_exception",
                    "reason": "No mapping found for [price_clause] in order to sort on",
                    "index_uuid": "5mBi9ZWVTBW1B0_1G3UHxw",
                    "index": "newfloralndosite-post-1"
                }
            }
        ]
    },
    "status": 400
}

Screenshots, screen recording, code snippet

No response

Environment information

No response

WordPress and ElasticPress information

WordPress

WordPress Environment

wp_version: 6.0
home_url: http://newflora.lndo.site
site_url: http://newflora.lndo.site
is_multisite: false
theme: Newflora NEW ()
plugins: Admin Columns Pro (5.5.3), Admin Columns Pro - Advanced Custom Fields (ACF) (2.6.4), Advanced Custom Fields PRO (5.12.2), Cyr to Lat enhanced (3.5), Debug Bar ElasticPress (2.1.1), ElasticPress (4.4.1), Featured Image Admin Thumb (1.6), Image Watermark (1.7.2), Newflora Модули (1.0), Query Monitor (3.9.0), Regenerate Thumbnails (3.1.5), Remove Emoji CSS and JS (1.1), Remove Taxonomy Base Slug (2.1), Safe SVG (2.0.1), Simple Custom Post Order (2.5.6), Transients Manager (2.0.2), WebP Express (0.25.5), WP OPcache (4.1.3), WP Super Cache (1.7.9), Yoast SEO (20.1) и Yoast SEO Premium (20.1)
revisions: all

Server Environment

php_version: 8.0.27
memory_limit: 256M
timeout: 90

Indexable Content

Newflora — http://newflora.lndo.site

product_count: 2 982
product_meta_keys: 9
total-all-post-types: 9
distinct-meta-keys: price, filter, color, season, size, type, flower-life, aroma и consist

ElasticPress

Settings

host: http://elasticsearch.newflora.internal:9200
index_prefix:
language: ru_RU
per_page: 350
network_active: false

Timeouts

request_timeout: 5
index_document_timeout: 15
bulk_request_timeout: 30

Failed Queries

Elasticsearch Indices

newfloralndosite-post-1

health: yellow
status: open
index: newfloralndosite-post-1
uuid: HycH_a_HRTy_RvEhWo9z-w
pri: 5
rep: 1
docs.count: 13261
docs.deleted: 3153
store.size: 163.7mb
pri.store.size: 163.7mb
total_fields_limit: 5000

Last Sync

2023/03/03 11:26:48 дп

method: WP-CLI
end_date_time: 2023/03/03 11:27:01 дп
total_time: 0 часов, 0 минут, 14 секунд
total: 2982
synced: 2982
skipped: 0
failed: 0
errors: array (
)

Feature Settings

Code of Conduct

  • I agree to follow this project's Code of Conduct
@selim13 selim13 added the bug Something isn't working label Mar 3, 2023
@felipeelia
Copy link
Member

Hey @selim13, thanks for opening the issue. While we work on fixing that, you may want to try changing the orderby clause to:

        'orderby' => [
            'meta.price.value.sortable' => 'DESC',
        ],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants