Elasticsearch 5.X 渗透:如何自动生成 copy_to 字段?

Elasticsearch 5.X Percolate: How to autogenerate copy_to fields?

在 ES 2.3.3 中,我正在处理的系统中的许多查询都使用 _all 字段。有时这些被注册到渗透索引,当文档上的运行 percolator 时,_all 会自动生成。

在转换为 ES 5.X 时,_all 已被弃用,因此 _all 已被替换为包含我们真正关心的组件的 copy_to 字段,它非常适合这些搜索。

将相同的查询注册到具有相同文档映射(包括 copy_to 字段)的渗透索引工作正常。但是,发送带有文档的渗透查询永远不会命中 copy_to 字段。

通过简单的字符串连接手动构建 copy_to 字段似乎可行,只是我希望能够查询 -> DocIndex 并获得与 Doc -> PercolateQuery 相同的结果...所以我只是在寻找一种方法让 ES 在正在渗透的文档上自动生成 copy_to 字段。

最终 ES 当然没有任何问题,post在这里以防它对其他人有帮助。在尝试为 post 生成一个更简单的示例并提供详细信息时弄清楚了......基本上问题归结为这样一个事实,即试图渗透过滤索引中不存在的类型的文档不会' t 返回任何错误,但似乎应用所有渗透查询而不应用任何映射,这只是令人困惑,因为它适用于简单的测试用例,但不适用于复杂的测试用例。这是一个例子:

  1. 从 copy_to 文档中,生成一个带有 copy_to 映射的索引。看到对 copy_to 字段的查询有效。

    PUT my_index
    {
      "mappings": {
        "my_type": {
          "properties": {
            "first_name": {
              "type": "text",
              "copy_to": "full_name" 
            },
            "last_name": {
              "type": "text",
              "copy_to": "full_name" 
            },
            "full_name": {
              "type": "text"
            }
          }
        }
      }
    }
    
    PUT my_index/my_type/1
    {
      "first_name": "John",
      "last_name": "Smith"
    }
    
    GET my_index/_search
    {
      "query": {
        "match": {
          "full_name": { 
            "query": "John Smith",
            "operator": "and"
          }
        }
      }
    }
    
  2. 创建相同类型的过滤索引

    PUT /my_percolate_index
    {
      "mappings": {
        "my_type": {
          "properties": {
            "first_name": {
              "type": "text",
              "copy_to": "full_name" 
            },
            "last_name": {
              "type": "text",
              "copy_to": "full_name" 
            },
            "full_name": {
              "type": "text"
            }
          }
        },
        "queries": {
          "properties": {
            "query": {
              "type": "percolator"
            }
          }
        }
      }
    }
    
  3. 创建一个与我们在 copy_to 字段上的其他渗透查询相匹配的渗透查询,以及仅在基本未修改字段上查询的第二个查询

    PUT /my_percolate_index/queries/1?refresh
    {
      "query": {
        "match": {
          "full_name": { 
            "query": "John Smith",
            "operator": "and"
          }
        }
      }
    }
    PUT /my_percolate_index/queries/2?refresh
    {
      "query": {
        "match": {
          "first_name": { 
            "query": "John"
          }
        }
      }
    }
    
  4. 搜索,但类型错误...即使没有文档映射匹配请求,也会在基本字段上命中(first_name:约翰)

    GET /my_percolate_index/_search
    {
      "query" : {
        "percolate" : {
          "field" : "query",
          "document_type" : "non_type",
          "document" : {
            "first_name": "John",
            "last_name": "Smith"
          }
        }
      }
    }
    {"took":7,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":0.2876821,"hits":[{"_index":"my_percolate_index","_type":"queries","_id":"2","_score":0.2876821,"_source":{
      "query": {
        "match": {
          "first_name": { 
            "query": "John"
          }
        }
      }
    }}]}}
    
  5. 发送正确的 document_type 并按预期查看两个匹配项

    GET /my_percolate_index/_search
    {
      "query" : {
        "percolate" : {
          "field" : "query",
          "document_type" : "my_type",
          "document" : {
            "first_name": "John",
            "last_name": "Smith"
          }
        }
      }
    }
    {"took":7,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":2,"max_score":0.51623213,"hits":[{"_index":"my_percolate_index","_type":"queries","_id":"1","_score":0.51623213,"_source":{
      "query": {
        "match": {
          "full_name": { 
            "query": "John Smith",
            "operator": "and"
          }
        }
      }
    }},{"_index":"my_percolate_index","_type":"queries","_id":"2","_score":0.2876821,"_source":{
      "query": {
        "match": {
          "first_name": { 
            "query": "John"
          }
        }
      }
    }}]}}