如何使用"类型"在不同文档的同一索引中添加If-else条件(在doc中单独添加)在elasticsearch查询中?

时间:2017-03-14 12:16:46

标签: java elasticsearch elasticsearch-plugin

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

我创建了一个索引" IndexABCD"其中我有两个不同的文件&由他们的"类型"领域。     下面是我弹性的样本文件。

例如 -

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

1 个答案:

答案 0 :(得分:1)

下面的查询首先按“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
    }
  }
}