如何查询弹性搜索自动填充字段,并仅返回特定结果?

时间:2015-10-01 08:57:55

标签: elasticsearch

给出以下映射:

{
      "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": {}
      }
    }

此外,_users是一个字符串数组。

如何查询代码名称建议,并仅返回“&id; id”的文档。我查询是在_users数组?

以下是示例代码:

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

2 个答案:

答案 0 :(得分:1)

查看Context Suggester是否能满足您的需求。

答案 1 :(得分:1)

彼得,谢谢你的想法。这是基于它的解决方案。

映射:

 {
  "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": {}
  }
}

查询:

 /_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
            }
        }
    }
}