【问题标题】:ElasticSearch - Search fails for 2 string fieldsElasticSearch - 搜索 2 个字符串字段失败
【发布时间】:2015-07-29 09:35:25
【问题描述】:

我遇到了搜索错误 - 需要一些帮助。我有带有 id、标题、艺术家、流派字段的文章索引。当我运行此查询时,我得到零结果-

POST /d3acampaign/article/_search

    {
       "query": {
            "filtered": {
                "query": {
                    "match": {"genre": "metal"}    
                },
                "filter": {
                    "term": {"artist": "Somanath"}         
                }

            }
        }
    } 

但是,如果我将查询更改为 - POST /d3acampaign/article/_search

    {
       "query": {
            "filtered": {
                "query": {
                    "match": {"genre": "metal"}    
                },
                "filter": {
                    "term": {"id": "7"}         
                }

            }
        }
    } 

我得到以下结果 -

    {
       "took": 1,
       "timed_out": false,
       "_shards": {
          "total": 5,
          "successful": 5,
          "failed": 0
       },
       "hits": {
          "total": 1,
          "max_score": 1.4054651,
          "hits": [
             {
                "_index": "d3acampaign",
                "_type": "article",
                "_id": "7",
                "_score": 1.4054651,
                "_source": {
                   "id": "7",
                   "title": "The Last Airbender",
                   "artist": "Somanath",
                   "genre": "metal"
                }
             }
          ]
       }
    } 

澄清 - 我注意到如果我尝试使用字符串,例如搜索失败。艺术家,标题

【问题讨论】:

    标签: elasticsearch term


    【解决方案1】:

    你得到空匹配的原因是当你使用term query查询时,它将匹配索引中的确切词包括大写

    默认分析器将创建小写索引。

    文本分析有多种方式:默认标准分析器 删除大多数标点符号,将文本分解为单个单词,以及 小写它们。 https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html

    解决办法:

       {
           "query": {
                "filtered": {
                    "query": {
                        "match": {"genre": "metal"}    
                    },
                    "filter": {
                        "term": {"artist": "somanath"}  //to lower case      
                    }
    
                }
            }
        } 
    

    另一种解决方案是将您的 索引映射 更改为 not_analyzed 以适应您的索引类型。

    完整示例:

    #!/bin/bash
    
    curl -XDELETE "localhost:9200/testindex"
    curl -XPUT "localhost:9200/testindex/?pretty" -d '
    {
        "mappings":{
            "test":{
                "properties":{
                    "name":{
                        "index":"not_analyzed",
                            "type":"string"
                    }
                }
            }
        }
    }'
    
    curl -XGET "localhost:9200/testindex/_mapping?pretty"
    
    curl -XPOST "localhost:9200/testindex/test/1" -d '{
        "name": "Jack"
    }'
    
    sleep 1
    echo -e
    echo -e
    echo -e
    echo -e "Filtered Query Search in not_analyzed index:"
    echo -e
    
    curl -XGET "localhost:9200/testindex/test/_search?pretty" -d '{
        "query": {
            "filtered": {
                    "filter": {
                        "term": {"name": "Jack"}  
                    }
    
            }
        }
    }'
    

    【讨论】:

    • 来自official doc:“HTTP GET 和 HTTP POST 都可用于执行带正文的搜索。由于并非所有客户端都支持带正文的 GET,因此也允许使用 POST。”
    • 我已经尝试过索引映射'not_analyzed',仍然遇到同样的问题。请确认我设置的映射是否正确,
    • @Guruprasad 你能粘贴你的映射吗?
    • FilterContainer fc = null; TermFilter titleTF = new TermFilter() { Field = "title", Value = titleToMatch, }; TermFilter 艺术家TF = new TermFilter() { Field = "艺术家", Value = artistToMatch, }; fc = titleTF & 艺术家TF;
    • codeQueryContainer qc = new FilteredQuery() { Filter = fc }; var searchRequest = new SearchRequest { SearchType = Elasticsearch.Net.SearchType.QueryAndFetch, Query = qc, Indices = new IndexNameMarker[] { database }, Types = new TypeNameMarker[] { "article" }, }; var findArticles = client.SearchAsync
      (searchRequest); findArtices.Wait();
    猜你喜欢
    • 2020-11-16
    • 1970-01-01
    • 2018-08-08
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    • 2019-11-16
    • 2018-12-22
    • 1970-01-01
    相关资源
    最近更新 更多