簡體   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