user320550
user320550

Reputation: 1185

Elastic Search search related

Let's say we have the following 3 documents stored in an ES Type:

Document 1:

{
  foo: [
    "c"
  ],
  bar: "bar1"
}

Document 2:

{
  foo: [
    "a"
    "b"
  ],
  bar: "bar2"
}

Document 3:

{
  foo: [
    "a",
    "b",
    "c"
  ],
  bar: "bar3"
}

I want to do a search in ES such that given the input foo: ['a', 'b', 'c', 'd'], the result should return only Document 3. This is basically saying, find that single document which has the max number of entries in the foo array.

Upvotes: 0

Views: 39

Answers (1)

Vigikaran
Vigikaran

Reputation: 725

You can use should and size is 1 so ideally the document 3 got high relevance and should be in the top. and size 1 will return the most relevant document

"size" : 1,
"query": {
    "bool": {
      "should": [
        { "term": {"foo": "a"}},
        { "term": {"foo": "b"}},
        { "term": {"foo": "c"}},
        { "term": {"foo": "d"}},
      ]
}

Upvotes: 1

Related Questions