Elasticsearch只找到完美匹配

时间:2014-07-23 08:32:40

标签: elasticsearch

我一直试图这样做,阅读和搜索了很多,我还没有找到任何明确的答案或解决方案。

我们假设我们添加了一些文件:

$ curl -XPUT http://localhost:9200/tm/entries/1 -d '{"item": "foo" }'
{"_index":"tm","_type":"entries","_id":"1","_version":1,"created":true}

$ curl -XPUT http://localhost:9200/tm/entries/2 -d '{"item": "foo bar" }'
{"_index":"tm","_type":"entries","_id":"2","_version":1,"created":true}

$ curl -XPUT http://localhost:9200/tm/entries/3 -d '{"item": "foo bar foo" }'
{"_index":"tm","_type":"entries","_id":"3","_version":1,"created":true}

在此之后,我想找到与搜索查询完全匹配的文档

$ curl -XGET http://localhost:9200/_search?q=foo

结果包含所有3个文件,我只想得到一个匹配" foo"只有,没有别的。

此外,

$ curl -XGET http://localhost:9200/_search?q=bar foo

不应该返回任何结果。

  1. Elasticsearch能做到吗?
  2. 如何?
  3. 更新: 现有的映射:

    {
        "tm": {
            "mappings": {
                "entries": {
                    "properties": {
                        "item": {
                            "type": "string"
                        }
                    }
                }
            }
        }
    }
    

4 个答案:

答案 0 :(得分:2)

使用他跟随Mapping。

{
"tm": {
    "mappings": {
        "entries": {
            "properties": {
                "item": {
                    "type": "string", 
                    "index" : "not_analyzed"
                }
            }
        }
    }
}
}

使用术语查询查找完全匹配。不分析术语查询。refer

curl -XGET "http://localhost:9200/tm/entries/_search" -d'
{
"query": {
   "term": {
      "item": {
         "value": "foo bar"
      }
   }
  }
 }'

答案 1 :(得分:1)

尝试在映射中添加"index" : "not_analyzed"

查询应该是

{
  "match_phrase": {
    "item": "foo"
  }
} 

答案 2 :(得分:0)

您应该使用匹配查询而不是query_string。它会解决您的问题。

{
    "match" : {
        "item" : "bar foo"
    }
}

看看this

此外,请确保您搜索的字词实际存在于索引字段中。为此,您需要使用分析器"关键字"。有关更多信息,请查看this

由于

答案 3 :(得分:0)

如果您尝试从GET请求中搜索,我认为这可能会有所帮助:

$ curl -XGET http://localhost:9200/tm/entries/_search?q=item:foo

所以语法为_search?q= <field>:<value>

您可以在此处找到文档URI Search

并且,如果您尝试使用过滤器,最好使用not_analyzed进行映射(如上所述)。

对于复杂的查询,

curl -XPOST "http://localhost:9200/tm/entries/_search" -d'
{
    "filter": {
        "term": {
           "item": "foo"
        }
    }
}'
希望这会有所帮助。

相关问题