简体   繁体   English

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

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

Let's say I have an index with the following mapping: 假设我有一个具有以下映射的索引:

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

and I index a simple document like the following: 并且我索引了一个简单的文档,如下所示:

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

What I want, it is to create a query that can be logically expressed as: 我想要的是创建一个可以逻辑表示为的查询:

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

But I want to search full_text in the query context while exact_value in the filter context 但我想搜索full_text而在查询上下文exact_value在过滤器中的上下文

I tried with the following query but it doesn't work (replacing MILAN with ROME is the prove) 我尝试使用以下查询,但不起作用(用ROME代替MILAN是证明)

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

Thanks in advance 提前致谢

I would try this : 我会尝试的:

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

However, as you want to use filter, please note that you won't have scoring. 但是,由于要使用过滤器,请注意,您不会获得评分。 Meaning that searching for "live OR MILAN" will produce the exact same response as "live OR ROMA" and "live" 这意味着搜索“ live OR MILAN”将产生与“ live OR ROMA”和“ live”完全相同的响应

The document actually containing "live or MILAN" might not be in first position 实际包含“ live或MILAN”的文档可能不在第一位置

Try to use this: 试着用这个:

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

I found the solution that can be built using the Java API 我找到了可以使用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