具有多个和/或流的Elasticsearch bool查询

时间:2016-05-05 20:58:57

标签: elasticsearch

我正在尝试使用elasticsearch查询DSL编写下面提到的SQL查询,但我无法获得与SQL查询相同的结果。谁可以帮助e与bool查询下面提到的情况。我使用的是elasticsearch 2.1.1版本。

public class List extends ListFragment {

java.util.List<Wine_String> lstWine;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {



    wineListData();



    WineAdapter adapter = new WineAdapter(getActivity(), R.layout.custom_winelist, lstWine);
    setListAdapter(adapter);

    return super.onCreateView(inflater, container, savedInstanceState);
}


public void wineListData() {

    lstWine = new ArrayList<Wine_String>();

    lstWine.add(new Wine_String(R.drawable.blush, "Blushshs", "Random", "MONEY"));
    lstWine.add(new Wine_String(R.drawable.blush, "Blush", "Random", "String"));
    lstWine.add(new Wine_String(R.drawable.blush, "Blush", "YEAR", "Random"));
    lstWine.add(new Wine_String(R.drawable.blush, "Blush", "fffffffffffffffffffffff", "Random"));
    lstWine.add(new Wine_String(R.drawable.blush, "Blush", "YEAR", "Random"));
    lstWine.add(new Wine_String(R.drawable.blush, "Blush", "YEAR", "Random"));
    lstWine.add(new Wine_String(R.drawable.blush, "Blush", "YEAR", "Random"));
    lstWine.add(new Wine_String(R.drawable.blush, "Blush", "YEAR", "Random"));
    lstWine.add(new Wine_String(R.drawable.blush, "Blush", "fffffffffffffffff", "Random"));
    lstWine.add(new Wine_String(R.drawable.blush, "Blush", "YEAR", "Random"));
    lstWine.add(new Wine_String(R.drawable.blush, "Blush", "YEAR", "Random"));
    lstWine.add(new Wine_String(R.drawable.blush, "Blush", "YEAR", "Random"));



}
 }

这是我试过的。如果这是正确的,我如何在Java DSL中编写相同的查询?

id.eq(someId)
  .and(
     (anotherId.eq(another).and(node.eq(node)))
     .or
     (domain.in(List))
    )

3 个答案:

答案 0 :(得分:1)

根据它的外观,您可能会在第一个bool之前关闭第一个should查询。 如果您将查询分成不同的行:

id.eq(someId)
  .and(
     (anotherId.eq(another).and(node.eq(node)))
     .or
     (domain.in(List))
    )

了解如何构建ElasticSearch查询要容易得多:

{ 
    "bool" : { 
        "must": [
            { "term": { "id": 15 } },
            { "bool": {
                "should": [
                    { "bool": {
                        "must": [
                            { "term": { "anotherId": "dd" } },
                            { "terms": { "node": [ "ff" ] } }
                            ]
                        }
                    },
                    { "terms": { "domain": [ "nn", "nn" ] } }
                ]
             }
            }
         ]
     }
}

希望括号正确关闭:)如果没有,请告诉我,并尝试纠正它们。

答案 1 :(得分:1)

这是在ES 2.1.1

中执行此操作的正确方法
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "id": 1
          }
        },
        {
          "bool": {
            "should": [
              {
                "bool": {
                  "filter": [
                    {
                      "term": {
                        "anotherId": "ss"
                      }
                    },
                    {
                      "term": {
                        "node": "ss"
                      }
                    }
                  ]
                }
              },
              {
                "terms": {
                  "domain": [
                    "cc",
                    "cc"
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}

答案 2 :(得分:0)

上面提到的标记为答案的查询都可以正常工作。如果有人想知道如何使用Java API嵌套must / should,请参考这个答案。如果它没有按预期工作,请告诉我。

QueryBuilders.boolQuery().must(QueryBuilders.termQuery(ID1, associationId)).must(QueryBuilders.boolQuery().should(QueryBuilders.boolQuery()
                        .must(QueryBuilders.termQuery(Another_ID, "cc")).must(QueryBuilders.termsQuery(NODE, "cc")))
                        .should(QueryBuilders.termsQuery(DOMAIN, ["cc","cc"])));

OR:

QueryBuilders.boolQuery().filter(QueryBuilders.termQuery(ID1, associationId)).filter(QueryBuilders.boolQuery().should(QueryBuilders.boolQuery()
                        .must(QueryBuilders.termQuery(Another_ID, "cc")).must(QueryBuilders.termsQuery(NODE, "cc")))
                        .should(QueryBuilders.termsQuery(DOMAIN, ["cc","cc"])));