在弹性搜索中查询某些字段和条件?

时间:2019-09-01 06:53:24

标签: dataframe apache-spark elasticsearch

我有关于产品的数据,其中有一些字段(String url = "http://www.yourdomain/loginstudent.php"; RequestParams params = new RequestParams(); params.put("username", YOURTEXT); params.put("password", lng); AsyncHttpClient client=new AsyncHttpClient(); client.post(url, params, new TextHttpResponseHandler() { @Override public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) { Log.d(TAG, "onFailure:CrachReporter " + throwable.toString()); } @Override public void onSuccess(int statusCode, Header[] headers, String responseString) { } }); ...)。它已在Elastic Search中建立了索引。而且我想在Shop中搜索产品的maxProductVersion。

例如:

_id, Shop, ProductVerion

版本可能相同。

现在,我要获得的产品具有:

Shop Amazon has 3 Version crawl product: 111,222,333.
Shop Ebay has 2 version: 222,444
Shop Alibaba has 2 version: 111, 444

但我不知道对此进行查询。 请帮帮我!

2 个答案:

答案 0 :(得分:1)

我尝试了一些示例文档。我将版本字段保留为数字字段。

这些是我尝试过的示例文档

[
  {
    "_index": "test",
    "_type": "doc",
    "_id": "12334",
    "_score": 1,
    "_source": {
      "shopName": "amazon",
      "version": 341
    }
  },
  {
    "_index": "test",
    "_type": "doc",
    "_id": "123",
    "_score": 1,
    "_source": {
      "shopName": "amazon",
      "version": 3412
    }
  },
  {
    "_index": "test",
    "_type": "doc",
    "_id": "1233",
    "_score": 1,
    "_source": {
      "shopName": "amazon",
      "version": 341
    }
  },
  {
    "_index": "test",
    "_type": "doc",
    "_id": "1238",
    "_score": 1,
    "_source": {
      "shopName": "alibaba",
      "version": 34120
    }
  },
  {
    "_index": "test",
    "_type": "doc",
    "_id": "1239",
    "_score": 1,
    "_source": {
      "shopName": "alibaba",
      "version": 3414
    }
  },
  {
    "_index": "test",
    "_type": "doc",
    "_id": "123910",
    "_score": 1,
    "_source": {
      "shopName": "alibaba",
      "version": 124
    }
  }
]

按照@demas的指定,我继续进行术语汇总和热门歌曲汇总

indexName/_search

{
  "size": 0,
  "aggs": {
    "shop": {
      "terms": {
        "field": "shopName.keyword"
      },
      "aggs": {
        "product": {
          "top_hits": {
            "size": 1,
            "sort": [
              {
                "version": {
                  "order": "desc"
                }
              }
            ]
          }
        }
      }
    }
  }
}

这应该为您提供包含每个商店的最高产品版本号的文档,如下所示。

{
  "took": 8,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 6,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "shop": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "alibaba",
          "doc_count": 3,
          "product": {
            "hits": {
              "total": 3,
              "max_score": null,
              "hits": [
                {
                  "_index": "test",
                  "_type": "doc",
                  "_id": "1238",
                  "_score": null,
                  "_source": {
                    "shopName": "alibaba",
                    "version": 34120
                  },
                  "sort": [
                    34120
                  ]
                }
              ]
            }
          }
        },
        {
          "key": "amazon",
          "doc_count": 3,
          "product": {
            "hits": {
              "total": 3,
              "max_score": null,
              "hits": [
                {
                  "_index": "test",
                  "_type": "doc",
                  "_id": "123",
                  "_score": null,
                  "_source": {
                    "shopName": "amazon",
                    "version": 3412
                  },
                  "sort": [
                    3412
                  ]
                }
              ]
            }
          }
        }
      ]
    }
  }
} 

答案 1 :(得分:0)

如果提供可重现的示例会更好,但是看起来您需要使用aggregation frameworkterm aggregationtop hits aggregation

GET /_search
{
   "aggs": {
      "shops": {
        "terms": { "field": "Shop" }
      },
      "aggs": {
        "tops": {
           "top_hits": {
              "size": 100
           }
        }
      }
   }
}