2

我想在 elasticsearch 中搜索,但即使条件不匹配也会被击中。例如:-

     {
         tweet: [
              {
                  firstname: Lav
                  lastname: byebye
             }
             {
                   firstname: pointto
                   lastname: ihadcre
             }
             {
                   firstname: letssearch
                   lastname: sarabhai
             }
          ]
      }
    }

现在有以下情况:-

1) 必须:- 名字:Lav 必须:- 姓氏:byebye 要求:应该被击中

获得:命中

2)必须:-名字:Lav必须:-姓氏:ihadcre要求:不应该被击中

获得:命中

我不应该在第二种情况下受到打击,这是问题

感谢帮助

4

1 回答 1

4

To achieve the behavior that you are describing, tweets should be indexed as nested objects and searched using nested query or filter. For example:

curl -XDELETE localhost:9200/test-idx
curl -XPUT localhost:9200/test-idx -d '{
    "settings": {
        "index.number_of_shards": 1,
        "index.number_of_replicas": 0
    },
    "mappings": {
        "doc": {
            "properties": {
                "tweet": {"type": "nested"}
            }
        }
    }
}'
curl -XPUT "localhost:9200/test-idx/doc/1" -d '{
    "tweet": [{
        "firstname": "Lav",
        "lastname": "byebye"
    }, {
        "firstname": "pointto",
        "lastname": "ihadcre"
    }, {
        "firstname": "letssearch",
        "lastname": "sarabhai"
    }]
}
'
echo
curl -XPOST "localhost:9200/test-idx/_refresh"
echo
curl "localhost:9200/test-idx/doc/_search?pretty=true" -d '{
    "query": {
        "nested" : {
            "path" : "tweet",
            "score_mode" : "avg",
            "query" : {
                "bool" : {
                    "must" : [
                        {
                            "match" : {"tweet.firstname" : "Lav"}
                        },
                        {
                            "match" : {"tweet.lastname" : "byebye"}
                        }
                    ]
                }
            }
        }
    }
}'
echo
curl "localhost:9200/test-idx/doc/_search?pretty=true" -d '{
    "query": {
        "nested" : {
            "path" : "tweet",
            "score_mode" : "avg",
            "query" : {
                "bool" : {
                    "must" : [
                        {
                            "match" : {"tweet.firstname" : "Lav"}
                        },
                        {
                            "match" : {"tweet.lastname" : "ihadcre"}
                        }
                    ]
                }
            }
        }
    }
}'
于 2013-07-03T13:36:33.627 回答