通过elasticsearch搜索数组

时间:2013-07-03 13:31:28

标签: elasticsearch

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

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

现在有以下条件: -

1)     必须: - 名字:Lav     必须: - 姓:byebye 必需:应该点击

获取:点击

2)     必须: - 名字:Lav     必须: - 姓:ihadcre 必需:不应该点击

获取:点击

我不应该在第二个条件下遇到问题

感谢您的帮助

1 个答案:

答案 0 :(得分:4)

要实现您所描述的行为,请将推文编入索引nested objects并使用nested queryfilter进行搜索。例如:

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"}
                        }
                    ]
                }
            }
        }
    }
}'