4

I'm working on simple search app with completion feature. I need to somehow secure those suggestions so I figured out that simplest way to do so would be to add context to completion suggester. My problem is that I don't know how to use suggester context in nested fields.

This is how my mapping looks like, very simple, just 3 fields and one as nested.

curl-XPUT'http: //localhost: 9200/cr/_mapping/agreement_index'-d'{
    "agreement_index": {
        "properties": {
            "agreement_name": {
                "type": "string",
                "fields": {
                    "suggest": {
                        "type": "completion",
                        "analyzer": "simple",
                        "payloads": false,
                        "preserve_separators": true,
                        "preserve_position_increments": true,
                        "max_input_length": 50,
                        "context": {
                            "permitted": {
                                "type": "category",
                                "path": "permitted",
                                "default": []
                            }
                        }
                    }
                }
            },
            "permitted": {
                "type": "integer"
            },
            "team": {
                "type": "nested",
                "dynamic": "false",
                "properties": {
                    "email": {
                        "type": "string",
                        "fields": {
                            "raw": {
                                "type": "string",
                                "index": "not_analyzed"
                            },
                            "suggest": {
                                "type": "completion",
                                "analyzer": "simple",
                                "payloads": false,
                                "preserve_separators": true,
                                "preserve_position_increments": true,
                                "max_input_length": 50,
                                "context": {
                                    "permitted": {
                                        "type": "category",
                                        "path": "permitted",
                                        "default": []
                                    }
                                }
                            }
                        }
                    },
                    "name": {
                        "type": "string",
                        "fields": {
                            "raw": {
                                "type": "string",
                                "index": "not_analyzed"
                            },
                            "suggest": {
                                "type": "completion",
                                "analyzer": "simple",
                                "payloads": false,
                                "preserve_separators": true,
                                "preserve_position_increments": true,
                                "max_input_length": 50,
                                "context": {
                                    "permitted": {
                                        "type": "category",
                                        "path": "permitted",
                                        "default": []
                                    }
                                }
                            }
                        }
                    },
                    "permitted": {
                        "type": "integer"
                    }
                }
            }
        }
    }
}'

During indexing documents like this:

curl-XPUT'http: //localhost: 9200/cr/agreement_index/1'-d'{
    "agreement_name": "QWERTY",
    "team": [{
        "name": "Tomasz Sobkowiak",
        "permitted": ["2"],
        "email": "tsobkowiak@fake.com"
    }],
    "permitted": ["2"]
}'

I got below error:

{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"one or more prefixes needed"}],"type":"illegal_argument_exception","reason":"one or more prefixes needed"},"status":400}

After removing context from completion suggesters in nested fields everything work fine. So my question is, how I can use context suggesters in nested fields with path pointed to field in outer document? Is something like that even possible?

sop3k
  • 101
  • 2
  • 8

1 Answers1

3

The problem is in your mapping. Default can not be left empty. You need to to assign at least one default value in the mapping for context suggester.

    "context": {
        "permitted": {
            "type": "category",
            "path": "permitted",
            "default": [] // <-- defaults can not be empty, provide at least one default integer value
        }
}

The value of the default field is used, when ever no specific is provided for the certain context. Note that a context is defined by at least one value.

Also, In the document you are trying to index, you are using string in permitted whereas it is mapped as Integer.

"permitted": ["2"] // <-- change this to "permitted":[2]
Rahul
  • 15,979
  • 4
  • 42
  • 63