如何在elasticsearch查询中通过"type"(在doc中单独添加)在具有不同文档的同一索引中添加If-else条件?

How to add If-else condition in same index with different documents by "type" (individually added in doc) in elasticsearch query?

我在 java 中使用弹性搜索 1.6。 实际上,我是弹性查询的新手。在学习阶段。 尝试一些高级条件。

我创建了一个索引 "IndexABCD",其中我有两个不同的文档并由它们的 "type" 字段标识。 下面我以 elastic 形式显示示例文档。

例如-

Doc1
{
    "id":"DS-1",
    "type":"DATASOURCE",
    "name":"test1",
    "category":"cat1",
    "subCategory":"subcat1"
}

Doc2
{
    "id":"FL-1",
    "type":"FLOW",
    "name":"test1"
}

Doc3
{
    "id":"DS-2",
    "type":"DATASOURCE",
    "category":"cat1",
    "subCategory":"subcat1",
    "name":"test2"
}

Doc4
{
    "id":"FL-3",
    "type":"FLOW",
    "name":"test3"
}

如何添加以下 if-else 条件以提供预期输出?

if(type=="Datasource"){
    category = "cat1";
    subCategory = "subCat1";
}

if(type=="DATASOURCE" && type="FLOW"){
    category = "cat1";
    subCategory = "subCat1";
    &
    don't apply category & subcategory on type FLOW 
}    

我的案例中使用了以下查询,但我没有找到适合我的场景的完美解决方案。
在查询中,如果我给出的 DATASOURCE 结果类似于 - 2 条记录,其中包含类别和子类别。 或者哪个查询适合当前查询?

Current Query:-    
{
  "query":  
 {
  "filtered" : {
    "query" : {
      "bool" : {
        "must" : [  {
          "bool" : {
            "should" : {
              "terms" : {
                "datasource_and_flow.type" : [ "DATASOURCE" ]
              }
            }
          }
        },{
          "query_string" : {
            "query" : "test*",
            "default_field" : "datasource_and_flow.name"
          }
        } ],
        "should": [
           {
          "bool" : {
            "should" : {
              "terms" : {
                "category" : [ "cat1" ]
              }
            }
          }
        }, {
          "bool" : {
            "should" : {
              "terms" : {
                "sub_category" : [ "subcat2" ]
              }
            }
          }
        }
        ]
      }
    },        
    "filter" : {      
    }
  }
},
  "from": 0,
  "size": 10,
  "sort": [],
  "facets": {}, "aggs": {
    "datasource_and_flow.type": {
      "terms": {
        "field": "type"
      }
    }  
    }
}

如果我们加上

datasource_and_flow.type = [ "DATASOURCE","FLOW"]

现在我们想要

total records = 2 records + flow by name "test*"

但当前查询仅提供 DATASOURCE 类型,未从 FLOW 获取记录

供您参考 - 索引 json 映射 - click here for JSON Mapping file

下面的查询首先通过 "test" 关键字过滤所有文档,然后应用一个 should 子句,检查 2 个条件中的任何 1 个条件,即 (DATASOURCE + cat1 + subcat2) OR (FLOW)

GET _search
{
  "from": 0,
  "size": 20,
  "query": {
    "query_string": {
      "query": "test*",
      "default_field": "name"
    },
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "type": "DATASOURCE"
                }
              },
              {
                "term": {
                  "category": "cat1"
                }
              },
              {
                "term": {
                  "sub_category": "subcat1"
                }
              }
            ]
          }
        },
        {
          "term": {
            "type": "FLOW"
          }
        }
      ],
      "minimum_number_should_match": 1
    }
  }
}