ElasticSearch - 带空格和过滤的前缀

时间:2014-08-14 08:45:19

标签: elasticsearch

我的ElasticSearch服务器包含以下格式的文档:

{
    "_index": "xindex",
    "_type": "xtype",
    "_id": "1100",
    "_score": 3.00010,
    "_source": {
        "_id": "2333345",
        "field1": "11111111111111",
        "field2": "y",
        "name": "hello world",
    }
}

我需要使用 name 前缀“hello wo”和 field2 “y”获取所有文档。 尝试了很多查询,但都没有。有空格问题的前缀有各种解决方案,但是当为field2添加过滤/另一个查询时,结果会被破坏。

感谢。

1 个答案:

答案 0 :(得分:6)

您可以通过3个步骤实现此目的:

  1. 将字段名称的映射更改为 not_analyzed
  2. 使用 match_phrase_prefix 查询(文档here
  3. 通过将此查询结果包装在已过滤查询中并使用期限过滤器对字段2使用值" y"
  4. 来过滤此查询结果

    您可以看到它使用以下数据集:

    PUT test/prefix/_mapping
    {
      "properties": {
        "name":{
          "type": "string",
          "index": "not_analyzed"
        }
      }
    }
    
    //should match
    PUT test/prefix/2333345
    {
      "field1": "11111111111111",
      "field2": "y",
      "name": "hello world"
    }
    
    //should match    
    PUT test/prefix/1112223
    {
      "field1": "22222222222222",
      "field2": "y",
      "name": "hello wombat"
    }
    
    //should not match (field2 value is different)
    PUT test/prefix/4445556
    {
      "field1": "33333333333333",
      "field2": "z",
      "name": "hello world"
    }
    
    //should not match (second word not starting with wo)
    PUT test/prefix/4445556
    {
      "field1": "33333333333333",
      "field2": "y",
      "name": "hello zombie"
    }
    

    然后,查询是:

    GET test/prefix/_search
    {
      "query": {
        "filtered": {
          "query": {
            "match_phrase_prefix" : {
            "name" : "hello wo"
            }
          },
          "filter": {
            "term": {
              "field2": "y"
            }  
          }
        } 
      }
    }
    

    按预期输出文件1112223和2333345:

    {
       "took": 20,
       "timed_out": false,
       "_shards": {
          "total": 5,
          "successful": 5,
          "failed": 0
       },
       "hits": {
          "total": 2,
          "max_score": 1.592944,
          "hits": [
             {
                "_index": "test",
                "_type": "prefix",
                "_id": "2333345",
                "_score": 1.592944,
                "_source": {
                   "field1": "11111111111111",
                   "field2": "y",
                   "name": "hello world"
                }
             },
             {
                "_index": "test",
                "_type": "prefix",
                "_id": "1112223",
                "_score": 1.592944,
                "_source": {
                   "field1": "22222222222222",
                   "field2": "y",
                   "name": "hello wombat"
                }
             }
          ]
       }
    }