Skip to content

Mixing field collapsing and suggestions can lead to errors #70328

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

Closed
cbuescher opened this issue Mar 11, 2021 · 1 comment · Fixed by #70414
Closed

Mixing field collapsing and suggestions can lead to errors #70328

cbuescher opened this issue Mar 11, 2021 · 1 comment · Fixed by #70414
Assignees
Labels
>bug :Search/Search Search-related issues that do not fall into other categories Team:Search Meta label for search team v7.11.0 v8.0.0-alpha1

Comments

@cbuescher
Copy link
Member

There seem to be certain circumstances when requesting completion suggestions along with a regular search that uses the "collapse" feature can lead to "array_index_out_of_bounds_exception" in the fetch phase.

I was able to reproduce the issue on master and 7.11 with the following script:

DELETE test

PUT test
{
  "mappings": {
    "properties": {
      "collapse_field": {
        "type": "keyword"
      },
      "suggest_field": {
        "type": "completion",
        "analyzer": "whitespace"
      }
    }
  },
  "settings": {
    "number_of_shards": 2
  }
}

POST /test/_bulk?refresh=true
{ "index" : { "_id" : 1 } }
{ "collapse_field" : "aaa", "suggest_field" : "suggestion1" }
{ "index" : { "_id" : 2 } }
{ "collapse_field" : "bbb", "suggest_field" : "suggestion2" }
{ "index" : { "_id" : 3 } }
{ "collapse_field" : "bbb", "suggest_field" : "suggestion3" }
{ "index" : { "_id" : 4 } }
{ "collapse_field" : "ccc", "suggest_field" : "suggestion4" }
{ "index" : { "_id" : 5 } }
{ "collapse_field" : "ccc", "suggest_field" : "suggestion5" }

POST /test/_search
{
  "query": {"match_all": {}}, 
  "suggest": {
    "my_suggestion": {
      "prefix": "sug",
      "completion": {
        "field": "suggest_field",
        "size": 1
      }
    }
  },
  "collapse": {
    "field": "collapse_field"
  },
  "from": 3,
  "size": 1
}

The request returns

{
  "error" : {
    "root_cause" : [ ],
    "type" : "search_phase_execution_exception",
    "reason" : "",
    "phase" : "fetch",
    "grouped" : true,
    "failed_shards" : [ ],
    "caused_by" : {
      "type" : "array_index_out_of_bounds_exception",
      "reason" : "Index 1 out of bounds for length 1"
    }
  },
  "status" : 500
}

My guess is this only happens with at least 2 shards, but I might be mistaken. When I omitted the document ids, reproduction sometimes worked, sometimes failed, so I assume the distribution of documents on the shards also matters.

Stack trace of the exception is coming from SearchPhaseController.merge where we are exceeding the array bounds:

Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
        at org.elasticsearch.action.search.SearchPhaseController.merge(SearchPhaseController.java:272) ~[elasticsearch-8.0.0-SNAPSHOT.jar:8.0.0-SNAPSHOT]
        at org.elasticsearch.action.search.FetchSearchPhase.moveToNextPhase(FetchSearchPhase.java:217) ~[elasticsearch-8.0.0-SNAPSHOT.jar:8.0.0-SNAPSHOT]
        at org.elasticsearch.action.search.FetchSearchPhase.lambda$innerRun$1(FetchSearchPhase.java:101) ~[elasticsearch-8.0.0-SNAPSHOT.jar:8.0.0-SNAPSHOT]
        at org.elasticsearch.action.search.CountedCollector.countDown(CountedCollector.java:40) ~[elasticsearch-8.0.0-SNAPSHOT.jar:8.0.0-SNAPSHOT]
        at org.elasticsearch.action.search.ArraySearchPhaseResults.consumeResult(ArraySearchPhaseResults.java:35) ~[elasticsearch-8.0.0-SNAPSHOT.jar:8.0.0-SNAPSHOT]
        at org.elasticsearch.action.search.CountedCollector.onResult(CountedCollector.java:48) ~[elasticsearch-8.0.0-SNAPSHOT.jar:8.0.0-SNAPSHOT]
        at org.elasticsearch.action.search.FetchSearchPhase$2.innerOnResponse(FetchSearchPhase.java:171) ~[elasticsearch-8.0.0-SNAPSHOT.jar:8.0.0-SNAPSHOT]
@cbuescher cbuescher added >bug :Search/Search Search-related issues that do not fall into other categories v8.0.0 needs:triage Requires assignment of a team area label v7.11.0 labels Mar 11, 2021
@cbuescher cbuescher self-assigned this Mar 11, 2021
@elasticmachine elasticmachine added the Team:Search Meta label for search team label Mar 11, 2021
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-search (Team:Search)

@cbuescher cbuescher removed the needs:triage Requires assignment of a team area label label Mar 15, 2021
cbuescher pushed a commit to cbuescher/elasticsearch that referenced this issue Mar 15, 2021
Under certain circumstances we can get "array_index_out_of_bounds" exceptions in
the fetch phase when merging comletion suggestion results with additonal field
collapsing in place. We store the ScoreDoc array in the SortedTopDocs score docs
contain both regular search hits and completion suggestion results added in
SearchPhaseController#sortDocs, so when merging these we need to know the
correct array offset where the completion results begin. This is based on the
hits length calculated in SearchPhaseController#getHits, which doesn't take into
account that there might be suggestion results present when calculating the
number of hits. This change adds the number of suggestions to SortedTopDocs in
order to be able to later account for it when calculating the hits.

Closes elastic#70328
cbuescher pushed a commit that referenced this issue Mar 22, 2021
Under certain circumstances we can get "array_index_out_of_bounds" exceptions in
the fetch phase when merging comletion suggestion results with additonal field
collapsing in place. We store the ScoreDoc array in the SortedTopDocs score docs
contain both regular search hits and completion suggestion results added in
SearchPhaseController#sortDocs, so when merging these we need to know the
correct array offset where the completion results begin. This is based on the
hits length calculated in SearchPhaseController#getHits, which doesn't take into
account that there might be suggestion results present when calculating the
number of hits. This change adds the number of suggestions to SortedTopDocs in
order to be able to later account for it when calculating the hits.

Closes #70328
cbuescher pushed a commit to cbuescher/elasticsearch that referenced this issue Mar 22, 2021
Under certain circumstances we can get "array_index_out_of_bounds" exceptions in
the fetch phase when merging comletion suggestion results with additonal field
collapsing in place. We store the ScoreDoc array in the SortedTopDocs score docs
contain both regular search hits and completion suggestion results added in
SearchPhaseController#sortDocs, so when merging these we need to know the
correct array offset where the completion results begin. This is based on the
hits length calculated in SearchPhaseController#getHits, which doesn't take into
account that there might be suggestion results present when calculating the
number of hits. This change adds the number of suggestions to SortedTopDocs in
order to be able to later account for it when calculating the hits.

Closes elastic#70328
cbuescher pushed a commit to cbuescher/elasticsearch that referenced this issue Mar 22, 2021
Under certain circumstances we can get "array_index_out_of_bounds" exceptions in
the fetch phase when merging comletion suggestion results with additonal field
collapsing in place. We store the ScoreDoc array in the SortedTopDocs score docs
contain both regular search hits and completion suggestion results added in
SearchPhaseController#sortDocs, so when merging these we need to know the
correct array offset where the completion results begin. This is based on the
hits length calculated in SearchPhaseController#getHits, which doesn't take into
account that there might be suggestion results present when calculating the
number of hits. This change adds the number of suggestions to SortedTopDocs in
order to be able to later account for it when calculating the hits.

Closes elastic#70328
cbuescher pushed a commit that referenced this issue Mar 22, 2021
Under certain circumstances we can get "array_index_out_of_bounds" exceptions in
the fetch phase when merging comletion suggestion results with additonal field
collapsing in place. We store the ScoreDoc array in the SortedTopDocs score docs
contain both regular search hits and completion suggestion results added in
SearchPhaseController#sortDocs, so when merging these we need to know the
correct array offset where the completion results begin. This is based on the
hits length calculated in SearchPhaseController#getHits, which doesn't take into
account that there might be suggestion results present when calculating the
number of hits. This change adds the number of suggestions to SortedTopDocs in
order to be able to later account for it when calculating the hits.

Closes #70328
cbuescher pushed a commit that referenced this issue Mar 22, 2021
Under certain circumstances we can get "array_index_out_of_bounds" exceptions in
the fetch phase when merging comletion suggestion results with additonal field
collapsing in place. We store the ScoreDoc array in the SortedTopDocs score docs
contain both regular search hits and completion suggestion results added in
SearchPhaseController#sortDocs, so when merging these we need to know the
correct array offset where the completion results begin. This is based on the
hits length calculated in SearchPhaseController#getHits, which doesn't take into
account that there might be suggestion results present when calculating the
number of hits. This change adds the number of suggestions to SortedTopDocs in
order to be able to later account for it when calculating the hits.

Closes #70328
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>bug :Search/Search Search-related issues that do not fall into other categories Team:Search Meta label for search team v7.11.0 v8.0.0-alpha1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants