Elasticsearch 5.x渗透:如何自动化copy_to字段



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

转换为ES 5.x _ all正在弃用,因此,_所有人已被copy_to字段替换为包含我们实际关心的组件,并且非常适合这些搜索。

将相同的查询注册到具有相同文档映射(包括Copy_to字段(的渗透索引的同一查询。发送文档的渗透性查询永远不会导致copy_to字段的命中。

通过简单的字符串串联手动构建copy_to字段似乎有效,我希望能够查询 -> docIndex并获得与doc-> percalyquery相同的结果...所以我只是在寻找为了使ES在被渗透的文档上自动生成copy_to字段。

最终ES当然没有错,在此处发布,以防万一它可以帮助他人。在尝试生成更简单的示例时弄清楚了此处的详细信息……基本上,试图渗透渗透索引中不存在的类型的文档没有任何错误的事实。回来,但似乎可以应用所有渗透性查询,而无需应用任何映射,而这些映射在简单的测试用例中起作用,但并不复杂。这是一个例子:

  1. 来自copy_to docs,生成带有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. 搜索,但是使用错误的类型...即使没有文档映射与请求匹配

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

最新更新