Elasticsearch Java API - 构建查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24225766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:55:47  来源:igfitidea点击:

Elasticsearch Java API - building queries

javaelasticsearch

提问by Neilos

I have looked through the docs for the Search APIbut find them not descriptive enough (even though they are very well written). I am trying to build a query but understand little about all the different options available and cannot find information on the matter, when building a query and I am unable to translate queries I can run in Sense to queries I can run using the Java API.

我已经浏览了Search API的文档,但发现它们的描述性不够(即使它们写得很好)。我正在尝试构建查询,但对所有可用的不同选项知之甚少,并且无法找到有关此事的信息,在构建查询时,我无法将可以在 Sense 中运行的查询转换为可以使用Java API运行的查询。

In Sense I have the following:

在 Sense 我有以下几点:

GET index/_search
{
  "query": {
    "match" : {
      "name" : "some string"
    }
  }
}

And in my Java code I have:

在我的 Java 代码中,我有:

node = nodeBuilder().client(true).clusterName(CLUSTER_NAME).node();
client = node.client();
QueryBuilder qb = QueryBuilders.termQuery("name", "some string");
SearchResponse response = client.prepareSearch("index") //
    .setQuery(qb) // Query
    .execute().actionGet();

But they produce different search results. What is the difference as I cannot see it? Also is there a good source of information that might be of use?

但它们会产生不同的搜索结果。我看不到有什么区别?是否还有可能有用的良好信息来源?

采纳答案by John Petrone

If you want the two queries to return the same results you need to use the same type of query. In your Sense query you are performing a match query:

如果您希望两个查询返回相同的结果,您需要使用相同类型的查询。在您的 Sense 查询中,您正在执行匹配查询:

"query": {
    "match" : {
      "name" : "some string"
    }
  }

but in your Java code you are performing a termQuery:

但在您的 Java 代码中,您正在执行 termQuery:

QueryBuilder qb = QueryBuilders.termQuery("name", "some string");

So to answer your question use a match query instead in your Java code:

因此,要回答您的问题,请在您的 Java 代码中使用匹配查询:

QueryBuilder qb = QueryBuilders.matchQuery("name", "some string");

Regarding your second question, it's a bit broad. I'd certainly try going thru the documentation and searching here on StackOverflow. Regarding the Java API I'd look herefor the overview and herefor the info on the query dsl thru Java.

关于你的第二个问题,它有点宽泛。我当然会尝试浏览文档并在 StackOverflow 上搜索。关于 Java API,我会在此处查看概述,并在此处查看有关通过 Java 查询 dsl 的信息。

I think a good general understanding of how Elasticsearch works and some comfort with the query mechanism thru the REST API would be very helpful in getting to understand the Java API. Good places to start:

我认为对 Elasticsearch 的工作原理有一个很好的总体了解,并对通过 REST API 的查询机制有所了解,这对理解 Java API 非常有帮助。开始的好地方:

http://joelabrahamsson.com/elasticsearch-101/

http://joelabrahamsson.com/elasticsearch-101/

http://exploringelasticsearch.com/

http://exploringelasticsearch.com/

http://java.dzone.com/articles/elasticsearch-getting-started

http://java.dzone.com/articles/elasticsearch-getting-started