elasticsearch 带有两个字段和';和';过滤器-javaapi
假设我在elasticsearch中有一个包含两个字段的索引:elasticsearch 带有两个字段和';和';过滤器-javaapi,elasticsearch,java,elasticsearch,Java,假设我在elasticsearch中有一个包含两个字段的索引:title和tags。我那里几乎没有文件 应通过查询返回 { title: "My main title on this page", tags: ["first", "whatever"] } { title: "My on this page", tags: ["first", "page", "whatever"] } { title: "My main title on this page", tags: ["page",
title
和tags
。我那里几乎没有文件
{ title: "My main title on this page", tags: ["first", "whatever"] }
{ title: "My on this page", tags: ["first", "page", "whatever"] }
{ title: "My main title on this page", tags: ["page", "whatever"]}
{ title: "My main title on this page", tags: ["first", "whatever"] }
{ title: "My on this page", tags: ["first", "page", "whatever"] }
{ title: "My main title on this page", tags: ["page", "whatever"]}
{ title: "My main title on this page", tags: ["first", "whatever"] }
{ title: "My on this page", tags: ["first", "page", "whatever"] }
{ title: "My main title on this page", tags: ["page", "whatever"]}
包含
“主标题”和
标记“第一个”或
“页面”的所有文档。
我想使用javaapi
实现这一点,但我不确定如何实现这一点。我知道我可以使用过滤器查询来创建“或
”和“和
”。不确定如何添加以查询标题部分,以及如何获得“列表中至少有一个”的逻辑
有什么想法吗?这取决于你是否关心“主要”和“标题”单词的顺序(这是一个短语),但这相对简单:
{
"query" : {
"bool" : {
"must" : [
{
"match_phrase" : {
"title" : "main title"
}
},
{
"terms" : {
"tags" : [ "first", "page" ]
}
}
]
}
}
}
默认情况下,terms
查询将作为单个匹配项工作,它将通过匹配多个标记来提高分数(相关性)。这将执行精确匹配,您应该只使用而不是
字符串。任何在中包含的内容都必须
将固有地表现为和
;你可以理解这个问题。这非常简单地转化为Java API:
import static org.elasticsearch.index.query.QueryBuilders.*;
SearchResponse response =
client.prepareSearch("your-index").setTypes("your-type")
.setQuery(
boolQuery()
.must(matchPhraseQuery("title", "main title"))
.must(termsQuery("tags", "first", "page"))
.execute()
.actionGet();