Elastic Multimatch查询与文档不匹配

时间:2019-08-02 19:52:01

标签: elasticsearch boolean match analyzer

我正在用以下查询在弹性(v6.7)中查询与词组“ x射线”匹配的项目:

POST item/_search
{
  "query": {
    "bool": {
      "must": {
        "multi_match": {
          "type": "phrase_prefix",
          "query": "X-Ray",
          "fields": [
            "mpn", 
            "product_description"
            "manufacturer_name"
          ], 
          "operator": "and",
          "analyzer": "standard"
        }
      }
    }
  }
}

结果集为空。

我有包含短语“ X射线”的项目文档。例如,如果我查询:

GET items/_doc/3e4a2d80-9d5e-11e7-a6c5-6ddf18575461

它返回:

{
    "_index": "items",
    "_type": "_doc",
    "_id": "3e4a2d80-9d5e-11e7-a6c5-6ddf18575461",
    "_version": 1,
    "_seq_no": 7605,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "manufacturer_name": "GE",
        "var_pricing": 0,
        "on_hand": 1,


        ...

        "product_description": "Portable X-Ray w/Fuji CR Reader", <----This should be a match!
        "project_id": null,
        "user_id": "12",
        "quote_items": [],
        "parentCategory": [
            0
        ]
    }
}

如果我在新安装的Elastic(v7.3)版本上运行查询,在该版本中我添加了三个文档,如下所示:

POST product/_bulk
{"index":{"_id":1001}}
{"name":"x-ray Machine","price":152000,"in_stock":38,"sold":47,"tags":["Alcohol","Wine"],"description":"x-ray machine for x-rays","is_active":true,"created":"2004\/05\/13"}
{"index":{"_id":1002}}
{"name":"X-Ray film","price":99,"in_stock":10,"sold":430,"tags":[],"description":"just some x-ray film","is_active":true,"created":"2007\/10\/14"}
{"index":{"_id":1003}}
{"name":"Table","price":2500,"in_stock":24,"sold":215,"tags":[],"description":"could be used for an x-ray table","is_active":true,"created":"2000\/11\/17"}

然后查询:

POST product/_search
{
  "query": {
    "bool": {
      "must": {
        "multi_match": {
          "type": "phrase_prefix",
          "query": "X-Ray",
          "fields": [
            "name", 
            "description"
          ], 
          "operator": "and",
          "analyzer": "standard"
        }
      }
    }
  }
}

返回所有三个项目:

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 31.876595,
    "hits" : [
      {
        "_index" : "product",
        "_type" : "default",
        "_id" : "1001",
        "_score" : 31.876595,
        "_source" : {
          "name" : "x-ray Machine",
          "price" : 152000,
          "in_stock" : 38,
          "sold" : 47,
          "tags" : [
            "Alcohol",
            "Wine"
          ],
          "description" : "x-ray machine for x-rays",
          "is_active" : true,
          "created" : "2004/05/13"
        }
      },
      {
        "_index" : "product",
        "_type" : "default",
        "_id" : "1002",
        "_score" : 27.347116,
        "_source" : {
          "name" : "X-Ray film",
          "price" : 99,
          "in_stock" : 10,
          "sold" : 430,
          "tags" : [ ],
          "description" : "just some x-ray film",
          "is_active" : true,
          "created" : "2007/10/14"
        }
      },
      {
        "_index" : "product",
        "_type" : "default",
        "_id" : "1003",
        "_score" : 25.889376,
        "_source" : {
          "name" : "Table",
          "price" : 2500,
          "in_stock" : 24,
          "sold" : 215,
          "tags" : [ ],
          "description" : "could be used for an x-ray table",
          "is_active" : true,
          "created" : "2000/11/17"
        }
      }
    ]
  }
}

有什么作用?

我使用了explain API来获得更多的见解,但它说明没有匹配项:

POST items/_doc/3e4a2d80-9d5e-11e7-a6c5-6ddf18575461/_explain
{
  "query": {
    "bool": {
      "must": [
        {
            "multi_match": {
              "type": "phrase_prefix",
              "query": "X-Ray",
              "fields": [
                "product_description",
                "mpn",
                "manufacturer_name"
              ], 
              "operator": "and",
              "analyzer": "standard"
        }}
        ]
      }
    }
  }
}

返回:

{
    "_index": "items",
    "_type": "_doc",
    "_id": "3e4a2d80-9d5e-11e7-a6c5-6ddf18575461",
    "matched": false,
    "explanation": {
        "value": 0,
        "description": "Failure to meet condition(s) of required/prohibited clause(s)",
        "details": [
            {
                "value": 0,
                "description": "no match on required clause (((+product_description:x +product_description:ray) | (+mpn:x +mpn:ray) | (+manufacturer_name:x +manufacturer_name:ray)))",
                "details": [
                    {
                        "value": 0,
                        "description": "No matching clause",
                        "details": []
                    }
                ]
            },
            {
                "value": 0,
                "description": "no match on required clause (MatchNoDocsQuery(\"Type list does not contain the index type\"))",
                "details": [
                    {
                        "value": 0,
                        "description": "MatchNoDocsQuery(\"Type list does not contain the index type\") doesn't match id 12556",
                        "details": []
                    }
                ]
            }
        ]
    }
}

当我将分析器更改为空白或关键字时,变化不大。

2 个答案:

答案 0 :(得分:1)

(这不是答案,但我无法在评论中输入所有内容)

如果您打算整体匹配analyzer,我不确定您是否真的需要在查询中使用X-Ray

看看这个

POST _analyze
{
  "analyzer": "standard", 
  "text":"X-Ray"
}

,响应为

{
  "tokens" : [
    {
      "token" : "x",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "ray",
      "start_offset" : 2,
      "end_offset" : 5,
      "type" : "<ALPHANUM>",
      "position" : 1
    }
  ]
}

因此您的搜索词X-Ray变成了xray。这是您想要的吗?

答案 1 :(得分:0)

所以我确定我的问题是标准分析器一直都在使用,因为在映射中将其设置为使用自定义分析器(使用了标准分析器)。

显示在这里:

$text = 'Date(1549170000000-0500)';
preg_match('#\((.*?)\)#', $text, $match);

$mil = $match[1];
$seconds = $mil / 1000;
echo date("d/m/Y H:i:s", $seconds);

显示

GET items/_mapping

这与我查询的其他两个索引字段相同。

这里的课程:

如果您在搜索方面遇到问题,请检查映射以确保未为某些字段设置自定义分析器。