弹性搜索嵌套查询

时间:2015-07-07 08:26:11

标签: c# elasticsearch nest

当我使用此查询进行搜索时:

{
   "query": {
      "bool": {
         "should": [
            {
               "nested": {
                  "path": "userQuestionlst",
                  "query": {
                     "bool": {
                        "must": [
                           {
                              "match": {
                                 "userQuestionlst.question": "Any pet peeves?"
                              }
                           },
                           {
                              "match": {
                                 "userQuestionlst.answer": "yes tiger"
                              }
                           }
                        ]
                     }
                  }
               }
            }
         ]
      }
   }
}

它为我提供了这个结果:

   {
       "took": 2,
       "timed_out": false,
       "_shards": {
          "total": 2,
          "successful": 2,
          "failed": 0
       },
       "hits": {
          "total": 2,
          "max_score": 2.5099993,
          "hits": [
             {
                "_index": "roommate",
                "_type": "usermaster",
                "_id": "5597b5aa3163501e40cac8ca",
                "_score": 2.5099993,
                "_source": {
                   "iD": "5597b5aa3163501e40cac8ca",
                   "firstName": "Trilok",
                   "lastName": "chandra",
                   "hometown": "Akutan Alaska",
                   "dob": "2002-08-31T00:00:00",
                   "relationShip": "Single",
                   "gender": "Male",
                   "profilePicturePath": "",
                   "isRoom": false,
                   "isActive": false,
                   "userQuestionlst": [
                      {
                         "question": "How often do you clean up?",
                         "answer": "I am a tidy person",
                         "specialField": ""
                      },
                      {
                         "question": "Any pet peeves?",
                         "answer": "yes tiger",
                         "specialField": "PetPeeves"
                      },
                      {
                         "question": "Are you ok with opposite sex roommates?",
                         "answer": "Yes",
                         "specialField": ""
                      },
                      {
                         "question": "Do you prefer to be similar age with your roommates?",
                         "answer": "Yes",
                         "specialField": ""
                      },
                      {
                         "question": "Spoken Languages",
                         "answer": "Hindi,",
                         "specialField": "Languages"
                      },
                      {
                         "question": "Are you currently enrolled in college?",
                         "answer": "Yes",
                         "specialField": ""
                      },
                      {
                         "question": "Where are you going to school?",
                         "answer": "Jacksonville University",
                         "specialField": ""
                      },
                      {
                         "question": "When do you graduate?",
                         "answer": "2021",
                         "specialField": ""
                      },
                      {
                         "question": "Where are taking most of your classes?",
                         "answer": "On Campus",
                         "specialField": ""
                      },
                      {
                         "question": "What Major?",
                         "answer": "Judaism & Jewish Studies",
                         "specialField": ""
                      },
                      {
                         "question": "Do you have any pets?",
                         "answer": "Yes",
                         "specialField": ""
                      },
                      {
                         "question": "What kind of pets do you have?",
                         "answer": "Turtle",
                         "specialField": "Pets"
                      },
                      {
                         "question": "Are you OK with roommates who have pets?",
                         "answer": "Sure",
                         "specialField": ""
                      },
                      {
                         "question": "Do you drink alcohol?",
                         "answer": "Yes",
                         "specialField": ""
                      },
                      {
                         "question": "How often do you drink?",
                         "answer": "Drinking now",
                         "specialField": "Drinkalcohol"
                      },
                      {
                         "question": "Are you OK with a roommate who drinks?",
                         "answer": "Sure",
                         "specialField": ""
                      },
                      {
                         "question": "Do you smoke cigarettes?",
                         "answer": "Inside and outside",
                         "specialField": "Smokes"
                      },
                      {
                         "question": "Are you ok with a roommate who smokes?",
                         "answer": "Inside and out is fine",
                         "specialField": ""
                      },
                      {
                         "question": "Would you like to be matched based upon your political alignment?",
                         "answer": "Yes",
                         "specialField": ""
                      },
                      {
                         "question": "Which political party do you associate most with?",
                         "answer": "Green Party",
                         "specialField": "PoliticalParty"
                      },
                      {
                         "question": "Would you like to be matched based upon your religious alignment?",
                         "answer": "Yes",
                         "specialField": ""
                      },
                      {
                         "question": "What religion do you associate most with?",
                         "answer": "Hinduism",
                         "specialField": ""
                      },
                      {
                         "question": "Do you play sports?",
                         "answer": "No",
                         "specialField": ""
                      },
                      {
                         "question": "Do you play video games?",
                         "answer": "No",
                         "specialField": ""
                      },
                      {
                         "question": "Do you play board or card games?",
                         "answer": "No",
                         "specialField": ""
                      },
                      {
                         "question": "Have any favorite TV shows?",
                         "answer": "No",
                         "specialField": ""
                      },
                      {
                         "question": "Have any favorite movies?",
                         "answer": "No",
                         "specialField": ""
                      },
                      {
                         "question": "Are you a music fan?",
                         "answer": "No",
                         "specialField": ""
                      },
                      {
                         "question": "Do you have any hobbies?",
                         "answer": "No",
                         "specialField": ""
                      },
                      {
                         "question": "Where do you live?",
                         "answer": "Akutan Alaska",
                         "specialField": "Address",
                         "location": {
                            "lat": 54.143,
                            "lon": -165.7854
                         }
                      },
                      {
                         "question": "What kind of place do you have?",
                         "answer": "Apartment",
                         "specialField": "PreferPlace"
                      },
                      {
                         "question": "How much to rent at your place?",
                         "answer": "500",
                         "specialField": "Rent"
                      },
                      {
                         "question": "When is the space available?",
                         "answer": "4/2/2016",
                         "specialField": ""
                      },
                      {
                         "question": "How long do you need a roommate for?",
                         "answer": "A year or so",
                         "specialField": "HowlongLookingRent"
                      },
                      {
                         "question": "Does the renter share a bathroom?",
                         "answer": "Yes",
                         "specialField": ""
                      },
                      {
                         "question": "Please share some details about the space for rent",
                         "answer": "kl",
                         "specialField": ""
                      }
                   ],
                   "lastlogin": "2015-07-07T11:18:21.6556311+05:30"
                }
             },
             {
                "_index": "roommate",
                "_type": "usermaster",
                "_id": "559b676e31635021dc804115",
                "_score": 0.50675774,
                "_source": {
                   "iD": "559b676e31635021dc804115",
                   "firstName": "pratik",
                   "lastName": "jangid",
                   "hometown": "jodpur",
                   "dob": "2000-10-17T00:00:00",
                   "relationShip": "Married",
                   "gender": "Male",
                   "isRoom": false,
                   "isActive": false,
                   "userQuestionlst": [
                      {
                         "question": "Where do you live?",
                         "answer": "Anchorage Alaska",
                         "specialField": "Address",
                         "location": {
                            "lat": 61.2116,
                            "lon": -149.8761
                         }
                      },
                      {
                         "question": "What kind of place do you have?",
                         "answer": "Apartment",
                         "specialField": "PreferPlace"
                      },
                      {
                         "question": "How much to rent at your place?",
                         "answer": "344",
                         "specialField": "Rent"
                      },
                      {
                         "question": "When is the space available?",
                         "answer": "2/5/2021",
                         "specialField": ""
                      },
                      {
                         "question": "How long do you need a roommate for?",
                         "answer": "A year or so",
                         "specialField": "HowlongLookingRent"
                      },
                      {
                         "question": "Does the renter share a bathroom?",
                         "answer": "Yes",
                         "specialField": ""
                      },
                      {
                         "question": "Please share some details about the space for rent",
                         "answer": "adsf",
                         "specialField": ""
                      },
                      {
                         "question": "How often do you clean up?",
                         "answer": "My clothes are organized by color",
                         "specialField": ""
                      },
                      {
                         "question": "Any pet peeves?",
                         "answer": "a",
                         "specialField": "PetPeeves"
                      },
                      {
                         "question": "Are you ok with opposite sex roommates?",
                         "answer": "Yes",
                         "specialField": ""
                      },
                      {
                         "question": "Do you prefer to be similar age with your roommates?",
                         "answer": "Yes",
                         "specialField": ""
                      },
                      {
                         "question": "Spoken Languages",
                         "answer": "English,Hindi,",
                         "specialField": "Languages"
                      },
                      {
                         "question": "Are you currently enrolled in college?",
                         "answer": "No",
                         "specialField": ""
                      },
                      {
                         "question": "Do you have any pets?",
                         "answer": "Yes",
                         "specialField": ""
                      },
                      {
                         "question": "What kind of pets do you have?",
                         "answer": "Bird,Turtle",
                         "specialField": "Pets"
                      },
                      {
                         "question": "Are you OK with roommates who have pets?",
                         "answer": "Sure",
                         "specialField": ""
                      },
                      {
                         "question": "Do you drink alcohol?",
                         "answer": "Yes",
                         "specialField": ""
                      },
                      {
                         "question": "How often do you drink?",
                         "answer": "Weekends",
                         "specialField": "Drinkalcohol"
                      },
                      {
                         "question": "Are you OK with a roommate who drinks?",
                         "answer": "Sure",
                         "specialField": ""
                      },
                      {
                         "question": "Do you smoke cigarettes?",
                         "answer": "Outside only",
                         "specialField": "Smokes"
                      },
                      {
                         "question": "Are you ok with a roommate who smokes?",
                         "answer": "Inside and out is fine",
                         "specialField": ""
                      },
                      {
                         "question": "Would you like to be matched based upon your political alignment?",
                         "answer": "Not important",
                         "specialField": ""
                      },
                      {
                         "question": "Would you like to be matched based upon your religious alignment?",
                         "answer": "Yes",
                         "specialField": ""
                      },
                      {
                         "question": "What religion do you associate most with?",
                         "answer": "Anglicanism",
                         "specialField": ""
                      },
                      {
                         "question": "Any thing else you want people to know?",
                         "answer": "a",
                         "specialField": "Aboutme"
                      }
                   ],
                   "lastlogin": "0001-01-01T00:00:00"
                }
             }
          ]
       }
    }

而且,如果我查询如下:

{
   "query": {
      "bool": {
         "should": [
            {
               "nested": {
                  "path": "userQuestionlst",
                  "query": {
                     "bool": {
                        "must": [
                           {
                              "term": {
                                 "userQuestionlst.question": "Any pet peeves?"
                              }
                           },
                           {
                              "term": {
                                 "userQuestionlst.answer": "yes tiger"
                              }
                           }
                        ]
                     }
                  }
               }
            }
         ]
      }
   }
}

但是,预计不会出现这种结果:

{
   "took": 16,
   "timed_out": false,
   "_shards": {
      "total": 2,
      "successful": 2,
      "failed": 0
   },
   "hits": {
      "total": 0,
      "max_score": null,
      "hits": []
   }
}

我想检索与大多数问题和答案相匹配的用户数据。

1 个答案:

答案 0 :(得分:0)

我自己找到了答案

{
   "query": {
      "bool": {
         "should": [
            {
               "nested": {
                  "path": "userQuestionlst",
                  "query": {
                     "bool": {
                        "must": [
                           {
                              "match": {
                                 "question": {
                                    "query": "Any pet peeves?",
                                    "operator": "and"
                                 }
                              }
                           },
                           {
                              "match": {
                                 "answer": {
                                    "query": "Nofgh"
                                 }
                              }
                           }
                        ]
                     }
                  }
               }
            },
            {
               "nested": {
                  "path": "userQuestionlst",
                  "query": {
                     "bool": {
                        "must": [
                           {
                              "match": {
                                 "question": {
                                    "query": "Spoken Languages",
                                    "operator": "and"
                                 }
                              }
                           },
                           {
                              "match": {
                                 "answer": {
                                    "query": "hindi"
                                 }
                              }
                           }
                        ]
                     }
                  }
               }
            },
            {
               "nested": {
                  "path": "userQuestionlst",
                  "query": {
                     "bool": {
                        "must": [
                           {
                              "match": {
                                 "question": {
                                    "query": "What kind of place do you have?",
                                    "operator": "and"
                                 }
                              }
                           },
                           {
                              "match": {
                                 "answer": {
                                    "query": "Attechment"
                                 }
                              }
                           }
                        ]
                     }
                  }
               }
            }
         ],
         "minimum_should_match": 1
      }
   }
}