关键字“OR”的ElasticSearch查询匹配

uurity8g  于 2023-01-16  发布在  ElasticSearch
关注(0)|答案(1)|浏览(209)

我使用的是ElasticSearch 7.0
给定Map:

{
  "searchquestion": {
    "mappings": {
      "properties": {
        "server": {
          "properties": {
            "hostname": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            }
          }
        }
      }
    }
  }
}

我已将下列文件编入本索引:

{
   "server": {
       "hostname": "server1-windows.loc2.uk"
   }      
}
{
   "server": {
       "hostname": "server1-windows.loc2.uk"
   }      
}
{
   "server": {
       "hostname": "server1-linux.loc1.uk"
   }      
}

我想查询hostname的确切文本。幸运的是,这是可以做到的,因为在这个字段上有一个额外的关键字类型字段。
查询成功:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "server.hostname.keyword": {
              "query": "server1-windows.loc2.uk"
            }
          }
        }
      ]
    }
  }
}

但是,我希望扩展这个查询字符串,以包括另一个要搜索的主机名。在我的结果中,我希望返回两个文档。
我的尝试:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "server.hostname.keyword": {
              "query": "server1-windows.loc2.uk server1-linux.loc1.uk",
              "operator": "or"
            }
          }
        }
      ]
    }
  }
}

我怀疑这没有返回任何结果,因为默认的分析器将这个查询分割成多个部分,但我实际上搜索的是一个完整的字符串关键字字段。我不能将analyzer: keyword添加到这个查询搜索中,因为server1-windows.loc2.uk server1-linux.loc1.uk作为一个精确的字符串也不会匹配任何内容。
我怎样才能搜寻这两个弦,作为它们的完整自我呢?
"query": ["server1-windows.loc2.uk", "server1-linux.loc1.uk"]
我还希望使用通配符来匹配任何loc。我希望"query": ["server1-windows.*.uk"]匹配两个windows服务器,但我没有得到匹配。
我错过了什么?

7rfyedvj

7rfyedvj1#

可以使用Query_String来获得所需的结果

    • 案例1:**
    • 查询**:
GET server/_search
{
  "query": {
    "query_string": {
      "query": "(server1-windows.loc2.uk) OR (server1-linux.loc1.uk)",
      "default_field": "server.hostname.keyword"
    }
  }
}
    • 输出**:
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 0.9808291,
    "hits": [
      {
        "_index": "server",
        "_id": "3",
        "_score": 0.9808291,
        "_source": {
          "server": {
            "hostname": "server1-linux.loc1.uk"
          }
        }
      },
      {
        "_index": "server",
        "_id": "1",
        "_score": 0.4700036,
        "_source": {
          "server": {
            "hostname": "server1-windows.loc2.uk"
          }
        }
      },
      {
        "_index": "server",
        "_id": "2",
        "_score": 0.4700036,
        "_source": {
          "server": {
            "hostname": "server1-windows.loc2.uk"
          }
        }
      }
    ]
  }
}
    • 情况2:使用通配符(*)**
    • 查询:**
GET server/_search
{
  "query": {
    "query_string": {
      "query": "server1-windows.*.uk",
      "default_field": "server.hostname.keyword"
    }
  }
}
    • 输出:**
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "server",
        "_id": "1",
        "_score": 1,
        "_source": {
          "server": {
            "hostname": "server1-windows.loc2.uk"
          }
        }
      },
      {
        "_index": "server",
        "_id": "2",
        "_score": 1,
        "_source": {
          "server": {
            "hostname": "server1-windows.loc2.uk"
          }
        }
      }
    ]
  }
}

相关问题