Alon
Alon

Reputation: 61

Elastic search nested object auto complete

I'm trying to add auto complete functionality to a nested object I created in elastic search.

The mapping settings I've set:

"mappings": {
    "doc": {
        "object": {
            "type": "nested",
            "properties": {
                "author": {
                    "type": "text",
                    "analyzer": "hebrew"
                },
                "content": {
                    "type": "text",
                    "analyzer": "hebrew"
                },
                "title": {
                    "type": "text",
                    "analyzer": "hebrew"
                }, 
                "suggest" : { "type": "completion"}
            }
       }
   }
}

I'm using a nested object because I'm also using fscrawler to add the json docs into the index.

I use the following query:

{
    "suggest": {
        "suggester" : {
            "prefix" : "test", 
            "completion" : { 
                "field" : "object.suggest"
            }
        }
    }
}

But the problem is that I'm getting no results no matter what I type.

Have I set the mappings correctly? Or is the query wrong?

Upvotes: 2

Views: 1126

Answers (1)

Alon
Alon

Reputation: 61

What worked for me in the end was using a suggest sub field for each object field that I want to use auto suggest with so for example if I want to use auto suggest with fields "author" and "title" then I'd use:

"mappings": {
"doc": {
    "object": {
        "type": "nested",
        "properties": {
            "author": {
                "type": "text",
                "analyzer": "hebrew",
                "fields": {
                    "exact": {
                        "type": "text",
                        "analyzer": "hebrew_exact"
                    },
                    "suggest": {
                        "type": "completion",
                        "analyzer": "simple",
                        "preserve_separators": false,
                        "preserve_position_increments": true,
                        "max_input_length": 50
                    }
                }
            },
            "content": {
                "type": "text",
                "analyzer": "hebrew_exact"
            },
            "title": {
                "type": "text",
                "analyzer": "hebrew",
                "fields": {
                    "exact": {
                        "type": "text",
                        "analyzer": "hebrew_exact"
                    },
                    "suggest": {
                        "type": "completion",
                        "analyzer": "simple",
                        "preserve_separators": false,
                        "preserve_position_increments": true,
                        "max_input_length": 50
                    }
                }
            }
        }
    }
}

And to use it I'd search the field: "object.title.suggest" with a suggestor like in the documentation.

Upvotes: 3

Related Questions