【问题标题】:How to match first name first and if first name matched then last name in query DSL?如何首先匹配名字,如果名字匹配,那么查询DSL中的姓氏?
【发布时间】:2019-05-15 06:04:02
【问题描述】:

我将 Elasticsearch 中的名称索引在一列中,有些名称只有名字,而其他名称是名字和姓氏,我使用了 match_phrase、match_phrase_prefix、multi_match 但它没有给出正确的结果,所以任何嵌套查询如果名字匹配然后只匹配姓氏,我可以使用匹配吗?

multi_match、match_phrase、match_phrase_prefix

我试过 match_phrase,match_phrase_prefix 查询,

例子1:

输入:詹姆斯。

实际输出: Smith James、David James。

预期产出:詹姆斯·史密斯、詹姆斯、詹姆斯·托马斯等

示例2:

输入:詹姆斯·沃克。

输出:“无”

预期输出:詹姆斯

【问题讨论】:

  • 您应该在 elasticsearch 中创建 2 个字段,一个用于名字,一个用于姓氏。然后应该使用简单的match 查询来搜索。这将很容易维护,您的 ES 查询也将很简单.. 没有这个,您可能必须创建一个复杂的 ES 查询
  • @AmitKhandelwal,在我的数据中,许多名字只包含单个单词,如果我们传递全名,它将只返回我不想要的姓氏。
  • 对不起,我没有找到你
  • 有的全名超过2个字,有的只有1个字,这种情况怎么办?因此使用两个字段进行索引将无济于事。
  • 你能用一些例子解释一下吗?

标签: elasticsearch elasticsearch-dsl


【解决方案1】:

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

下面是我的索引

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"
            }
          }
        ]
      }
    ]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多