Skip to content
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

Reversed logic in the JSON field name matcher #33

Open
paulcappadona opened this issue Nov 7, 2018 · 2 comments
Open

Reversed logic in the JSON field name matcher #33

paulcappadona opened this issue Nov 7, 2018 · 2 comments

Comments

@paulcappadona
Copy link

Method below

    private void appendValue(StringBuilder json, String field, String value) {
        if (getJsonFieldsPattern() != null 
                && getJsonFieldsPattern().matches(field)) {
            json.append(value);
        } else {
            json.append('"')
                .append(StringEscapeUtils.escapeJson(value))
                .append("\"");
        }
    }

should have the json pattern match reversed

 private void appendValue(StringBuilder json, String field, String value) {
        if (getJsonFieldsPattern() != null 
                && field.matches(getJsonFieldsPattern())) {
            json.append(value);
        } else {
            json.append('"')
                .append(StringEscapeUtils.escapeJson(value))
                .append("\"");
        }
    }
@kalhomoud
Copy link
Contributor

Hi @paulcappadona,
Could you please elaborate? Can you give me an example?

Thanks!

@paulcappadona
Copy link
Author

paulcappadona commented Nov 9, 2018

Hi @kalhomoud

The method in question should be matching document metadata field names against a regex pattern, and if matching return true so that the committer processes the data as a JSON object (not a string).

The following code illustrates that the match method should be issued against the field, not the regex pattern

	/**
	 * Attempting to match any fields beginning with "obj-" so they are treated as JSON objects
	 */
	public static void main(String[] args) {
		String pattern = "^" + "obj-" + ".*$";
		// Expected match result is true for the following
		testField("obj-crawl-meta", pattern);
		testField("obj-document-meta", pattern);
		// Expected no match, so false
		testField("somefield", pattern);
	}
	
	private static void testField(String field, String pattern) {
		// this is the logic in the ElasticSearchCommitter
		System.out.println("Matching (pattern.match(field)) pattern " + pattern + " against field " + field + " : Matched = " + pattern.matches(field));
		// this is the correct logic
		System.out.println("Matching (field.match(pattern)) field " + field + " against pattern " + pattern + " : Matched = " + field.matches(pattern));
	}

The output of this code is
// Expected match (true)
Matching (pattern.match(field)) pattern ^obj-.$ against field obj-crawl-meta : Matched = false
Matching (field.match(pattern)) field obj-crawl-meta against pattern ^obj-.
$ : Matched = true
Matching (pattern.match(field)) pattern ^obj-.$ against field obj-document-meta : Matched = false
Matching (field.match(pattern)) field obj-document-meta against pattern ^obj-.
$ : Matched = true
// Expected fail (false)
Matching (pattern.match(field)) pattern ^obj-.$ against field somefield : Matched = false
Matching (field.match(pattern)) field somefield against pattern ^obj-.
$ : Matched = false

Regards
Paul

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