elasticsearch - 无法使通配符查询在Elasticsearch中的多个字段上工作

标签 elasticsearch

我正在尝试使通配符在 flex 搜索中的多个字段上工作,但似乎不起作用。当我使用它时,它只会返回与提交空查询相同的结果。这是我的查询:

{
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    },
    {
      "ratingCount": {
        "order": "desc"
      }
    },
    {
      "avgRating": {
        "order": "desc"
      }
    }
  ],
  "from": 20,
  "size": 20,
  "query": {
    "bool": {
      "filter": {
        "term": {
          "cities": "59c95a090338d4fe4aea6af8"
        }
      },
      "should": [
        {
          "wildcard": {
            "firstName": "Mich*"
          }
        },
        {
          "wildcard": {
            "lastName": "Mich*"
          }
        }
      ]
    }
  }
}

最佳答案

也许from属性是您的问题?看看From/Size documentation。从20开始意味着与第一个结果的偏移量是20。这意味着您需要总共至少具有21个结果才能在查询中看到一条记录。

看一个例子。首先放三个记录:

PUT index/_doc/1
{
  "cities": "59c95a090338d4fe4aea6af8",
  "firstName": "Michael",
  "lastName": "Jordan"
}

PUT index/_doc/2
{
  "cities": "59c95a090338d4fe4aea6af8",
  "firstName": "Tomasz",
  "lastName": "Michalowicz"
}

PUT index/_doc/3
{
  "cities": "59c95a090338d4fe4aea6af8",
  "firstName": "Bartosz",
  "lastName": "Michalski"
}

然后使用您的查询和from设置为3进行搜索:
GET _search
{
  "from": 3,
  "query": {
    "bool": {
      "filter": {
        "term": {
          "cities": "59c95a090338d4fe4aea6af8"
        }
      },
      "should": [
        {
          "wildcard": {
            "firstName": "Mich*"
          }
        },
        {
          "wildcard": {
            "lastName": "Mich*"
          }
        }
      ]
    }
  }
}

响应将是:
{
  "took": 15,
  "timed_out": false,
  "_shards": {
    "total": 10,
    "successful": 10,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0,
    "hits": []
  }
}

您可以看到没有可见的hits,但total等于3。

然后将from更改为2并再次查询:
GET _search
{
  "from": 2,
  "query": {
    "bool": {
      "filter": {
        "term": {
          "cities": "59c95a090338d4fe4aea6af8"
        }
      },
      "should": [
        {
          "wildcard": {
            "firstName": "Mich*"
          }
        },
        {
          "wildcard": {
            "lastName": "Mich*"
          }
        }
      ]
    }
  }
}

答案是:
{
  "took": 13,
  "timed_out": false,
  "_shards": {
    "total": 10,
    "successful": 10,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0,
    "hits": [
      {
        "_index": "twitter",
        "_type": "_doc",
        "_id": "3",
        "_score": 0,
        "_source": {
          "cities": "59c95a090338d4fe4aea6af8",
          "firstName": "Bartosz",
          "lastName": "Michalski"
        }
      }
    ]
  }
}

然后将from更改为0并再次查询:
GET _search
{
  "from": 0,
  "query": {
    "bool": {
      "filter": {
        "term": {
          "cities": "59c95a090338d4fe4aea6af8"
        }
      },
      "should": [
        {
          "wildcard": {
            "firstName": "Mich*"
          }
        },
        {
          "wildcard": {
            "lastName": "Mich*"
          }
        }
      ]
    }
  }
}

响应:
{
  "took": 8,
  "timed_out": false,
  "_shards": {
    "total": 10,
    "successful": 10,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0,
    "hits": [
      {
        "_index": "twitter",
        "_type": "_doc",
        "_id": "2",
        "_score": 0,
        "_source": {
          "cities": "59c95a090338d4fe4aea6af8",
          "firstName": "Tomasz",
          "lastName": "Michalowicz"
        }
      },
      {
        "_index": "twitter",
        "_type": "_doc",
        "_id": "1",
        "_score": 0,
        "_source": {
          "cities": "59c95a090338d4fe4aea6af8",
          "firstName": "Michael",
          "lastName": "Jordan"
        }
      },
      {
        "_index": "twitter",
        "_type": "_doc",
        "_id": "3",
        "_score": 0,
        "_source": {
          "cities": "59c95a090338d4fe4aea6af8",
          "firstName": "Bartosz",
          "lastName": "Michalski"
        }
      }
    ]
  }
}

关于elasticsearch - 无法使通配符查询在Elasticsearch中的多个字段上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52993680/

相关文章:

java - 使用 Elasticsearch JAVA API 将 network.publish_host 设置为客户端节点

json - 为什么 Elasticsearch 批量 API 无法解析我的 JSON?

elasticsearch - 在与类型 [doc] 的映射中找不到 [path] 的字段

ruby-on-rails - 使用elasticsearch进行索引映射时将索引设置为not_analyzed有什么作用?

django - 如何在Django Haystack中查询空的MultiValueField结果

elasticsearch - Elasticsearch顶级query.term和query.bool.must.term:是否等效?

php - 多维数组按值排序,同时将其转换为整数

ruby-on-rails - Elasticsearch返回的结果少于SQL?

c# - Elasticsearch.net - 范围查询

sorting - Elasticsearch:如果命中太多,则有条件/禁用排序