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

Related Posts not work #3901

Closed
1 task done
rstrah opened this issue Apr 26, 2024 · 8 comments
Closed
1 task done

Related Posts not work #3901

rstrah opened this issue Apr 26, 2024 · 8 comments

Comments

@rstrah
Copy link

rstrah commented Apr 26, 2024

Describe your question

I have function:

function ep_get_related_posts($post_id = null, $related_post_count = 5)
{
	if (class_exists('\ElasticPress\Features')) {
		$related_posts = \ElasticPress\Features::factory()->get_registered_feature('related_posts')->find_related($post_id, $related_post_count);

		if ($related_posts) {
			echo '<h2>Похожие новости:</h2>';
			echo '<ul>';
			foreach ($related_posts as $related_post) {
				echo '<li><a href="' . get_permalink($related_post->ID) . '">' . $related_post->post_title . '</a></li>';
			}
			echo '</ul>';
		} else {
			echo 'No related posts found.';
		}
	} else {
		echo 'ElasticPress is not available.';
	}
}

I get "No related posts found"

I call it like this (I'm sure the $id being passed is not empty)
ep_get_related_posts($id, $related_post_count = 5)

Through the block in Gutenberg I get the same thing(If you use your block).

Code of Conduct

  • I agree to follow this project's Code of Conduct
@felipeelia
Copy link
Member

@rstrah, ep_get_related_posts($id, $related_post_count = 5), how $id is being created?

Also, using the debugging add-on, if you visit the page where the "No related posts found" message is displayed, you should be able to debug the query being sent to ElasticPress. When you get that, feel free to share it here if you want.

@rstrah
Copy link
Author

rstrah commented May 3, 2024

@felipeelia Hi.

<div class="main-content" id="<?php echo $post->ID; ?>">
<?php render_news_whole_post($post->ID,NULL,true); ?>
<?php ep_get_related_posts($post->ID, $related_post_count = 5); ?>

Like that )

@rstrah
Copy link
Author

rstrah commented May 3, 2024

## Queries info ##

### https://dev.minsknews.by/v-oktyabrskom-rajone-uzhe-zavershayut-plan-po-kapremontu-zhilyh-domov-za-2023-god (2024-05-03 10:16:39) ###
es_req: POST http://127.0.0.1:9200/devminsknewsby-post-1/_search
request_id: 682cc6024f9c44879f3d5b2f762f56df
query_time: 0.041961669921875
wp_args: array (
  'post_type' => 
  array (
    0 => 'post',
    1 => 'page',
    2 => 'event',
    3 => 'newspaper',
    4 => 'district',
    5 => 'cae',
    6 => 'prm',
    7 => 'aaq',
    8 => 'aqq-appeals',
    9 => 'meri',
    10 => 'authors-column',
    11 => 'video',
    12 => 'programs',
    13 => 'news',
    14 => 'qsm_quiz',
  ),
  'post_status' => 
  array (
    0 => 'publish',
  ),
  's' => 'ep_autosuggest_placeholder',
  'ep_integrate' => true,
  'error' => '',
  'm' => '',
  'p' => 0,
  'post_parent' => '',
  'subpost' => '',
  'subpost_id' => '',
  'attachment' => '',
  'attachment_id' => 0,
  'name' => '',
  'pagename' => '',
  'page_id' => 0,
  'second' => '',
  'minute' => '',
  'hour' => '',
  'day' => 0,
  'monthnum' => 0,
  'year' => 0,
  'w' => 0,
  'category_name' => '',
  'tag' => '',
  'cat' => '',
  'tag_id' => '',
  'author' => '',
  'author_name' => '',
  'feed' => '',
  'tb' => '',
  'paged' => 0,
  'meta_key' => '',
  'meta_value' => '',
  'preview' => '',
  'sentence' => '',
  'title' => '',
  'fields' => '',
  'menu_order' => '',
  'embed' => '',
  'category__in' => 
  array (
  ),
  'category__not_in' => 
  array (
  ),
  'category__and' => 
  array (
  ),
  'post__in' => 
  array (
  ),
  'post__not_in' => 
  array (
  ),
  'post_name__in' => 
  array (
  ),
  'tag__in' => 
  array (
  ),
  'tag__not_in' => 
  array (
  ),
  'tag__and' => 
  array (
  ),
  'tag_slug__in' => 
  array (
  ),
  'tag_slug__and' => 
  array (
  ),
  'post_parent__in' => 
  array (
  ),
  'post_parent__not_in' => 
  array (
  ),
  'author__in' => 
  array (
  ),
  'author__not_in' => 
  array (
  ),
  'search_columns' => 
  array (
  ),
  'ep_suggestion' => true,
  'cache_results' => false,
  'ignore_sticky_posts' => false,
  'suppress_filters' => false,
  'update_post_term_cache' => true,
  'update_menu_item_cache' => false,
  'lazy_load_term_meta' => true,
  'update_post_meta_cache' => true,
  'posts_per_page' => 10,
  'nopaging' => false,
  'comments_per_page' => '50',
  'no_found_rows' => false,
  'search_terms_count' => 1,
  'search_terms' => 
  array (
    0 => 'ep_autosuggest_placeholder',
  ),
  'search_orderby_title' => 
  array (
    0 => 'wp_posts.post_title LIKE \'{0864cc6327dc9035ea94c8f15e0146910ae5163e7d231c380f589d9e1b20f25c}ep\\\\_autosuggest\\\\_placeholder{0864cc6327dc9035ea94c8f15e0146910ae5163e7d231c380f589d9e1b20f25c}\'',
  ),
  'order' => 'DESC',
)
status_code: 200
body: {"from":0,"size":10,"post_filter":{"bool":{"must":[{"terms":{"post_type.raw":["post","page","event","newspaper","district","cae","prm","aaq","aqq-appeals","meri","authors-column","video","programs","news","qsm_quiz"]}},{"term":{"post_status":"publish"}},{"bool":{"must_not":[{"terms":{"meta.ep_exclude_from_search.raw":["1"]}}]}}]}},"query":{"function_score":{"query":{"bool":{"should":[{"bool":{"must":[{"bool":{"should":[{"multi_match":{"query":"ep_autosuggest_placeholder","type":"phrase","fields":["post_title^100","post_excerpt^100","post_content^100","terms.ep_custom_result.name^9999","meta.\\_crb\\_manager\\_description|%.value^100","meta._crb_manager_description|%.value^100"],"boost":3}},{"multi_match":{"query":"ep_autosuggest_placeholder","fields":["post_title^100","post_excerpt^100","post_content^100","meta.\\_crb\\_manager\\_description|%.value^100","meta._crb_manager_description|%.value^100","post_title.suggest^1"],"operator":"and","boost":1,"fuzziness":"auto"}},{"multi_match":{"query":"ep_autosuggest_placeholder","type":"cross_fields","fields":["post_title^1","post_excerpt^1","post_content^1","terms.ep_custom_result.name^1","meta.\\_crb\\_manager\\_description|%.value^1","meta._crb_manager_description|%.value^1"],"boost":1,"analyzer":"standard","tie_breaker":0.5,"operator":"and"}}]}}],"filter":[{"match":{"post_type.raw":"page"}}]}},{"bool":{"must":[{"bool":{"should":[{"multi_match":{"query":"ep_autosuggest_placeholder","type":"phrase","fields":["post_title^82","post_excerpt^59","post_content^65","post_author.display_name^54","terms.ep_custom_result.name^9999","terms.news-district.name","terms.news-list.name"],"boost":3}},{"multi_match":{"query":"ep_autosuggest_placeholder","fields":["post_title^82","post_excerpt^59","post_content^65","post_author.display_name^54","terms.news-district.name","terms.news-list.name","post_title.suggest^1","term_suggest^1"],"operator":"and","boost":1,"fuzziness":"auto"}},{"multi_match":{"query":"ep_autosuggest_placeholder","type":"cross_fields","fields":["post_title^1","post_excerpt^1","post_content^1","post_author.display_name^1","terms.ep_custom_result.name^1","terms.news-district.name","terms.news-list.name"],"boost":1,"analyzer":"standard","tie_breaker":0.5,"operator":"and"}}]}}],"filter":[{"match":{"post_type.raw":"news"}}]}}]}},"functions":[{"exp":{"post_date_gmt":{"scale":"14d","decay":0.25,"offset":"7d"}}},{"weight":0.001}],"score_mode":"sum","boost_mode":"multiply"}},"sort":[{"_score":{"order":"desc"}}],"highlight":{"fields":{"post_title":{"pre_tags":["<mark class='ep-highlight'>"],"post_tags":["<\/mark>"],"type":"plain","number_of_fragments":0},"post_content":{"pre_tags":["<mark class='ep-highlight'>"],"post_tags":["<\/mark>"],"type":"plain","number_of_fragments":0},"post_excerpt":{"pre_tags":["<mark class='ep-highlight'>"],"post_tags":["<\/mark>"],"type":"plain","number_of_fragments":0}}},"suggest":{"text":"ep_autosuggest_placeholder","ep_suggestion":{"phrase":{"field":"post_content.shingle","max_errors":2,"direct_generator":[{"field":"post_content.shingle"}]}}}}
result: array (
  0 => 'This is a fake request to build the ElasticPress Autosuggest query. It is not really sent.',
)

### https://dev.minsknews.by/v-oktyabrskom-rajone-uzhe-zavershayut-plan-po-kapremontu-zhilyh-domov-za-2023-god (2024-05-03 10:16:39) ###
es_req: POST http://127.0.0.1:9200/devminsknewsby-post-1/_search
request_id: 60924c349c66485d86587cde20f857c8
query_time: 16.212224960327
wp_args: array (
  'more_like' => 1236992,
  'posts_per_page' => 5,
  'ep_integrate' => true,
  'ignore_sticky_posts' => true,
  'error' => '',
  'm' => '',
  'p' => 0,
  'post_parent' => '',
  'subpost' => '',
  'subpost_id' => '',
  'attachment' => '',
  'attachment_id' => 0,
  'name' => '',
  'pagename' => '',
  'page_id' => 0,
  'second' => '',
  'minute' => '',
  'hour' => '',
  'day' => 0,
  'monthnum' => 0,
  'year' => 0,
  'w' => 0,
  'category_name' => '',
  'tag' => '',
  'cat' => '',
  'tag_id' => '',
  'author' => '',
  'author_name' => '',
  'feed' => '',
  'tb' => '',
  'paged' => 0,
  'meta_key' => '',
  'meta_value' => '',
  'preview' => '',
  's' => '',
  'sentence' => '',
  'title' => '',
  'fields' => '',
  'menu_order' => '',
  'embed' => '',
  'category__in' => 
  array (
  ),
  'category__not_in' => 
  array (
  ),
  'category__and' => 
  array (
  ),
  'post__in' => 
  array (
  ),
  'post__not_in' => 
  array (
  ),
  'post_name__in' => 
  array (
  ),
  'tag__in' => 
  array (
  ),
  'tag__not_in' => 
  array (
  ),
  'tag__and' => 
  array (
  ),
  'tag_slug__in' => 
  array (
  ),
  'tag_slug__and' => 
  array (
  ),
  'post_parent__in' => 
  array (
  ),
  'post_parent__not_in' => 
  array (
  ),
  'author__in' => 
  array (
  ),
  'author__not_in' => 
  array (
  ),
  'search_columns' => 
  array (
  ),
  'cache_results' => false,
  'suppress_filters' => false,
  'update_post_term_cache' => true,
  'update_menu_item_cache' => false,
  'lazy_load_term_meta' => true,
  'update_post_meta_cache' => true,
  'post_type' => '',
  'nopaging' => false,
  'comments_per_page' => '50',
  'no_found_rows' => false,
  'order' => 'DESC',
)
status_code: 200
body: {"from":0,"size":5,"post_filter":{"bool":{"must":[{"terms":{"post_type.raw":["post"]}},{"terms":{"post_status":["publish"]}}]}},"query":{"more_like_this":{"like":{"_id":1236992},"fields":["post_title","post_content","terms.post_tag.name"],"min_term_freq":1,"max_query_terms":12,"min_doc_freq":1}},"sort":[{"post_date":{"order":"desc"}}]}
result: array (
  'took' => 13,
  'timed_out' => false,
  '_shards' => 
  array (
    'total' => 5,
    'successful' => 5,
    'skipped' => 0,
    'failed' => 0,
  ),
  'hits' => 
  array (
    'total' => 
    array (
      'value' => 0,
      'relation' => 'eq',
    ),
    'max_score' => NULL,
    'hits' => 
    array (
    ),
  ),
)

### https://dev.minsknews.by/v-oktyabrskom-rajone-uzhe-zavershayut-plan-po-kapremontu-zhilyh-domov-za-2023-god (2024-05-03 10:16:39) ###
es_req: POST http://127.0.0.1:9200/devminsknewsby-post-1/_search
request_id: 71ddd4c6dfdd4be28c9fcd4c9460a9e6
query_time: 11.871099472046
wp_args: array (
  'more_like' => 1236992,
  'posts_per_page' => 5,
  'ep_integrate' => true,
  'ignore_sticky_posts' => true,
  'error' => '',
  'm' => '',
  'p' => 0,
  'post_parent' => '',
  'subpost' => '',
  'subpost_id' => '',
  'attachment' => '',
  'attachment_id' => 0,
  'name' => '',
  'pagename' => '',
  'page_id' => 0,
  'second' => '',
  'minute' => '',
  'hour' => '',
  'day' => 0,
  'monthnum' => 0,
  'year' => 0,
  'w' => 0,
  'category_name' => '',
  'tag' => '',
  'cat' => '',
  'tag_id' => '',
  'author' => '',
  'author_name' => '',
  'feed' => '',
  'tb' => '',
  'paged' => 0,
  'meta_key' => '',
  'meta_value' => '',
  'preview' => '',
  's' => '',
  'sentence' => '',
  'title' => '',
  'fields' => '',
  'menu_order' => '',
  'embed' => '',
  'category__in' => 
  array (
  ),
  'category__not_in' => 
  array (
  ),
  'category__and' => 
  array (
  ),
  'post__in' => 
  array (
  ),
  'post__not_in' => 
  array (
  ),
  'post_name__in' => 
  array (
  ),
  'tag__in' => 
  array (
  ),
  'tag__not_in' => 
  array (
  ),
  'tag__and' => 
  array (
  ),
  'tag_slug__in' => 
  array (
  ),
  'tag_slug__and' => 
  array (
  ),
  'post_parent__in' => 
  array (
  ),
  'post_parent__not_in' => 
  array (
  ),
  'author__in' => 
  array (
  ),
  'author__not_in' => 
  array (
  ),
  'search_columns' => 
  array (
  ),
  'cache_results' => false,
  'suppress_filters' => false,
  'update_post_term_cache' => true,
  'update_menu_item_cache' => false,
  'lazy_load_term_meta' => true,
  'update_post_meta_cache' => true,
  'post_type' => '',
  'nopaging' => false,
  'comments_per_page' => '50',
  'no_found_rows' => false,
  'order' => 'DESC',
)
status_code: 200
body: {"from":0,"size":5,"post_filter":{"bool":{"must":[{"terms":{"post_type.raw":["post"]}},{"terms":{"post_status":["publish"]}}]}},"query":{"more_like_this":{"like":{"_id":1236992},"fields":["post_title","post_content","terms.post_tag.name"],"min_term_freq":1,"max_query_terms":12,"min_doc_freq":1}},"sort":[{"post_date":{"order":"desc"}}]}
result: array (
  'took' => 9,
  'timed_out' => false,
  '_shards' => 
  array (
    'total' => 5,
    'successful' => 5,
    'skipped' => 0,
    'failed' => 0,
  ),
  'hits' => 
  array (
    'total' => 
    array (
      'value' => 0,
      'relation' => 'eq',
    ),
    'max_score' => NULL,
    'hits' => 
    array (
    ),
  ),
)


Copy link

github-actions bot commented May 7, 2024

It has been 3 days since more information was requested from you in this issue and we have not heard back. This issue is now marked as stale and will be closed in 3 days, but if you have more information to add then please comment and the issue will stay open.

@github-actions github-actions bot added the stale label May 7, 2024
@rstrah
Copy link
Author

rstrah commented May 8, 2024

i have problem

@github-actions github-actions bot removed the stale label May 9, 2024
@felipeelia
Copy link
Member

@rstrah the query is going through, but not finding anything related. This is the Elasticsearch query:

{
	"from": 0,
	"size": 5,
	"post_filter": {
		"bool": {
			"must": [
				{
					"terms": {
						"post_type.raw": [
							"post"
						]
					}
				},
				{
					"terms": {
						"post_status": [
							"publish"
						]
					}
				}
			]
		}
	},
	"query": {
		"more_like_this": {
			"like": {
				"_id": 1236992
			},
			"fields": [
				"post_title",
				"post_content",
				"terms.post_tag.name"
			],
			"min_term_freq": 1,
			"max_query_terms": 12,
			"min_doc_freq": 1
		}
	},
	"sort": [
		{
			"post_date": {
				"order": "desc"
			}
		}
	]
}

You will need to make sure your content is properly indexed, and that you have content in the "post" post_type with similar title, content, and/or tags of the post with ID 1236992.

If you need to use different fields, you'll need to use the ep_related_posts_fields filter, with something like this:

add_filter(
	'ep_related_posts_fields',
	function( $fields ) {
		$fields = array_merge(
			$fields,
			[ 'post_content_filtered', 'terms.category.name' ]
		);
		return $fields;
	}
);

(more options are available in this gist.)

Copy link

It has been 3 days since more information was requested from you in this issue and we have not heard back. This issue is now marked as stale and will be closed in 3 days, but if you have more information to add then please comment and the issue will stay open.

@github-actions github-actions bot added the stale label May 13, 2024
Copy link

This issue has been automatically closed because there has been no response to our request for more information in the past 3 days. With only the information that is currently available, we are unable to take further action on this ticket. Please reach out if you have found or find the answer we need so that we can investigate further. When the information is ready, you can re-open this ticket to share it with us.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants