69976

Check value exists in params array using ElasticSearch painless script

Question:

I am new to ElasticSearch. I am working on a project where I have to search listings which matches the input http://foo.bar/search?listing=123,456. I have built an array which key and value are the same and representing the listing ids. I have tried to run the following example but it failed near contains. The script is built for painless language. However, I always get the runtime error.

I cannot get this to work, what could I be doing wrong?

'function_score' => [ 'query' => $query, 'score_mode' => 'sum', 'functions' => [[ 'script_score' => [ 'script' => [ 'params' => ['listing' => [123 => 123, 456 => 456]], 'source' => "(params.listing.contains(doc['id'].value) ? Math.pow(3, 3) : 0)", ], ], ]], ],

Answer1:

You can make use of the below query. You can check the script part as how what you need has been implemented.

POST <your_index_name>/_search { "query": { "function_score": { "score_mode": "sum", "query": { "match_all": {} }, "functions": [ { "script_score": { "script": { "source": """ boolean allValues = false; for(int i=0; i<params.value.size(); i++){ if(doc['id'].value.contains(params.value.get(i))){ allValues = true; }else{ allValues = false; break; } } if(allValues){ return Math.pow(3,3) } else { return 0; } """, "params":{ "value": ["123","456"] } } } } ] } } }

What the query does is, it checks to see if all the values present in params i.e 123 & 456 is present in the id field, if it does, it would go ahead and calculate score accordingly.

Not that id field is a keyword when I tested the query at my end.

You can use the query accordingly in your php script. Let me know if this helps!

Recommend

  • Weighted search on one field and a normal search on other field
  • Elasticsearch painless script not replacing the nested object field value using if condition
  • How to apply a specific shape to a div?
  • java bytecode editor? [closed]
  • GIT or SVN or… for front-end developers
  • Doxygen usage of \\includedoc
  • How to concat Pandas dataframe columns
  • runtime error when linking ffmpeg libraries in qt creator
  • SQL - Select lowest values with group by and order by?
  • Ruby on Rails App deployed to heroku showing “We're sorry, but something went wrong”
  • Detecting null parameter in preprocessor macro
  • R convert summary result (statistics with all dataframe columns) into dataframe
  • NUnit 3.0 TestCase const custom object arguments
  • Approximate Order-Preserving Huffman Code
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • Problems installing Yesod for Haskell
  • Unable to install Git-core+svn by MacPorts
  • What does 'Language neutral' mean with regard to MAKELANGID?
  • Grails calculated field in SQL
  • Android activity accessing service's static reference before the service is ready
  • VSO Build — Response status code does not indicate success: 404 (Not Found)
  • Switching to Release Build causes runtime error in Web Reference
  • DomPDF {PAGE_NUM} not on first page
  • ilmerge with a PFX file
  • Javascript simulate pressing enter in input box
  • Is possible to count alias result on mysql
  • Importing jscolor library in angular 2
  • R: gsub and capture
  • SVN: Merging two branches together
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Comma separated Values
  • A cron job substitute?
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • How to load view controller without button in storyboard?