Skip to content

help, how to get results when using scripts_field #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
stoneriverheart opened this issue Oct 2, 2017 · 5 comments
Closed

help, how to get results when using scripts_field #20

stoneriverheart opened this issue Oct 2, 2017 · 5 comments

Comments

@stoneriverheart
Copy link

If scripts_field is used, how can we get results of fields execution?

query{
  profile(
    opts: {
      body: {
        script_fields: {
          results:{
            script:{
              lang: "painless"
              inline: "Date n = new Date(); Date b = new Date(doc['birthday'].value); DateFormat f = new SimpleDateFormat('yyyy-MM-dd'); return Period.between(LocalDate.parse(f.format(b)), LocalDate.parse(f.format(n))).getYears();"
            }
          }
        }
    query: {
      bool: {
        must: {
      term: {
        systemId__keyword: {
          value: "test-xxxxxx"
        }
      }
        }
      }
    }
      }
    }
  ) {
    took
    count
    hits {
      _source {
        fields {
          results <--no hits.
        }
      }
    }
  }
}
@nodkz
Copy link
Member

nodkz commented Oct 4, 2017

Please provide trace log from elasticClient when you execute this query:

elasticsearch.Client({
  ...
  log: 'trace',
});

This package very hard to debug without free public elastic search server. So let do it with console logs.

@stoneriverheart
Copy link
Author

use elasticsearch version 5.5.
console trace logs.

Elasticsearch DEBUG: 2017-10-05T03:09:58Z
  starting request {
    "method": "POST",
    "path": "/escompose20171005/escompose/_search",
    "body": {
      "script_fields": {
        "results": {
          "script": {
            "lang": "painless",
            "inline": "Date n = new Date(); Date b = new Date(doc['birthday'].value); DateFormat f = new SimpleDateFormat('yyyy-MM-dd'); return Period.between(LocalDate.parse(f.format(b)), LocalDate.parse(f.format(n))).getYears();"
          }
        }
      },
      "_source": [
        "name",
        "birthday"
      ],
      "query": {
        "match_all": {}
      }
    },
    "query": {}
  }
  

Elasticsearch TRACE: 2017-10-05T03:09:58Z
  -> POST https://xxxxxxx/escompose20171005/escompose/_search
  {
    "script_fields": {
      "results": {
        "script": {
          "lang": "painless",
          "inline": "Date n = new Date(); Date b = new Date(doc[\u0027birthday\u0027].value); DateFormat f = new SimpleDateFormat(\u0027yyyy-MM-dd\u0027); return Period.between(LocalDate.parse(f.format(b)), LocalDate.parse(f.format(n))).getYears();"
        }
      }
    },
    "_source": [
      "name",
      "birthday"
    ],
    "query": {
      "match_all": {}
    }
  }
  <- 200
  {
    "took": 0,
    "timed_out": false,
    "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
    },
    "hits": {
      "total": 2,
      "max_score": 1,
      "hits": [
        {
          "_index": "escompose20171005",
          "_type": "escompose",
          "_id": "01",
          "_score": 1,
          "_source": {
            "birthday": "1973-09-28T00:00:00.000Z",
            "name": "aaaa"
          },
          "fields": {
            "results": [
              44
            ]
          }
        },
        {
          "_index": "escompose20171005",
          "_type": "escompose",
          "_id": "02",
          "_score": 1,
          "_source": {
            "birthday": "1970-10-28T00:00:00.000Z",
            "name": "bbbb"
          },
          "fields": {
            "results": [
              46
            ]
          }
        }
      ]
    }
  }

Elasticsearch DEBUG: 2017-10-05T03:09:58Z
  Request complete

script_fields is hit item top level "fields". support it?

@stoneriverheart
Copy link
Author

append return value "fields" to SearchHitItem.js in getSearchHitItemTC.

edit.

export function getSearchHitItemTC(opts: SearchOptsT = {}): TypeComposer {
  const name = getTypeName('SearchHitItem', opts);
    :
    :
        // return sort values for search_after
        sort: 'JSON',

        // ↓add  
        fields: 'JSON',

      },
    })
  );
}

graphql query add hits item in "fields" .

{ escompose(  query: { match_all: {} } opts: {body: {script_fields: {results: {script: {lang: \\"painless\\" inline: \\"Date n = new Date(); Date b = new Date(doc[\'birthday\'].value); DateFormat f = new SimpleDateFormat(\'yyyy-MM-dd\'); return Period.between(LocalDate.parse(f.format(b)), LocalDate.parse(f.format(n))).getYears();\\"}}} } } ) {
 took count hits { _source { name birthday  } sort fields } } }

run, elasticsearch trace log.

Elasticsearch DEBUG: 2017-10-05T03:57:34Z
  starting request {
    "method": "POST",
    "path": "/escompose20171005/escompose/_search",
    "body": {
      "script_fields": {
        "results": {
          "script": {
            "lang": "painless",
            "inline": "Date n = new Date(); Date b = new Date(doc['birthday'].value); DateFormat f = new SimpleDateFormat('yyyy-MM-dd'); return Period.between(LocalDate.parse(f.format(b)), LocalDate.parse(f.format(n))).getYears();"
          }
        }
      },
      "_source": [
        "name",
        "birthday"
      ],
      "query": {
        "match_all": {}
      }
    },
    "query": {}
  }
  

Elasticsearch TRACE: 2017-10-05T03:57:34Z
  -> POST https://xxxxxxx/escompose20171005/escompose/_search
  {
    "script_fields": {
      "results": {
        "script": {
          "lang": "painless",
          "inline": "Date n = new Date(); Date b = new Date(doc[\u0027birthday\u0027].value); DateFormat f = new SimpleDateFormat(\u0027yyyy-MM-dd\u0027); return Period.between(LocalDate.parse(f.format(b)), LocalDate.parse(f.format(n))).getYears();"
        }
      }
    },
    "_source": [
      "name",
      "birthday"
    ],
    "query": {
      "match_all": {}
    }
  }
  <- 200
  {
    "took": 0,
    "timed_out": false,
    "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
    },
    "hits": {
      "total": 2,
      "max_score": 1,
      "hits": [
        {
          "_index": "escompose20171005",
          "_type": "escompose",
          "_id": "01",
          "_score": 1,
          "_source": {
            "birthday": "1973-09-28T00:00:00.000Z",
            "name": "aaaa"
          },
          "fields": {
            "results": [
              44
            ]
          }
        },
        {
          "_index": "escompose20171005",
          "_type": "escompose",
          "_id": "02",
          "_score": 1,
          "_source": {
            "birthday": "1970-10-28T00:00:00.000Z",
            "name": "bbbb"
          },
          "fields": {
            "results": [
              46
            ]
          }
        }
      ]
    }
  }

Elasticsearch DEBUG: 2017-10-05T03:57:34Z
  Request complete

{
    "statusCode": 200,
    "headers": {
        "Access-Control-Allow-Origin": "*"
    },
    "body": "{\"data\":{\"escompose\":{\"took\":0,\"count\":2,\"hits\":[{\"_source\":{\"name\":\"aaaa\",\"birthday\":\"1973-09-28T00:00:00.000Z\"},\"sort\":null,\"fields\":{\"results\":[44]}},{\"_source\":{\"name\":\"bbbb\",\"birthday\":\"1970-10-28T00:00:00.000Z\"},\"sort\":null,\"fields\":{\"results\":[46]}}]}}}"
}

success getting fields values.

@nodkz nodkz closed this as completed in 97cd92a Oct 5, 2017
@nodkz
Copy link
Member

nodkz commented Oct 5, 2017

Thanks for debugging!

After several minutes will be published a new package.
Please try it and confirm that it solves your problem.

@stoneriverheart
Copy link
Author

Thank you, new package release!
The problem was solved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants