Skip to content

Search with MoreLikeThisQuery should use Pageable. #1787

New issue

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

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

Already on GitHub? # to your account

Closed
xwlcn opened this issue Apr 23, 2021 · 1 comment · Fixed by #1789
Closed

Search with MoreLikeThisQuery should use Pageable. #1787

xwlcn opened this issue Apr 23, 2021 · 1 comment · Fixed by #1789
Labels
type: bug A general bug

Comments

@xwlcn
Copy link

xwlcn commented Apr 23, 2021

Step 1:

MoreLikeThisQuery query = new MoreLikeThisQuery();
query.setId(stringIdRepresentation(extractIdFromBean(entity)));
query.setPageable(pageable);    //set in this, but don't use

if (fields != null) {
	query.addFields(fields);
}

SearchHits<T> searchHits = execute(operations -> operations.search(query, entityClass, getIndexCoordinates()));
AggregatedPage<SearchHit<T>> page = SearchHitSupport.page(searchHits, pageable);
return (Page<T>) SearchHitSupport.unwrapSearchHits(page);

Step 2: Look into<T> SearchHits<T> search(MoreLikeThisQuery query, Class<T> clazz, IndexCoordinates index);:

public <T> SearchHits<T> search(MoreLikeThisQuery query, Class<T> clazz, IndexCoordinates index) {

	Assert.notNull(query.getId(), "No document id defined for MoreLikeThisQuery");

	MoreLikeThisQueryBuilder moreLikeThisQueryBuilder = requestFactory.moreLikeThisQueryBuilder(query, index);
	return search(new NativeSearchQueryBuilder().withQuery(moreLikeThisQueryBuilder).build(), clazz, index);
}

Step 3: It seems no problem, then look intorequestFactory.moreLikeThisQueryBuilder(query, index);:

public MoreLikeThisQueryBuilder moreLikeThisQueryBuilder(MoreLikeThisQuery query, IndexCoordinates index) {

	String indexName = index.getIndexName();
	MoreLikeThisQueryBuilder.Item item = new MoreLikeThisQueryBuilder.Item(indexName, query.getId());

	String[] fields = query.getFields().toArray(new String[] {});

	MoreLikeThisQueryBuilder moreLikeThisQueryBuilder = QueryBuilders.moreLikeThisQuery(fields, null,
			new MoreLikeThisQueryBuilder.Item[] { item });

	if (query.getMinTermFreq() != null) {
		moreLikeThisQueryBuilder.minTermFreq(query.getMinTermFreq());
	}

	if (query.getMaxQueryTerms() != null) {
		moreLikeThisQueryBuilder.maxQueryTerms(query.getMaxQueryTerms());
	}

	if (!isEmpty(query.getStopWords())) {
		moreLikeThisQueryBuilder.stopWords(query.getStopWords());
	}

	if (query.getMinDocFreq() != null) {
		moreLikeThisQueryBuilder.minDocFreq(query.getMinDocFreq());
	}

	if (query.getMaxDocFreq() != null) {
		moreLikeThisQueryBuilder.maxDocFreq(query.getMaxDocFreq());
	}

	if (query.getMinWordLen() != null) {
		moreLikeThisQueryBuilder.minWordLength(query.getMinWordLen());
	}

	if (query.getMaxWordLen() != null) {
		moreLikeThisQueryBuilder.maxWordLength(query.getMaxWordLen());
	}

	if (query.getBoostTerms() != null) {
		moreLikeThisQueryBuilder.boostTerms(query.getBoostTerms());
	}

	return moreLikeThisQueryBuilder;
}

Ok, you can see MoreLikeThisQuery's pageable property is missing, I found this issue in the 3.x version, and it still exists in the 4.x version.

In step 2, modify below

return search(new NativeSearchQueryBuilder().withQuery(moreLikeThisQueryBuilder).build(), clazz, index);

to

return search(new NativeSearchQueryBuilder().withPageable(query.getPageable()).withQuery(moreLikeThisQueryBuilder).build(), clazz, index);

can fix it.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 23, 2021
@sothawo sothawo changed the title searchSimilar page not work `org.springframework.data.elasticsearch.core.SearchOperations#search(org.springframework.data.elasticsearch.core.query.MoreLikeThisQuery, java.lang.Class<T>, org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) Apr 24, 2021
@sothawo sothawo changed the title `org.springframework.data.elasticsearch.core.SearchOperations#search(org.springframework.data.elasticsearch.core.query.MoreLikeThisQuery, java.lang.Class<T>, org.springframework.data.elasticsearch.core.mapping.IndexCoordinates) SearchOperations.search(MoreLikeThisQuery, Class<T>, IndexCoordinates) should use Pageable from the query Apr 24, 2021
@sothawo sothawo added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Apr 24, 2021
@sothawo sothawo changed the title SearchOperations.search(MoreLikeThisQuery, Class<T>, IndexCoordinates) should use Pageable from the query Search with MoreLikeThisQuery uses Pageable. Apr 26, 2021
@sothawo sothawo changed the title Search with MoreLikeThisQuery uses Pageable. Search with MoreLikeThisQuery should use Pageable. Apr 26, 2021
@sothawo sothawo added this to the 4.3 M1 (2021.1.0) milestone Apr 26, 2021
sothawo added a commit that referenced this issue Apr 26, 2021
Original Pull Request #1789
Closes #1787

(cherry picked from commit a2ca312)
(cherry picked from commit 85af546)
(cherry picked from commit 2cb9e30)
sothawo added a commit that referenced this issue Apr 26, 2021
Original Pull Request #1789
Closes #1787

(cherry picked from commit a2ca312)
sothawo added a commit that referenced this issue Apr 26, 2021
Original Pull Request #1789
Closes #1787

(cherry picked from commit a2ca312)
(cherry picked from commit 85af546)

# Conflicts:
#	src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java
@sothawo
Copy link
Collaborator

sothawo commented Apr 26, 2021

implemented in main branch and backported to 4.2.x, 4.1.x and 4.0.x

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants