如何按类型过滤建议查询

时间:2015-10-14 14:14:32

标签: elasticsearch nest

我们假设我已经映射了多个类型,例如CompanyCustomer。这些类型与建议字段一起映射。当我对两种类型执行suggest查询时,一切正常。现在我想限制查询以查找一种类型的结果。我阅读了有关Context Suggester的here。这也有效但现在我找不到这两种类型,因为我必须设置上下文。将上下文保留为空不会返回结果。如何切断戈尔迪结?有没有使用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"
                ]
             }
          }
       }
    }
 }

1 个答案:

答案 0 :(得分:1)

能够通过上下文建议器实现它。我在上下文中将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)建议:输入公司&amp;客户

post test/_suggest
{
        "suggest" : {
            "text" : "hel",
            "completion" : {
            "field" : "suggest",
            "size": 10,
            "context": {
                "type": ["company","customer"]
            }
        }
    }
}