1

Is it possible to add a custom script field that is a Boolean and returns true if the document's id exists in an array that is sent as a param?

Something like this https://gist.github.com/2437370

What would be the correct way to do this with mvel?

Update: Having trouble getting it to work as specified in Imotov's answer.

Mapping:

Sort:

:sort=>{:_script=>{:script=>"return friends_visits_ids.contains(_fields._id.value)", :type=>"string", :params=>{:friends_visits_ids=>["4f8d425366eaa71471000011"]}, :order=>"asc"}}}

place: { properties: { _id: { index: "not_analyzed", store: "yes" }, } }

I don't get any errors, the documents just doesn't get sorted right.

Update 2

Oh, and I do get this back on the documents:

"sort"=>["false"]

1 Answer 1

5

You were on the right track. It just might be more efficient to store list of ids in a map instead of an array if this list is large.

"sort" : {
  "_script" : {
    "script" : "return friends_visits_ids.containsKey(_fields._id.value)",
    "type" : "string",
    "params": {
      "friends_visits_ids": { "1" : {}, "2" : {}, "4" : {}}
    }
  }
}

Make sure that id field is stored. Otherwise _fields._id.value will return null for all records.

Sign up to request clarification or add additional context in comments.

4 Comments

Hey thanks for your answer, getting this error though: Query Failed [Failed to execute main query]]; nested: PropertyAccessException[[Error: unable to resolve method: java.util.ArrayList.containsKey() EDIT: NVM, I still had it as an array.
You can use an array, if it's small enough. You just need to replace containsKey() with contains().
What would be small enough? < 50?
It depends on the data in your result list. It's basically overhead of building HashMap vs overhead of scanning ArrayList and how it all compares to pulling the record from the index. The hashmap is built once, the array is scanned for every record in the result list. With 50 records HashMap will be 10-20 times faster. But then for 1mln records we are talking about 100ms vs 10 ms difference. So, if it was <5, I would guess you wouldn't see much of the difference at all. With <50, I would, probably, test it, just in case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.