如何按类型过滤建议查询
How to filter suggest query by type
假设我已经映射了多个类型,例如 Company
和 Customer
。这些类型与建议字段映射。当我对这两种类型执行 suggest
查询时,一切正常。现在我想限制查询以查找一种类型的结果。我阅读了 here 关于 Context Suggester 的内容。这也有效,但现在我找不到这两种类型,因为我必须设置上下文。将上下文留空 returns 没有结果。如何快刀斩乱麻?是否有与使用 Context Suggester
不同的方法?
这是我为 Company
映射的(稍微缩短了一点):
"company": {
"dynamic": "false",
"_id": {
"path": "Id"
},
"properties": {
"name": {
"type": "string"
},
"suggest": {
"type": "completion",
"analyzer": "simple",
"payloads": true,
"preserve_separators": true,
"preserve_position_increments": true,
"max_input_length": 50,
"context": {
"type": {
"type": "category",
"path": "type",
"default": [
"company"
]
}
}
}
}
}
能够通过 context suggester 实现。我将上下文中的 path
设置为 _type
。
示例:
1) 映射
put test/company/_mapping
{
"company": {
"properties": {
"name": {
"type": "string"
},
"suggest": {
"type": "completion",
"analyzer": "simple",
"payloads": true,
"preserve_separators": true,
"preserve_position_increments": true,
"max_input_length": 50,
"context": {
"type": {
"type": "category",
"path": "_type"
}
}
}
}
}
}
put test/customer/_mapping
{
"customer": {
"properties": {
"name": {
"type": "string"
},
"suggest": {
"type": "completion",
"analyzer": "simple",
"payloads": true,
"preserve_separators": true,
"preserve_position_increments": true,
"max_input_length": 50,
"context": {
"type": {
"type": "category",
"path": "_type"
}
}
}
}
}
}
2) 示例文档
PUT test/company/1
{
"name": "hello company",
"suggest": {
"input": ["hello", "hello company"]
}
}
PUT test/customer/1
{
"name": "hello customer",
"suggest": {
"input": ["hello again", "hello customer"]
}
}
3) 建议:输入公司
POST test/_suggest
{
"suggest" : {
"text" : "hel",
"completion" : {
"field" : "suggest",
"size": 10,
"context": {
"type": ["customer"]
}
}
}
}
4) 建议:键入客户
post test/_suggest
{
"suggest" : {
"text" : "hel",
"completion" : {
"field" : "suggest",
"size": 10,
"context": {
"type": ["company"]
}
}
}
}
5) 建议:输入公司和客户
post test/_suggest
{
"suggest" : {
"text" : "hel",
"completion" : {
"field" : "suggest",
"size": 10,
"context": {
"type": ["company","customer"]
}
}
}
}
假设我已经映射了多个类型,例如 Company
和 Customer
。这些类型与建议字段映射。当我对这两种类型执行 suggest
查询时,一切正常。现在我想限制查询以查找一种类型的结果。我阅读了 here 关于 Context Suggester 的内容。这也有效,但现在我找不到这两种类型,因为我必须设置上下文。将上下文留空 returns 没有结果。如何快刀斩乱麻?是否有与使用 Context Suggester
不同的方法?
这是我为 Company
映射的(稍微缩短了一点):
"company": {
"dynamic": "false",
"_id": {
"path": "Id"
},
"properties": {
"name": {
"type": "string"
},
"suggest": {
"type": "completion",
"analyzer": "simple",
"payloads": true,
"preserve_separators": true,
"preserve_position_increments": true,
"max_input_length": 50,
"context": {
"type": {
"type": "category",
"path": "type",
"default": [
"company"
]
}
}
}
}
}
能够通过 context suggester 实现。我将上下文中的 path
设置为 _type
。
示例:
1) 映射
put test/company/_mapping
{
"company": {
"properties": {
"name": {
"type": "string"
},
"suggest": {
"type": "completion",
"analyzer": "simple",
"payloads": true,
"preserve_separators": true,
"preserve_position_increments": true,
"max_input_length": 50,
"context": {
"type": {
"type": "category",
"path": "_type"
}
}
}
}
}
}
put test/customer/_mapping
{
"customer": {
"properties": {
"name": {
"type": "string"
},
"suggest": {
"type": "completion",
"analyzer": "simple",
"payloads": true,
"preserve_separators": true,
"preserve_position_increments": true,
"max_input_length": 50,
"context": {
"type": {
"type": "category",
"path": "_type"
}
}
}
}
}
}
2) 示例文档
PUT test/company/1
{
"name": "hello company",
"suggest": {
"input": ["hello", "hello company"]
}
}
PUT test/customer/1
{
"name": "hello customer",
"suggest": {
"input": ["hello again", "hello customer"]
}
}
3) 建议:输入公司
POST test/_suggest
{
"suggest" : {
"text" : "hel",
"completion" : {
"field" : "suggest",
"size": 10,
"context": {
"type": ["customer"]
}
}
}
}
4) 建议:键入客户
post test/_suggest
{
"suggest" : {
"text" : "hel",
"completion" : {
"field" : "suggest",
"size": 10,
"context": {
"type": ["company"]
}
}
}
}
5) 建议:输入公司和客户
post test/_suggest
{
"suggest" : {
"text" : "hel",
"completion" : {
"field" : "suggest",
"size": 10,
"context": {
"type": ["company","customer"]
}
}
}
}