【问题标题】:Can't get wild card query to work on multiple fields in elasticsearch无法让通配符查询在弹性搜索中的多个字段上工作
【发布时间】:2018-10-27 05:34:07
【问题描述】:

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

{
  "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*"
          }
        }
      ]
    }
  }
}

【问题讨论】:

  • 你的映射是什么样的,你能发布一个示例文档吗?

标签: elasticsearch


【解决方案1】:

也许from 属性是您的问题?看看From/Size documentation。 From 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"
        }
      }
    ]
  }
}

【讨论】:

    猜你喜欢
    • 2019-03-08
    • 1970-01-01
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    相关资源
    最近更新 更多