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

时间:2018-04-05 19:48:47

标签: elasticsearch elasticsearch-percolate

在ES 2.3.3中,我正在使用_all字段的系统中的许多查询。有时这些是注册到percolate索引,当在doc上运行percolator时,_all会自动生成。

在转换为ES 5.X _all时,不推荐使用_all,因此_all已替换为包含我们实际关注的组件的copy_to字段,并且对于这些搜索非常有效。

使用相同的文档映射(包括copy_to字段)将相同的查询注册到percolate索引可以正常工作。然后,使用文档发送percolate查询永远不会导致copy_to字段命中。

通过简单的字符串连接手动构建copy_to字段似乎有效,它只是我希望能够查询 - > DocIndex并获得与Doc相同的结果 - > PercolateQuery ...所以我只是想找到一种让ES自动生成copy_to字段的方法。

1 个答案:

答案 0 :(得分:0)

当然,ES当然没有任何问题,如果它能帮助别人,请点击此处。在尝试生成一个更简单的示例时将其弄清楚,以便在此处发布详细信息......基本上,问题归结为这样一个事实:尝试渗透在渗透指数中不存在的类型的文档并不是这样的。 t给出任何错误,但似乎应用所有percolate查询而不应用任何映射,这只是令人困惑,因为它适用于简单的测试用例,但不是复杂的测试用例。这是一个例子:

  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. 创建具有相同类型

    的percolate索引
    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字段上的其他percolate查询匹配的percolate查询,以及仅在基本未修改字段上查询的第二个查询

    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:John)也会出现命中

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