java 在elasticsearch中搜索ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17701019/
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-11-01 14:44:10  来源:igfitidea点击:

Id search in elasticsearch

javaelasticsearch

提问by Soo

Now i want to search id in field.

现在我想在字段中搜索 id。

builder.startObject().startObject(TYPE_SERIES).startObject("properties");

    builder.startObject(ID)
            .field("type", "long")
            .field("store", "yes")
            .field("index", "analyzed")
            .field("analyzer", "test_analyzer")
            .endObject();

    builder.startObject(TITLE)
            .field("type", "string")
            .field("store", "yes")
            .field("index", "analyzed")
            .field("analyzer", "test_analyzer")
            .field("boost", "10")
            .endObject();

I tried to search ID like this:

我试图像这样搜索 ID:

.setQuery(QueryBuilders.multiMatchQuery(query, 
                       ID, TITLE))

BTW, I can get result with id, instead I cannot search with title. I just only search with id for getting results.

顺便说一句,我可以用 id 得到结果,而不是我不能用标题搜索。我只是用 id 搜索以获得结果。

So I changed this like this. I used MultiSearchResponse so that I tried to execute two kids of queries.

所以我就这样改了。我使用了 MultiSearchResponse 以便我尝试执行两个查询的孩子。

One is

一个是

.setQuery(QueryBuilders.idsQuery(TYPE).ids(query))

the other is

另一个是

.setQuery(QueryBuilders.multiMatchQuery(query, 
                                            TITLE,DESCRIPTION))

Unfortunately, I just get result with title or description. If I search for ID, i cannot get any results.

不幸的是,我只得到带有标题或描述的结果。如果我搜索 ID,则无法获得任何结果。

================ I'd like to execute like this

================ 我想这样执行

curl -XGET localhost:9200/test/series/99

I want to get results with ID. Unfortunately, I don't know well how to make this code with JAVA API.

我想获得带有 ID 的结果。不幸的是,我不太清楚如何使用 JAVA API 制作此代码。

Please let me know. Thanks.

请告诉我。谢谢。

回答by Jilles van Gurp

There's a _id field that you can search in. So, curl http://localhost:9200/_search?q=_id:99+AND+_type:series+AND+_index:testor http://localhost:9200/test/series/_search?q=_id:99should do the trick.

有一个 _id 字段可供您搜索。因此,curlhttp://localhost:9200/_search?q=_id:99+AND+_type:series+AND+_index:testhttp://localhost:9200/test/series/_search?q=_id:99应该可以解决问题。

回答by James Boutcher

Like the previous people have said, a complete example would help. But let's say your document type you're looking for is "series":

就像前面的人所说的,一个完整的例子会有所帮助。但是假设您要查找的文档类型是“系列”:

.setQuery(QueryBuilders.idsQuery("series").addIds("99"));

Or a bit more complete:

或者更完整一点:

SearchResponse esResponse = esClient.prepareSearch("test")
                .setQuery(QueryBuilders.idsQuery("series").addIds("99"))
                .execute().actionGet();