elasticsearch - Elasticsearch :在查询中包含多个字段

标签 elasticsearch

我是Elastic Search的新手,我想在搜索查询中包含多个字段,例如:

      Title=my title and city=mycity or country = mycountry

如何使用Java客户端执行这种查询?我试过了
SearchResponse response = client.prepareSearch("titan")
    .setTypes("vertex")
    .setSearchType(SearchType.QUERY_AND_FETCH)
    .setQuery(QueryBuilders.fieldQuery("title", "mytitle"))
    .setQuery(QueryBuilders.fieldQuery("city", "mycity"))
    .setFrom(0).setSize(60).setExplain(true)
    .execute()
    .actionGet();

但是没用

最佳答案

我相信,您必须在那做booleanQuery

就像是:

SearchResponse response = client.prepareSearch("titan")
    .setTypes("vertex")
    .setSearchType(SearchType.QUERY_AND_FETCH)
    .setQuery(QueryBuilders.boolQuery()
    .must(QueryBuilders.fieldQuery("title", "mytitle"))
    .should(QueryBuilders.fieldQuery("city", "mycity"))
    .should(QueryBuilders.fieldQuery("country", "mycountry")))
    .setFrom(0).setSize(60).setExplain(true)
    .execute()
    .actionGet();

简而言之, bool(boolean) 查询的规则是,所有must子句都应为true,并且should子句中至少应为true(必须更改的true子句的数量可以更改,一个是默认设置)。

关于elasticsearch - Elasticsearch :在查询中包含多个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23710028/

相关文章:

ruby-on-rails - 在Ruby Elastic Search gem中转义@ at符号?

elasticsearch - 当“took”增加时,从输出返回的“from”增加

elasticsearch - Elassandra 数据建模 : When to create a secondary index and when not to

elasticsearch - 如何使用默认索引和自定义分析器创建Nest ElasticSearch客户端?

caching - 我们可以使用 Elasticsearch 作为缓存来快速检索数据吗?

php - Laravel Scout - 观察关系

elasticsearch - ElasticSearch:嵌套项目计入搜索结果

elasticsearch 监听多个ips

elasticsearch - 带有Ingest插件的ElasticSearch Bulk

elasticsearch - 如何在cassandra上安装搜索引擎?