0

I have an index with a field chunks with keyword type which is just an a list of keyword. When I search through I do something like

{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "chunks": "chunk1"
          }
        },
        {
          "term": {
            "chunks": "chunk2"
          }
        }
      ]
    }
  }
}

So I can retrieve all documents where there are both "chunk1" and "chunk2" inside the chunks field. However what if I care about the order? My solution is too have a script like

String[] chunks = doc['chunks'];
int c = 0;
String chunk = params.chunks[0];
for (int i = 0; i < chunks.length; ++i) {
  if (chunk == chunks[i]) {
    if (++c == params.chunks.length) {
      return true;
    }
    chunk = params.chunks[c];
  }
}
return false;

where params.chunks is something like ["chunk1", "chunk2"]. The problem is the doc['chunks'] is unordered, while params._source isn't allowed from _search context.

It should be possible somehow, because Elastic itself has similar functionality for multitext search, so I can emulate the same field structure.

Ximik
  • 2,435
  • 3
  • 27
  • 53
  • Possible duplicate of [Does Elasticsearch keep an order of multi-value fields?](https://stackoverflow.com/questions/34755389/does-elasticsearch-keep-an-order-of-multi-value-fields) – Pardeep Singh Mar 21 '18 at 07:13
  • Of course, it doesn't. It's in the documentation. – Ximik Mar 22 '18 at 10:03

0 Answers0