具有多个默认字段的 Elasticsearch query_string 查询

Mic*_*son 5 elasticsearch

我想利用query_string查询的功能,但我需要查询默认在字段子集(不是全部,但也不仅仅是一个)中进行搜索。当我尝试传递许多默认字段时,查询失败。有什么建议么?

没有在查询中指定特定字段,所以我想默认搜索三个字段:

{
    "query": {
        "query_string" : {
            "query" : "some search using advanced operators OR dog",
            "default_field": ["Title", "Description", "DesiredOutcomeDescription"]
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Dar*_*ert 5

如果您想对上述 3 个特定字段创建查询,只需使用该fields参数即可。

{
    "query": {
        "query_string" : {
            "query" : "some search using advanced operators OR dog",
            "fields": ["Title", "Description", "DesiredOutcomeDescription"]
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

或者,如果您想默认搜索这 3 个字段而不指定它们,则必须copy_to在设置映射时使用该参数。然后将默认字段设置为串联字段。

PUT my_index

{
  "settings": {
    "index.query.default_field": "full_name" 
  },
  "mappings": {
    "my_type": {
      "properties": {
        "first_name": {
          "type": "text",
          "copy_to": "full_name" 
        },
        "last_name": {
          "type": "text",
          "copy_to": "full_name" 
        },
        "full_name": {
          "type": "text"
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我已经使用过它,但不推荐它,因为对标记化的控制可能受到限制,因为您只能为连接字段指定一个标记生成器。

是 copy_to 上的页面。

  • 请注意,通过使用“copy_to”,您可能会错过用于确定相关性的“字段长度范数”因素。依赖 `_all` 字段(顺便说一句,在 ES 6.0 中已弃用)也会受到同样的影响。更多信息:https://www.elastic.co/guide/en/elasticsearch/guide/2.x/scoring-theory.html#field-norm (2认同)