Huston Hedinger
Huston Hedinger

Reputation: 511

How can I query an elastic search autocomplete field, and return only certain results?

Given the following mapping:

{
      "tags": {
        "aliases": {},
        "mappings": {
          "tag": {
            "properties": {
              "_users": {
                "type": "string"
              },
              "child": {
                "type": "string"
              },
              "level": {
                "type": "double"
              },
              "name": {
                "type": "string",
                "fields": {
                  "suggest": {
                    "type": "completion",
                    "analyzer": "simple",
                    "payloads": true,
                    "preserve_separators": true,
                    "preserve_position_increments": true,
                    "max_input_length": 50
                  }
                }
              },
              "parent": {
                "type": "string"
              },
              "root": {
                "type": "boolean"
              }
            }
          }
        },
        "settings": {
          "index": {
            "creation_date": "1443688314495",
            "number_of_shards": "5",
            "number_of_replicas": "1",
            "version": {
              "created": "1070299"
            },
            "uuid": "iH0QRRm_QF6pXEZ5sJI4yA"
          }
        },
        "warmers": {}
      }
    }

Also, _users is an array of strings.

How do I query for a tag name suggestion and return ONLY documents where the 'id' I query with is in the _users array?

Here is a sample tag:

{
  "_index": "tags",
  "_type": "tag",
  "_id": "560b25d8276a6504d808d703",
  "_score": 1.0,
  "_source": {
    "name": "elastic indexed?",
    "child": [],
    "_users": ["foobar", "someMongoObjectID", "hellothere"],
    "_id": "560b25d8276a6504d808d703"
  }
}

Upvotes: 0

Views: 220

Answers (2)

Huston Hedinger
Huston Hedinger

Reputation: 511

Peter, thank you for the idea. Here is a solution based on it.

The mapping:

 {
  "tags": {
    "aliases": {},
    "mappings": {
      "tag": {
        "properties": {
          "_users": {
            "type": "string"
          },
          "child": {
            "type": "string"
          },
          "level": {
            "type": "double"
          },
          "name": {
            "type": "string",
            "fields": {
              "suggest": {
                "type": "completion",
                "analyzer": "simple",
                "payloads": true,
                "preserve_separators": true,
                "preserve_position_increments": true,
                "max_input_length": 50,
                "context": {
                  "_users": {
                    "type": "category",
                    "path": "_users",
                    "default": 'public' // documents with no `_users` or where 
                                        //_users.length  === 0 should be passed 
                                        //'public' as context in the query.
                  }
                }
              }
            }
          },
          "parent": {
            "type": "string"
          },
          "root": {
            "type": "boolean"
          }
        }
      }
    },
    "settings": {
      "index": {
        "creation_date": "1443757145827",
        "number_of_shards": "5",
        "number_of_replicas": "1",
        "version": {
          "created": "1070299"
        },
        "uuid": "Uzb7Rc3sQIafgYD73CSp3g"
      }
    },
    "warmers": {}
  }
}

The query:

 /_suggest
{
    "tag-suggest": {
        "text": "foo",
        "completion": {
            "field": "name.suggest",
            "fuzzy": {},
            "context": {
                "_users": "someuserID" // or `public` for tags that are not
                                       // associated with at least one user
            }
        }
    }
}

Upvotes: 1

Peter Dixon-Moses
Peter Dixon-Moses

Reputation: 3209

See if the Context Suggester will do what you need.

Upvotes: 1

Related Questions