Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This toolkit element allows you to use regex, javascript, or xpath to create metadata objects (that can then be used to generate entities or assocations by subsequent pipeline elements).

This page has been broken down into the following sections for ease of localization.

Table of Contents

 

Format

TODO Convert to JSON

Code Block
{
	"display": string,
	"contentMetadata": [
		{} // see MetadataSpecPojo below
	]
}
//////////////////////////////////
 
	public static class MetadataSpecPojo {
		public String fieldName; // Any string, the key for generated array in "doc.metadata"
		public String scriptlang; // One of "javascript", "regex", "xpath"
		public String script; // The script that will generate the array in "doc.metadata" (under fieldName)
		public String flags; // Standard Java regex field (regex/xpath only), plus "H" to decode HTML, "D": will allow duplicate strings (by default they are de-duplicated), plus the following custom flags:
								// For javascript (defaults to "t" if none specified), "t" the script receives the doc fullText ("text"), "d" the script receives the entire doc (_doc), "m" the script receives the doc.metadata (_metadata)
								// For xpath: "o": if the XPath expression points to an HTML (/XML) object, then this object is converted to JSON and stored as an object in the corresponding metadata field array. (Can also be done via the deprecated "groupNum":-1)
		public String replace; // Replacement string for regex/xpath+regex matches, can include capturing groups as $1 etc
		public Boolean store; // Whether this field should be stored in the DB or discarded after the harvetharvest processing
		public Boolean index; // Whether this field should be full-text indexed or just stored in the DB
	}

...

ParameterDescriptionNoteData Types
fieldName
Any string, the key for generated array in "doc.metadata"
  
scriptlang javascript, regex or xpath.  
script script that will generate the array.  
flags

For javascript (defaults to "t" if none specified), "t" the script receives the doc fullText ("text"), "d" the script receives the entire doc (_doc), "m" the script receives the doc.metadata
 

There are also a few flags that provide additional variables in the javascript:

  • "m" to get "_doc.metadata", written into the variable "_metadata"
    • (for example this flag can be used to copy a subset of the fields from one fieldname to another, before using the "metadataFields" field in the "structuredAnalysis" object to delete the larger field)
  • "d" to get "_doc", written into the variable "_doc",
  • "t" to return the full text of the document into "text". 
    • If the "flags" field is not specified, this is returned by default. If the "flags" field is specified, then "t" must be included or the "text" variable is not populated.
  • 'H': HTML code the resulting field (same as for regex/xpath except that it only applied to single value returns. To decode HTML in more complex expressions/arrays, you can always call org.apache.commons.lang.StringEscapeUtils.unescapeHtml('' + js_string)
  

For xpath: "o": if the XPath expression points to an HTML (/XML) object, then this object is converted to JSON and stored as an object in the corresponding metadata field array.

For reference, here is the complete set of flags for xpath (and regex, except for "O"):

  • 'H': will HTML-decode resulting fields. (Eg "&" -> "&")
  • 'o': if  the XPath expression points to an HTML (/XML) object, then this object is converted to JSON and stored as an object in the corresponding metadata field array. (Can also be done via the deprecated "groupNum":-1)
  • 'x': if the XPath expression points to an HTML (/XML) object, then the XML of the object is displayed with no decoding (eg stripping of fields)
  • 'D': described above 
  • 'c': if set then fields with the same name are chained together (otherwise they will all append their results to the field within metadata)
  
replace   
store   
index   

...

Neither regex nor javascript are well suited for extracting fields from HTML and XML (particularly since the current Javascript engine, the Java version of Rhino, does not support DOM).

The following example shows how Xpath can be used  to extract embedded HTML from an XML document for the creation of entities and associations.

 

...

XML

The example XML data contains some severe weather incident reports.  For each report, we would like to extract the embedded HTML to create entities.

Code Block
         <![CDATA[
<table>
<tr><td>ztime: </td><td>2012-10-29T00:32:00Z</td></tr>
<tr><td>id: </td><td>1553661</td></tr>
<tr><td>event: </td><td>NON-TSTM WND GST</td></tr>
<tr><td>magnitude: </td><td>53.0</td></tr>
<tr><td>city: </td><td>NORFOLK NAS</td></tr>
<tr><td>county: </td><td>CITY OF NORFOLK</td></tr>
<tr><td>state: </td><td>VA</td></tr>
<tr><td>source: </td><td>ASOS</td></tr>
</table>
<iframe src="http://www.ncdc.noaa.gov/swdiws/csv/plsr/id=1553661" />          ]]>
        </description>
      <Point>
                <coordinates>-76.2800,36.9300,0        </coordinates>
      </Point>
        <TimeSpan>
          <begin>          2012-10-29T00:32:00Z          </begin>
          <end>          2012-10-29T02:56:00Z          </end>
        </TimeSpan>
      </Placemark>
      <Placemark>
        <styleUrl>#style_0</styleUrl>
        <description>
          <![CDATA[
<table>
<tr><td>ztime: </td><td>2012-10-29T00:32:00Z</td></tr>
<tr><td>id: </td><td>1550634</td></tr>
<tr><td>event: </td><td>NON-TSTM WND GST</td></tr>
<tr><td>magnitude: </td><td>53.0</td></tr>
<tr><td>city: </td><td>NORFOLK NAS</td></tr>
<tr><td>county: </td><td>CITY OF NORFOLK</td></tr>
<tr><td>state: </td><td>VA</td></tr>
<tr><td>source: </td><td>ASOS</td></tr>
</table>
<iframe src="http://www.ncdc.noaa.gov/swdiws/csv/plsr/id=1550634" />          ]]>
        </description>
      <Point>
                <coordinates>-76.2800,36.9300,0        </coordinates>
      </Point>
        <TimeSpan>
          <begin>          2012-10-29T00:32:00Z          </begin>
          <end>          2012-10-29T02:56:00Z          </end>
        </TimeSpan>
      </Placemark>
      <Placemark>
        <styleUrl>#style_0</styleUrl>
        <description>

 

Source

In the example source below the contentMetadata block is configured to create two metadata fields: "url" and "info."

Both "url" and "info" will be JSON objects and will be stored in the corresponding metadata field array.

"url" and "info" can then be used as variables in scripting for entities and associations.  For example doc.metadata.info and doc.metadata.url can be included in scripts using the $SCRIPT convention, in order to create entities such as "Weather", "City", and "Country"

 
Code Block
 },
        {
            "contentMetadata": [
                {
                    "fieldName": "url",
                    "flags": "o",
                    "index": false,
                    "script": "//iframe",
                    "scriptlang": "xpath",
                    "store": true
                },
                {
                    "fieldName": "info",
                    "flags": "o",
                    "index": false,
                    "script": "//table/*",
                    "scriptlang": "xpath",
                    "store": true
                }

 

Example Entities:

Code Block
 "display": "",
            "entities": [
                {
                    "creationCriteriaScript": "$SCRIPT( if(_doc.metadata.info[0].tbody.tr[2].td[1] == null) return false; else return true; )",
                    "dimension": "What",
                    "disambiguated_name": "$SCRIPT( return _doc.metadata.info[0].tbody.tr[2].td[1];)",
                    "type": "Weather",
                    "useDocGeo": true
                },
                {
                    "creationCriteriaScript": "$SCRIPT( if(_doc.metadata.info[0].tbody.tr[4].td[1] == null) return false; else return true; )",
                    "dimension": "What",
                    "disambiguated_name": "$SCRIPT(return _doc.metadata.info[0].tbody.tr[4].td[1]; )",
                    "type": "City",
                    "useDocGeo": false
                },
                {
                    "creationCriteriaScript": "$SCRIPT( if(_doc.metadata.info[0].tbody.tr[5].td[1] == null) return false; else return true; )",
                    "dimension": "What",
                    "disambiguated_name": "$SCRIPT( return _doc.metadata.info[0].tbody.tr[5].td[1];)",
                    "type": "County",
                    "useDocGeo": false
                },

 

 

Panel

Footnotes:

Legacy documentation:

Legacy documentation:

...