Elasticsearch集成高级和低级API

时间:2018-08-05 05:48:22

标签: spring-boot elasticsearch

我已经建立了如下的Elasticsearch查询(DSL)。我想从Java应用程序集成它。当我阅读高级和低级REST选项时,Elastic给出了这些选项。是否可以使用高级API构建复杂的查询?或者我将使用低级API并通过程序构建DSL?

GET myindex/_search
{
    "query": {
        "bool": {
        "must": [
          {
            "nested": {
              "path": "blog_types",
              "query": {
                "match": {
                  "blog_types.blog_type": 45649
                }
              }
            }
          },
          {
            "nested": {
              "path": "blog_types",
              "query": {
                "match": {
                  "blog_types.blog_type": 45647
                }
              }
            }
          }
        ],
        "filter" : {
            "geo_bounding_box" : {
                "request_location.location" : {
                    "top_left" : {
                        "lat" : 50.925996,
                        "lon" : 4.584309
                    },
                    "bottom_right" : {
                        "lat" : 50.845996,
                        "lon" : 4.494309
                    }
                }
            }
        }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

好的,去吧

QueryBuilder blogType1 = QueryBuilders.nestedQuery("blog_types", 
    QueryBuilders.matchQuery("blog_types.blog_type", 45649), ScoreMode.Avg);

QueryBuilder blogType2 = QueryBuilders.nestedQuery("blog_types", 
    QueryBuilders.matchQuery("blog_types.blog_type", 45647), ScoreMode.Avg); 

QueryBuilder geo = QueryBuilders.geoBoundingBoxQueryBuilder("request_location.location")
    .setCorners(50.925996, 4.584309, 50.845996, 4.494309);

QueryBuilder mainQuery = QueryBuilders.boolQuery()
    .filter(blogType1)
    .filter(blogType2)
    .filter(geo);

如果需要OR条件,可以使用should来做到这一点:

QueryBuilder blogType1 = QueryBuilders.nestedQuery("blog_types", 
    QueryBuilders.matchQuery("blog_types.blog_type", 45649), ScoreMode.Avg);

QueryBuilder blogType2 = QueryBuilders.nestedQuery("blog_types", 
    QueryBuilders.matchQuery("blog_types.blog_type", 45647), ScoreMode.Avg); 

QueryBuilder geo = QueryBuilders.geoBoundingBoxQueryBuilder("request_location.location")
    .setCorners(50.925996, 4.584309, 50.845996, 4.494309);

QueryBuilder mainQuery = QueryBuilders.boolQuery()
    .should(blogType1)
    .should(blogType2)
    .filter(geo)
    .minimumShouldMatch(1);

答案 1 :(得分:0)

对于将来的用户: 假设我们要使用ES Client复制此查询:

.googlemap-responsive{
    overflow:hidden;
    padding-bottom:56.25%;
    position:relative;
    height:0;
}
.googlemap-responsive iframe{
    left:0;
    top:0;
    height:100%;
    width:100%;
    position:absolute;
}

使用以下代码获取以下信息:

  {
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "match": {
                  "table_name": {
                    "query": "google",
                    "operator": "and"
                  }
                }
              },
              {
                "match": {
                  "table_name": {
                    "query": "yahoo",
                    "operator": "and"
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must_not": [
              {
                "match": {
                  "vendor_type": {
                    "query": "9",
                    "operator": "and"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}