Elasticsearch检查fieldA是否具有值x,然后搜索fieldB for term xyz

时间:2016-06-05 09:39:51

标签: php laravel elasticsearch

假设我存储了以下数据,并希望在object Main extends App { args.length match { case 0 => ??? case 1 => ??? case _ => ??? } } xyold_value的文档的new_valuefield_name字段中搜索字词curriculum_name_encurriculum_name_pr

{
   "took": 1,
   "timed_out": false,
   "_shards": {
       "total": 5,
       "successful": 5,
       "failed": 0
   },
   "hits": {
      "total": 98,
      "max_score": 1,
      "hits": [
        {
           "_index": "my_index",
           "_type": "audit_field",
           "_id": "57526c197e83c",
           "_score": 1,
           "_source": {
              "session_id": 119,
              "trans_seq_no": 1,
              "table_seq_no": 1,
              "field_id": 2,
              "field_name": "curriculum_id",
              "new_value": 118,
              "old_value": null
           }
        },
        {
           "_index": "my_index",
           "_type": "audit_field",
           "_id": "57526c197f2c3",
           "_score": 1,
           "_source": {
               "session_id": 119,
               "trans_seq_no": 1,
               "table_seq_no": 1,
               "field_id": 3,
               "field_name": "curriculum_name_en",
               "new_value": "Test Index creation",
               "old_value": null
           }
        },
        {
           "_index": "my_index",
           "_type": "audit_field",
           "_id": "57526c198045c",
           "_score": 1,
           "_source": {
               "session_id": 119,
               "trans_seq_no": 1,
               "table_seq_no": 1,
               "field_id": 4,
               "field_name": "curriculum_name_pr",
               "new_value": null,
               "old_value": null
           }
        },
        {
           "_index": "my_index",
           "_type": "audit_field",
           "_id": "57526c1981512",
           "_score": 1,
           "_source": {
              "session_id": 119,
              "trans_seq_no": 1,
              "table_seq_no": 1,
              "field_id": 5,
              "field_name": "curriculum_name_pa",
              "new_value": null,
              "old_value": null
           }
        }      
        ]
      }
   }

可能还有更多字段,现在用户可以选择其中一个或多个字段,并在他/她选择的字段中定义搜索字词,挑战在这里,我们如何说弹性考虑{{1 }}匹配用户选择的字段,然后搜索field_nameold_value 例如,如果用户选择new_valuecurriculum_name_en,然后想要在curriculum_name_pr这些文档的xyold_value字段内搜索new_value field_name在领域之上。 我们该怎么做?

2 个答案:

答案 0 :(得分:1)

这个要求的想法是你需要做出类似的事情:只有当new_value与某个值匹配时,查询才需要匹配old_value和/或field_name。没有类似程序的方式来说明如果那样

我建议的是这样的:

{
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "field_name": [
              "curriculum_name_en",
              "curriculum_name_pr"
            ]
          }
        },
        {
          "multi_match": {
            "query": "Test Index",
            "fields": ["new_value","old_value"]
          }
        }
      ]
    }
  }
}

那么,你的如果那个条件是来自must查询的bool语句,那么 if 然后< / em>分支位于must内。

答案 1 :(得分:0)

这可以解决您的问题

{
  "query": {
    "filtered": {
      "filter": {
        "and": [
          {
             "query" : {
               "terms" : {
                 "field_name" : [
                   "curriculum_name_en",
                   "curriculum_name_pr"
                 ],
                 "minimum_match" : 1
               }
             }
           },
           {
             "query" : {
                "terms" : {
                   "new_value" : [
                      "test", "index"  
                   ],
                   "minimum_match" : 1
                }
             }
           }
         ]
       }
     }
   }
 } 
}