【发布时间】:2019-01-25 22:53:50
【问题描述】:
我正在尝试创建一个组件,为用户和组提供预先输入的自动完成建议。我正在使用弹性搜索 6.5.3。我创建了一个索引,其中包含我要搜索的字段和 3 个要按 (isGroup、isUser、organizationId) 过滤的附加字段。在某些情况下,我想使用这个组件来搜索所有用户和组,有时只是用户或组,或者只是属于特定组织的用户。我计划根据具体的用例提供一个过滤器和搜索词。我正在使用nest 进行搜索,但我不知道该怎么做。是否有可能做到这一点,如果可以,怎么做?我是否为此走错了路?我主要按照这个guide 来创建分析器和东西。如果有帮助,我可以发布我的索引,但它有点长。
这是一个返回两个项目的搜索。
return client.Search<UserGroupDocument>(s => s
.Query(q=>q
.QueryString(qs=>qs.Query("adm"))
)
);
{
"_index": "users_and_groups_autocomplete_index",
"_type": "usergroupdocument",
"_id": "c54956ab-c50e-481c-b093-f9855cc74480",
"_score": 2.2962174,
"_source": {
"id": "c54956ab-c50e-481c-b093-f9855cc74480",
"isUser": true,
"isGroup": false,
"name": "admin",
"email": "admin@snapshotdesign.com",
"organizationId": 2
}
},
{
"_index": "users_and_groups_autocomplete_index",
"_type": "usergroupdocument",
"_id": "80f98d24-39e3-475d-9cb6-8f16ca472525",
"_score": 0.8630463,
"_source": {
"id": "80f98d24-39e3-475d-9cb6-8f16ca472525",
"isUser": false,
"isGroup": true,
"name": "new Group",
"users": [
{
"name": "Test User 1",
"email": "test@example.com"
},
{
"name": "admin",
"email": "admin@snapshotdesign.com"
}
],
"organizationId": 0
}
},
因此,根据我使用此组件的位置,我可能希望所有这些返回,仅返回用户、仅返回组或仅返回组织 2 中的用户。
这是我的 UserGroupDocument 类
public class UserGroupDocument
{
public string Id { get; set; }
public bool IsUser { get; set; }
public bool IsGroup { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public List<User> Users { get; set; }
public long OrganizationId { get; set; }
}
还有用户类
public class User
{
public string Name { get; set; }
public string Email { get; set; }
}
根据下面 Russ Cam 的回答,我更改了 Must 语句,如下所示。这给了我我想要的过滤,但不是预输入功能。在开始匹配之前,我仍然需要输入整个单词。
.Must(mu => mu
.QueryString(mmp => mmp
.Query(searchTerms)
.Fields(f => f
.Field(ff => ff.Name)
.Field(ff => ff.Users.Suffix("name"))
)
)
)
这是我正在使用的索引。
{
"users_and_groups_autocomplete_index": {
"aliases": {},
"mappings": {
"usergroupdocument": {
"properties": {
"email": {
"type": "text",
"fields": {
"autocomplete": {
"type": "text",
"analyzer": "autocomplete"
}
}
},
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"isGroup": {
"type": "boolean"
},
"isUser": {
"type": "boolean"
},
"name": {
"type": "text",
"fields": {
"autocomplete": {
"type": "text",
"analyzer": "autocomplete"
}
}
},
"organizationId": {
"type": "long"
},
"users": {
"properties": {
"email": {
"type": "text",
"fields": {
"autocomplete": {
"type": "text",
"analyzer": "autocomplete"
}
}
},
"name": {
"type": "text",
"fields": {
"autocomplete": {
"type": "text",
"analyzer": "autocomplete"
}
}
}
}
}
}
}
},
"settings": {
"index": {
"number_of_shards": "5",
"provided_name": "users_and_groups_autocomplete_index",
"creation_date": "1548363729311",
"analysis": {
"analyzer": {
"autocomplete": {
"filter": [
"lowercase"
],
"type": "custom",
"tokenizer": "autocomplete"
}
},
"tokenizer": {
"autocomplete": {
"token_chars": [
"digit",
"letter"
],
"min_gram": "1",
"type": "edge_ngram",
"max_gram": "20"
}
}
},
"number_of_replicas": "1",
"uuid": "Vxv-y58qQTG8Uh76Doi_dA",
"version": {
"created": "6050399"
}
}
}
}
}
【问题讨论】:
-
请添加您尝试过的查询、预期结果和您拥有的示例数据。这将有助于理解问题。
-
这有帮助还是您需要更多信息?我很乐意添加任何内容,我只是不想用一堆无用的 json 来混淆问题
标签: elasticsearch nest