Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
<img src="//i.stack.imgur.com/RUiNP.png" height="16" width="18" alt="" class="sponsor tag img">elasticsearch 带有两个字段和';和';过滤器-javaapi_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch_Java - Fatal编程技术网 elasticsearch 带有两个字段和';和';过滤器-javaapi,elasticsearch,java,elasticsearch,Java" /> elasticsearch 带有两个字段和';和';过滤器-javaapi,elasticsearch,java,elasticsearch,Java" />

elasticsearch 带有两个字段和';和';过滤器-javaapi

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",

假设我在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", "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();