【问题标题】:ElasticSearch MultiField Search QueryElasticSearch 多字段搜索查询
【发布时间】:2015-03-08 21:19:22
【问题描述】:

我有一个端点,我正在代理到 ElasticSearch API 中进行简单的用户搜索。

/users?nickname=myUsername&email=myemail@gmail.com&name=John+Smith

关于这些参数的一些细节如下

  • 所有参数都是可选的
  • 昵称可以作为全文搜索进行搜索(即“myUser”将返回“myUsername”)
  • 电子邮件必须完全匹配
  • name 可以作为每个标记的全文搜索进行搜索(即“john”将返回“John Smith”)

ElasticSearch 搜索调用应将参数共同视为 AND'd。

现在,我不确定从哪里开始,因为我可以单独对每个参数执行查询,但不能一起执行。

client.search({
    index: 'users',
    type: 'user',
    body: {
        "query": {
            //NEED TO FILL THIS IN
        }
    }
}).then(function(resp){
    //Do something with search results
});

【问题讨论】:

    标签: node.js mongodb elasticsearch mongoose elasticsearch-plugin


    【解决方案1】:

    首先,您需要为此特定用例创建映射。

    curl -X PUT "http://$hostname:9200/myindex/mytype/_mapping" -d '{
      "mytype": {
        "properties": {
          "email": {
            "type": "string",
            "index": "not_analyzed"
          },
          "nickname": {
            "type": "string"
          },
          "name": {
            "type": "string"
          }
        }
      }
    }'
    

    通过将电子邮件设置为 not_analyzed ,您可以确保只有完全匹配才有效。 完成后,您需要进行查询。 由于我们有多个条件,使用 bool 查询是个好主意。 您可以组合多个查询以及如何使用 bool 查询来处理它们

    查询 -

    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "name": "qbox"
              }
            },
            {
              "prefix": {
                "nickname": "qbo"
              }
            },
            {
              "match": {
                "email": "me@qbox.io"
              }
            }
          ]
        }
      }
    }
    

    使用前缀查询,您告诉 Elasticsearch 即使令牌以 qbo 开头,也将其限定为匹配项。

    前缀查询可能不是很快,在这种情况下你可以使用 ngram 分析器 - http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-ngram-tokenizer.html

    【讨论】:

      猜你喜欢
      • 2023-01-17
      • 1970-01-01
      • 2018-12-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 2016-08-18
      • 2022-12-04
      相关资源
      最近更新 更多