繁体   English   中英

使用Elasticsearch 2.x在查询上下文和过滤器上下文之间进行逻辑或

[英]Logical OR between query context and filter context with Elasticsearch 2.x

假设我有一个具有以下映射的索引:

PUT my_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "full_text": {
          "type":  "string" 
        },
        "exact_value": {
          "type":  "string",
          "index": "not_analyzed" 
        }
      }
    }
  }
}

并且我索引了一个简单的文档,如下所示:

PUT my_index/my_type/1
{
  "full_text":   "This is the city where I live", 
  "exact_value": "MILAN"  
}

我想要的是创建一个可以逻辑表示为的查询:

full_text:CONTAINS('live') OR exact_value:CONTAINS('MILAN')

但我想搜索full_text而在查询上下文exact_value在过滤器中的上下文

我尝试使用以下查询,但不起作用(用ROME代替MILAN是证明)

POST _search
{
  "query" : {
    "bool" : {
      "filter" : {
        "bool" : {
          "should" : {
            "term" : {
              "exact_value" : "MILAN"
            }
          }
        }
      },
      "should" : {
        "query_string" : {
          "query" : "live",
          "fields" : [ "full_text" ]
        }
      }
    }
  }
}

提前致谢

我会尝试的:

POST my_index/_search
{
  "query": {
    "bool": {
      "filter": {
        "or": [
          {
            "term": {
              "exact_value": "MILAN"
            }
          },
          {
            "term": {
              "full_text": "live"
            }
          }
        ]
      }
    }
  }
}

但是,由于要使用过滤器,请注意,您不会获得评分。 这意味着搜索“ live OR MILAN”将产生与“ live OR ROMA”和“ live”完全相同的响应

实际包含“ live或MILAN”的文档可能不在第一位置

试着用这个:

POST my_index/_search
{
"filter": {
  "bool": {
     "should": [
        {
           "term": {
              "exact_value": "MILAN"
           }
        },
        {
           "query": {
              "query_string": {
                 "default_field": "full_text",
                 "query": "live"
              }
           }
         }
       ]
     }
    }
   }

我找到了可以使用Java API构建的解决方案

{
  "query" : {
    "bool" : {
      "should" : [ {
        "bool" : {
          "filter" : {
            "term" : {
              "exact_value" : "MILAN"
            }
          }
        }
      }, {
        "query_string" : {
          "query" : "live",
          "fields" : [ "full_text" ]
        }
      } ]
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM