【问题标题】:Elasticsearch doesn't work with an slash at beginningElasticsearch 不能在开头使用斜线
【发布时间】:2015-03-03 07:55:10
【问题描述】:

我的数据库中充满了这样的文件:

{
  _index: "bla_bla",
  .
  .
  .
  _source: {
    domain: "somedomain.extension",
    path: "/you/know/the/path",
    lang: "en",
    keywords: ["yeah", "you", "rock", "dude", "help", "me", "good", "samaritan"]
  }
}

当我搜索时——不管我在寻找什么——它就像一个魅力,但是,如果我尝试通过使用名为 path 的字段来过滤某些东西——它——只是——不会'不工作;没有一个错误或警告被抛出。经过艰苦的研究,我想这是因为路径开头的斜线,我是否正确,但无论如何我需要像这样过滤:

{
    "query": {
        "filtered": {
            "filter": {
                "bool": {
                    "should": {
                        "terms": {
                            "keywords": ["stackoverflow", "rocks", "!"]
                        }
                    },
                    "must_not": {
                        "term": {
                            "path": "/"
                            // This works, i.e -> "lang": "en"
                        }
                    }
                }       
            }
        }
    },
    "from": 0,
    "size": 9
}

TL;DR:拥有 urls 的数据库,我怎样才能只获得 non-root [路径长于“ /"] 的?

【问题讨论】:

    标签: string elasticsearch search filter nosql


    【解决方案1】:

    在 ElasticSearch 中,文本被分割成多个字符,包括斜线。您需要做的是使用“not_analyzed”索引。这是一个工作示例,请注意“路径”字段上的索引规范:

    PUT /index1/test/_mapping
    {
        "test" : {
            "properties" : {
                "message" : {"type" : "string"},
                "path" : {"type" : "string", "index" : "not_analyzed"}
            }
        }
    }
    
    POST index1/test
    {
      "path" : "/foo/bar"  
    }
    
    GET index1/test/_search
    {
      "query": {
        "filtered": {
          "filter": {
            "term": {
              "path": "/foo/bar"
            }
          }
        }
      }
    }
    

    【讨论】:

    • 我可以轻松更改映射的这个参数吗? @jhilden
    • 如果您放置更新的映射,它将适用于所有新数据,但为了适用于现有数据,您需要重新索引。有很多帖子可以帮助完成这项任务。
    【解决方案2】:

    免责声明:我不是 ES 方面的专家,但如果正确理解它,您想要排除所有只有 / 的文档。到底。看到您始终将数据存储为/path,如果您有一个包含 1 个字符的字符串,它应该始终是/,那么为什么不使用正则表达式呢?

    http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-regexp-filter.html

    我认为这样的事情应该可以解决问题:

        {
        "query": {
            "filtered": {
                "filter": {
                    "and": [
                        {
                            "bool": {
                                "should": {
                                    "terms": {
                                        "keywords": [
                                            "stackoverflow",
                                            "rocks",
                                            "!"
                                        ]
                                    }
                                }
                            }
                        },
                        {
                            "filter": {
                                "regexp": {
                                    "path": ".{1,}"
                                }
                            }
                        }
                    ]
                }
            }
        },
        "from": 0,
        "size": 9
    }
    

    【讨论】:

      猜你喜欢
      • 2019-07-24
      • 1970-01-01
      • 2011-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多