Use script fields to filter data

Is possible use a script_fields to filter/find data?

Example:

{
    "script_fields": {
        "my_custom_field": {
            "script": {
                "inline": "int x; x = 0; x = ((doc['deadline'].empty)&&(doc['situation'].== 5))?1:2; return x;
            }
        }
    },
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "fields.my_custom_field": 2
                    }
                }
            ]
        }
    }
}

Scripts fields are for adding dynamic fields to the top N returned by search. Take a look at the script query.

1 Like

Is possible use a stored script inside in a source script?

Something, like:

  "script": {
    "source": "if( my_storedscript == 3){return false} else {return true}"
  }

Unfortunately this isn't possible. Scripts cannot be called nested within another.

Thanks @rjernst.

Another question, i have created two scripts and execute both inside query->bool->must, but the scripts are execute with the condition OR, script1 = true or script2 = true, is possible return just the data when both scripts return true?

Query:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "active": true
          }
        },
        {
          "script": {
            "script": [
              {
                "id": "script_query_1",
                "params": {
                  "list": [
                    1
                  ],
                  "current_timestamp": 1522344053924
                }
              },
              {
                "id": "script_query_2",
                "params": {
                  "list": [
                    1
                  ],
                  "current_timestamp": 1522344053924
                }
              }
            ]
          }
        }
      ]
    }
  },
  "script_fields": {
    "deadline_cumprido": {
      "script": {
        "id": "script_query_1",
        "params": {
          "list": [1],
          "current_timestamp": 1522344053924
        }
      }
    },
    "start_deadline_cumprido": {
      "script": {
        "id": "script_query_2",
        "params": {
          "list": [1],
          "current_timestamp": 1522344053924
        }
      }
    }
  }
}

Result:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 2.0,
    "hits": [
      {
        "_index": "myindex_example",
        "_type": "doc_example",
        "_id": "2648374-14492",
        "_score": 2.0,
        "fields": {
          "script_query_2": [
            true
          ],
          "script_query_1": [
            true
          ]
        }
      },
      {
        "_index": "myindex_example",
        "_type": "doc_example",
        "_id": "2648373-14492",
        "_score": 2.0,
        "fields": {
          "script_query_2": [
            true
          ],
          "script_query_1": [
            true
          ]
        }
      },
      {
        "_index": "myindex_example",
        "_type": "doc_example",
        "_id": "2648375-14492",
        "_score": 2.0,
        "fields": {
          "script_query_2": [
            true
          ],
          "script_query_1": [
            false
          ]
        }
      }
    ]
  }
}

You would need to put each script as a separate script query, inside a must or filter clause of your bool query. I'm actually surprised your example query does not throw an error; it should.

Like this way?

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "active": true
          },
          "script": {
            "script": 
              {
                "id": "script_query_1",
                "params": {
                  "list": [
                    1
                  ],
                  "current_timestamp": 1522344053924
                }
              }
            },
            "script": {
                "script": {
                "id": "script_query_2",
                "params": {
                  "list": [
                    1
                  ],
                  "current_timestamp": 1522344053924
                }
              }
            }
        }
      ]
    }
  },
  "script_fields": {
    "script_query_1_field": {
      "script": {
        "id": "script_query_1",
        "params": {
          "list": [1],
          "current_timestamp": 1522344053924
        }
      }
    },
    "script_query_2_field": {
      "script": {
        "id": "script_query_2",
        "params": {
          "list": [1],
          "current_timestamp": 1522344053924
        }
      }
    }
  }
}

Yes, that looks roughly correct.

Thanks, problem solved.

{
"query": {
    "bool": {
        "must": [
            {
                "term": {
                    "active": true
                }
            },
            {
                "script": {
                    "script": {
                        "id": "script_query_1",
                        "params": {
                            "list": [
                                1
                            ],
                            "current_timestamp": 1522344053924
                        }
                    }
                }
            },
            {
                "script": {
                    "script": {
                        "id": "script_query_2",
                        "params": {
                            "list": [
                                1
                            ],
                            "current_timestamp": 1522344053924
                        }
                    }
                }
            }
        ]
    }
},

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.