如何在查询DSL中匹配名字和名字,然后匹配名字?

时间:2019-05-15 06:04:03

标签: elasticsearch elasticsearch-dsl

我在一个列中为Elasticsearch中的名称建立索引,某些名称只有名字,而其他名称是名字和姓氏,我使用过match_phrase,match_phrase_prefix,multi_match但它没有给出正确的结果,因此任何嵌套查询我可以用来匹配是否匹配名字,然后只匹配姓氏?

multi_match,match_phrase,match_phrase_prefix

我尝试了match_phrase,match_phrase_prefix查询

example1:

输入:詹姆斯。

实际输出: Smith James,David James。

预期输出: James Smith,James,James Thomas等。

示例2:

输入:詹姆斯·沃克。

输出:“无”

预期输出:詹姆斯

1 个答案:

答案 0 :(得分:0)

如果您的目的是仅获取名称列表。您可以使用完成提示器

下面是我的索引

PUT my_index
{
    "mappings": {
        "properties" : {
            "name" : {
                "type" : "completion"
            }
        }
    }
}

查询

POST my_index/_search?pretty
{
    "suggest": {
        "name-suggest" : {
            "prefix" : "<Search text>",
            "completion" : { 
                "field" : "name" 
            }
        }
    }
}

下面是我的完整文件

"hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "joqRxGoBW0RKSbIqHqsw",
        "_score" : 1.0,
        "_source" : {
          "name" : "Smith James"
        }
      },
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "j4qRxGoBW0RKSbIqP6uW",
        "_score" : 1.0,
        "_source" : {
          "name" : "James Smith"
        }
      },
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "kIqRxGoBW0RKSbIqUKvP",
        "_score" : 1.0,
        "_source" : {
          "name" : "James"
        }
      }
    ]
  }
Query:
POST my_index/_search?pretty
{
    "suggest": {
        "name-suggest" : {
            "prefix" : "James",
            "completion" : { 
                "field" : "name" 
            }
        }
    }
}
]

Result:
"suggest" : {
    "name-suggest" : [
      {
        "text" : "James",
        "offset" : 0,
        "length" : 5,
        "options" : [
          {
            "text" : "James",
            "_index" : "my_index",
            "_type" : "_doc",
            "_id" : "kIqRxGoBW0RKSbIqUKvP",
            "_score" : 1.0,
            "_source" : {
              "name" : "James"
            }
          },
          {
            "text" : "James Smith",
            "_index" : "my_index",
            "_type" : "_doc",
            "_id" : "j4qRxGoBW0RKSbIqP6uW",
            "_score" : 1.0,
            "_source" : {
              "name" : "James Smith"
            }
          }
        ]
      }
    ]